module ActsAsFriendable

Constants

VERSION

Public Class Methods

included(receiver) click to toggle source
# 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

Public Instance Methods

friend_ids() click to toggle source
# File lib/acts_as_friendable/model_additions.rb, line 12
def friend_ids
  friends.map {|f| f.id}
end
friend_ids_and_me() click to toggle source
# File lib/acts_as_friendable/model_additions.rb, line 16
def friend_ids_and_me
  friend_ids << id
end
friends() click to toggle source
# File lib/acts_as_friendable/model_additions.rb, line 8
def friends
  direct_friends | inverse_friends
end
pending_and_friend_ids() click to toggle source
# 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