class HasFriendship::Friendship

Public Class Methods

create_relation(one, other, options) click to toggle source
# File lib/has_friendship/friendship.rb, line 48
def self.create_relation(one, other, options)
  relation = new relation_attributes(one, other)
  relation.attributes = options
  relation.save
end
exist?(friendable, friend) click to toggle source
# File lib/has_friendship/friendship.rb, line 58
def self.exist?(friendable, friend)
  find_relation(friendable, friend).any? && find_relation(friend, friendable).any?
end
find_blocked_friendship(friendable, friend) click to toggle source
# File lib/has_friendship/friendship.rb, line 66
def self.find_blocked_friendship(friendable, friend)
  find_relation(friendable, friend).where(status: 3).first
end
find_one_side(one, other) click to toggle source
# File lib/has_friendship/friendship.rb, line 70
def self.find_one_side(one, other)
  find_by(relation_attributes(one, other))
end
find_relation(friendable, friend, status: nil) click to toggle source
# File lib/has_friendship/friendship.rb, line 54
def self.find_relation(friendable, friend, status: nil)
  where relation_attributes(friendable, friend, status: status)
end
find_unblocked_friendship(friendable, friend) click to toggle source
# File lib/has_friendship/friendship.rb, line 62
def self.find_unblocked_friendship(friendable, friend)
  find_relation(friendable, friend).where.not(status: 3).first
end
relation_attributes(one, other, status: nil) click to toggle source
# File lib/has_friendship/friendship.rb, line 34
def self.relation_attributes(one, other, status: nil)
  attr = {
    friendable_id: one.id,
    friendable_type: one.class.base_class.name,
    friend_id: other.id
  }

  if status
    attr[:status] = status
  end

  attr
end

Private Instance Methods

satisfy_custom_conditions() click to toggle source
# File lib/has_friendship/friendship.rb, line 76
def satisfy_custom_conditions
  return unless friend.respond_to?(:friendship_errors)

  friend.friendship_errors(friendable).to_a.each do |friendship_error|
    errors.add(:base, friendship_error)
  end
end