Class: HasFriendship::Friendship

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/has_friendship/friendship.rb

Class Method Summary collapse

Class Method Details

.create_relation(one, other, options) ⇒ Object



48
49
50
51
52
# 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) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# 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) ⇒ Object



66
67
68
# 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) ⇒ Object



70
71
72
# 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) ⇒ Object



54
55
56
# 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) ⇒ Object



62
63
64
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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