-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from hpi-swt2/feature/two-sided-contacts
Two sided contacts
- Loading branch information
Showing
9 changed files
with
90 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# A class for handling contacts which ensures that the association is always symmetrical | ||
class Friendship < ApplicationRecord | ||
self.table_name = 'users_users' | ||
self.primary_keys = :user_id, :contact_id | ||
|
||
belongs_to :user, inverse_of: :friendships | ||
belongs_to :contact, class_name: 'User', inverse_of: :friendships | ||
|
||
after_create do |c| | ||
unless Friendship.find_by(user_id: c.contact_id, contact_id: c.user_id) | ||
Friendship.create(user_id: c.contact_id, contact_id: c.user_id) | ||
end | ||
end | ||
|
||
after_destroy do |c| | ||
reciprocal = Friendship.find_by(contact_id: c.user_id, user_id: c.contact_id) | ||
reciprocal&.destroy | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Friendship, type: :model do | ||
let(:users) { FactoryBot.create_list :user, 3 } | ||
|
||
before { described_class.create(user_id: users[0].id, contact_id: users[1].id) } | ||
|
||
it 'creates a symmetrical association' do | ||
expect(described_class.find_by(user_id: users[0].id, contact_id: users[1].id)).to be_present | ||
expect(described_class.find_by(user_id: users[1].id, contact_id: users[0].id)).to be_present | ||
end | ||
|
||
it 'deletes both parts of the association' do | ||
described_class.find_by(user_id: users[0].id, contact_id: users[1].id).destroy! | ||
expect(described_class.find_by(user_id: users[0].id, contact_id: users[1].id)).to be_blank | ||
expect(described_class.find_by(user_id: users[1].id, contact_id: users[0].id)).to be_blank | ||
end | ||
|
||
describe 'multiple contacts' do | ||
before { described_class.create(user_id: users[0].id, contact_id: users[2].id) } | ||
|
||
it 'allows a user to have multiple contacts' do | ||
expect(described_class.find_by(user_id: users[0].id, contact_id: users[1].id)).to be_present | ||
expect(described_class.find_by(user_id: users[0].id, contact_id: users[2].id)).to be_present | ||
end | ||
|
||
it 'allows to only delete one of the contacts' do | ||
described_class.find_by(user_id: users[0].id, contact_id: users[1].id).destroy! | ||
expect(described_class.find_by(user_id: users[0].id, contact_id: users[1].id)).to be_blank | ||
expect(described_class.find_by(user_id: users[0].id, contact_id: users[2].id)).to be_present | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters