-
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.
Add more tests for friendships and fix some newly discovered bugs
- Loading branch information
1 parent
1decb7d
commit e4d0ad4
Showing
6 changed files
with
71 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
# A class for handling contacts which ensures that the association is always symmetrical | ||
class Friendship < ApplicationRecord | ||
self.table_name = 'users_users' | ||
belongs_to :user, foreign_key: :contact_id, inverse_of: :friendships | ||
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| | ||
Friendship.create!(user_id: c.contact_id, contact_id: c.user_id) unless Friendship.find_by(contact_id: c.user_id) | ||
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) | ||
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
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