Class: RailsFriends::Friendship

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#status_wasObject (readonly)

Returns the value of attribute status_was.



13
14
15
# File 'lib/rails_friends/friendship.rb', line 13

def status_was
  @status_was
end

Class Method Details

.create_relation(one, other, options) ⇒ Object



53
54
55
56
57
# File 'lib/rails_friends/friendship.rb', line 53

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)


63
64
65
# File 'lib/rails_friends/friendship.rb', line 63

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

.find_blocked_friendship(friendable, friend) ⇒ Object



71
72
73
# File 'lib/rails_friends/friendship.rb', line 71

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

.find_one_side(one, other) ⇒ Object



75
76
77
# File 'lib/rails_friends/friendship.rb', line 75

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

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



59
60
61
# File 'lib/rails_friends/friendship.rb', line 59

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

.find_unblocked_friendship(friendable, friend) ⇒ Object



67
68
69
# File 'lib/rails_friends/friendship.rb', line 67

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

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails_friends/friendship.rb', line 41

def self.relation_attributes(one, other, status: nil)
  attr = {
    friendable_id: one.id,
    friendable_type: one.class.base_class.name,
    friend_id: other.id
  }

  attr[:status] = status if status

  attr
end

Instance Method Details

#block_by(blocker) ⇒ Object



36
37
38
39
# File 'lib/rails_friends/friendship.rb', line 36

def block_by(blocker)
  self.blocker_id = blocker.id
  block!
end