Module: ActsAsFriendable

Extended by:
ActiveSupport::Concern
Defined in:
lib/acts_as_friendable.rb,
lib/acts_as_friendable/version.rb,
lib/acts_as_friendable/friendships.rb,
lib/acts_as_friendable/model_additions.rb,
lib/generators/acts_as_friendable/install/install_generator.rb

Defined Under Namespace

Modules: ClassMethods, Generators Classes: Friendship

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/acts_as_friendable.rb', line 8

def self.included(receiver)
  receiver.class_eval do
    has_many :friendships, :class_name => "ActsAsFriendable::Friendship", :dependent => :destroy
    has_many :inverse_friendships, :class_name => "ActsAsFriendable::Friendship", :foreign_key => "friend_id", :dependent => :destroy
    has_many :direct_friends, :through => :friendships, :conditions => ["approved = ?", true], :source => :friend
    has_many :inverse_friends, :through => :inverse_friendships, :conditions => ["approved = ?", true], :source => :user
    has_many :pending_friends, :through => :friendships, :conditions => ["approved = ?", false], :source => :friend
    has_many :requested_friendships, :through => :inverse_friendships, :conditions => ["approved = ?", false], :source => :user
  end

  ActsAsFriendable::Friendship.class_eval do
    belongs_to "#{receiver.to_s.underscore}".to_sym, :class_name => "#{receiver.to_s}", :foreign_key => "user_id"
    belongs_to :friend, :class_name => "#{receiver.to_s}", :foreign_key => "friend_id"
  end

  receiver.extend         ClassMethods
  receiver.send :include
end

Instance Method Details

#friend_idsObject



12
13
14
# File 'lib/acts_as_friendable/model_additions.rb', line 12

def friend_ids
  friends.map {|f| f.id}
end

#friend_ids_and_meObject



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

def friend_ids_and_me
  friend_ids << id
end

#friendsObject



8
9
10
# File 'lib/acts_as_friendable/model_additions.rb', line 8

def friends
  direct_friends | inverse_friends
end

#pending_and_friend_idsObject



20
21
22
23
24
25
# File 'lib/acts_as_friendable/model_additions.rb', line 20

def pending_and_friend_ids
  pending_and_friend_ids = []
  pending_and_friend_ids << friend_ids
  pending_and_friend_ids << pending_friends.map {|f| f.id}
  pending_and_friend_ids.flatten
end