Module: PowerOfFriendship::ActsAsFriend::ClassMethods

Defined in:
lib/power_of_friendship.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pow_friendship_modelObject



20
21
22
# File 'lib/power_of_friendship.rb', line 20

def self.pow_friendship_model
  "#{self.name}Friendship".constantize
end

.pow_friendship_tableObject



24
25
26
# File 'lib/power_of_friendship.rb', line 24

def self.pow_friendship_table
  "#{pow_name_und}_friendships"
end

.pow_model_idObject



28
29
30
31
# File 'lib/power_of_friendship.rb', line 28

def self.pow_model_id
  "#{pow_name_und}_id".to_sym

end

.pow_name_undObject



16
17
18
# File 'lib/power_of_friendship.rb', line 16

def self.pow_name_und
  self.name.underscore.downcase
end

Instance Method Details

#act_as_friend(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/power_of_friendship.rb', line 14

def act_as_friend(options = {})

  def self.pow_name_und
    self.name.underscore.downcase
  end

  def self.pow_friendship_model
    "#{self.name}Friendship".constantize
  end

  def self.pow_friendship_table
    "#{pow_name_und}_friendships"
  end

  def self.pow_model_id
    "#{pow_name_und}_id".to_sym

  end

  has_many :friends, 
  through: :approved_friendships,
  :source => :friend

  has_many :approved_friendships,
  ->(t) {where("#{t.pow_friendship_table}.pending" => false)},
  :foreign_key => pow_model_id,
  :class_name => pow_friendship_model


  has_many  :friendships,
  foreign_key: pow_model_id,
  class_name: pow_friendship_model,
  dependent: :destroy

  has_many  :followers,
  :through => :inverse_friendships,
  source: pow_name_und

  has_many  :invited_friends,
  :through => :invited_friendships,
  source: :friend

  has_many :invited_friendships,
  ->(t) {where("#{t.pow_friendship_table}.pending" => true)},
  :foreign_key => pow_model_id,
  :class_name => pow_friendship_model


  has_many  :inverse_friendships,
  :class_name => pow_friendship_model,
  :foreign_key => :friend_id,
  dependent: :destroy

  has_many  :inverse_followers,
  :through => :friendships,
  source: :friend

  has_many  :pending_friends,
  ->(t) {where("#{t.pow_friendship_table}.pending" => true)},
  :through => :pending_friendships,
  source: pow_name_und

  has_many :pending_friendships,
  ->(t) {where("#{t.pow_friendship_table}.pending" => true)},
  :foreign_key => :friend_id,
  :class_name => pow_friendship_model


  include PowerOfFriendship::ActsAsFriend::LocalInstanceMethods
end