Last active
October 19, 2023 13:44
-
-
Save jibiel/5c18d36b93891cced991791529fc1686 to your computer and use it in GitHub Desktop.
DRY Mutual Friendships / Friends in Ruby on Rails
This file contains hidden or 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
# app/models/friendship.rb | |
class Friendship < ApplicationRecord | |
belongs_to :user | |
belongs_to :friend, class_name: 'User' | |
end |
This file contains hidden or 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
# app/queries/friendships_query.rb | |
module FriendshipsQuery | |
extend self | |
def both_ways(user_id:) | |
relation.unscope(where: :user_id) | |
.where(user_id: user_id) | |
.or(relation.where(friend_id: user_id)) | |
end | |
private | |
def relation | |
@relation ||= Friendship.all | |
end | |
end |
This file contains hidden or 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
# app/models/user.rb | |
class User < ApplicationRecord | |
has_many :friendships, | |
->(user) { FriendshipsQuery.both_ways(user_id: user.id) }, | |
inverse_of: :user, | |
dependent: :destroy | |
has_many :friends, | |
->(user) { UsersQuery.friends(user_id: user.id, scope: true) }, | |
through: :friendships | |
end |
This file contains hidden or 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
# app/queries/users_query.rb | |
module UsersQuery | |
extend self | |
def friends(user_id:, scope: false) | |
query = relation.joins(sql(scope: scope)).where.not(id: user_id) | |
query.where(friendships: { user_id: user_id }) | |
.or(query.where(friendships: { friend_id: user_id })) | |
end | |
private | |
def relation | |
@relation ||= User.all | |
end | |
def sql(scope: false) | |
if scope | |
<<~SQL | |
OR users.id = friendships.user_id | |
SQL | |
else | |
<<~SQL | |
INNER JOIN friendships | |
ON users.id = friendships.friend_id | |
OR users.id = friendships.user_id | |
SQL | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment