Class: HasFriendship::Friendship

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#status_wasObject (readonly)

Returns the value of attribute status_was.



15
16
17
# File 'lib/has_friendship/friendship.rb', line 15

def status_was
  @status_was
end

Class Method Details

.create_relation(one, other, options) ⇒ Object



62
63
64
65
66
# File 'lib/has_friendship/friendship.rb', line 62

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)


72
73
74
# File 'lib/has_friendship/friendship.rb', line 72

def self.exist?(friendable, friend)
  find_relation(friendable, friend).any? && find_relation(friend, friendable).any?
end

.find_blocked_friendship(friendable, friend) ⇒ Object



80
81
82
# File 'lib/has_friendship/friendship.rb', line 80

def self.find_blocked_friendship(friendable, friend)
  find_relation(friendable, friend).where(status: 3).first
end

.find_one_side(one, other) ⇒ Object



84
85
86
# File 'lib/has_friendship/friendship.rb', line 84

def self.find_one_side(one, other)
  find_by(relation_attributes(one, other))
end

.find_relation(friendable, friend, status: nil) ⇒ Object



68
69
70
# File 'lib/has_friendship/friendship.rb', line 68

def self.find_relation(friendable, friend, status: nil)
  where relation_attributes(friendable, friend, status: status)
end

.find_unblocked_friendship(friendable, friend) ⇒ Object



76
77
78
# File 'lib/has_friendship/friendship.rb', line 76

def self.find_unblocked_friendship(friendable, friend)
  find_relation(friendable, friend).where.not(status: 3).first
end

.relation_attributes(one, other, status: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/has_friendship/friendship.rb', line 48

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