acts_as_followable.rb
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
module ActiveRecord #:nodoc:
module Acts #:nodoc:
module Followable
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def acts_as_followable
has_many :follows, :as => :followable, :dependent => :destroy
include ActiveRecord::Acts::Followable::InstanceMethods
end
end
# This module contains instance methods
module InstanceMethods
# Returns the number of followers a record has.
def followers_count
self.follows.size
end
# Returns the following records.
def followers
Follow.find(:all, :include => [:follower], :conditions => ["followable_id = ? AND followable_type = ?",
self.id, parent_class_name(self)]).collect {|f| f.follower }
end
# Returns true if the current instance is followed by the passed record.
def followed_by?(follower)
self.follows.find(:first, :conditions => ["follower_id = ? AND follower_type = ?", follower.id, parent_class_name(follower)]) ? true : false
end
# Retrieves the parent class name if using STI.
def parent_class_name(obj)
if obj.class.superclass != ActiveRecord::Base
return obj.class.superclass.name
end
return obj.class.name
end
end
end
end
end