Commit ade22bcb710bd429190ff0a91151ac4642199492
Exists in
master
and in
3 other branches
Merged in JDG's changes for adding STI support
Showing
2 changed files
with
19 additions
and
9 deletions
Show diff stats
lib/acts_as_followable.rb
... | ... | @@ -24,14 +24,15 @@ module ActiveRecord #:nodoc: |
24 | 24 | |
25 | 25 | # Returns the following records. |
26 | 26 | def followers |
27 | - self.follows.collect{ |f| f.follower } | |
27 | + Follow.find(:all, :include => [:follower], :conditions => ["followable_id = ? AND followable_type = ?", | |
28 | + self.id, parent_class_name(self)]).collect {|f| f.follower } | |
28 | 29 | end |
29 | 30 | |
30 | 31 | # Returns true if the current instance is followed by the passed record. |
31 | 32 | def followed_by?(follwer) |
32 | 33 | rtn = false |
33 | 34 | self.follows.each do |f| |
34 | - rtn = true if follwer.id == f.follower_id && follwer.class.name == f.follower_type | |
35 | + rtn = true if follwer.id == f.follower_id && parent_class_name(follwer) == f.follower_type | |
35 | 36 | end |
36 | 37 | rtn |
37 | 38 | end | ... | ... |
lib/acts_as_follower.rb
... | ... | @@ -20,13 +20,13 @@ module ActiveRecord #:nodoc: |
20 | 20 | def following?(followable) |
21 | 21 | 0 < Follow.count(:all, :conditions => [ |
22 | 22 | "follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?", |
23 | - self.id, self.class.name, followable.id, followable.class.name | |
23 | + self.id, parent_class_name(self), followable.id, parent_class_name(followable) | |
24 | 24 | ]) |
25 | 25 | end |
26 | 26 | |
27 | 27 | # Returns the number of objects this instance is following. |
28 | 28 | def follow_count |
29 | - Follow.count(:all, :conditions => ["follower_id = ? AND follower_type = ?", self.id, self.class.name]) | |
29 | + Follow.count(:all, :conditions => ["follower_id = ? AND follower_type = ?", self.id, parent_class_name(self)]) | |
30 | 30 | end |
31 | 31 | |
32 | 32 | # Creates a new follow record for this instance to follow the passed object. |
... | ... | @@ -49,13 +49,13 @@ module ActiveRecord #:nodoc: |
49 | 49 | # TODO: Remove from public API. |
50 | 50 | # Returns the follow records related to this instance by type. |
51 | 51 | def follows_by_type(followable_type) |
52 | - Follow.find(:all, :conditions => ["follower_id = ? AND follower_type = ? AND followable_type = ?", self.id, self.class.name, followable_type]) | |
52 | + Follow.find(:all, :conditions => ["follower_id = ? AND follower_type = ? AND followable_type = ?", self.id, parent_class_name(self), followable_type]) | |
53 | 53 | end |
54 | 54 | |
55 | 55 | # TODO: Remove from public API. |
56 | 56 | # Returns the follow records related to this instance by type. |
57 | 57 | def all_follows |
58 | - Follow.find(:all, :conditions => ["follower_id = ? AND follower_type = ?", self.id, self.class.name]) | |
58 | + Follow.find(:all, :include => [:followable], :conditions => ["follower_id = ? AND follower_type = ?", self.id, parent_class_name(self)]) | |
59 | 59 | end |
60 | 60 | |
61 | 61 | # Returns the actual records which this instance is following. |
... | ... | @@ -73,7 +73,7 @@ module ActiveRecord #:nodoc: |
73 | 73 | # e.g. following_users == following_by_type('User') |
74 | 74 | def method_missing(m, *args) |
75 | 75 | if m.to_s[/following_(.+)/] |
76 | - following_by_type($1.classify) | |
76 | + following_by_type(parent_class_name($1).classify) | |
77 | 77 | else |
78 | 78 | super |
79 | 79 | end |
... | ... | @@ -83,9 +83,18 @@ module ActiveRecord #:nodoc: |
83 | 83 | |
84 | 84 | # Returns a follow record for the current instance and followable object. |
85 | 85 | def get_follow(followable) |
86 | - Follow.find(:first, :conditions => ["follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?", self.id, self.class.name, followable.id, followable.class.name]) | |
86 | + Follow.find(:first, :conditions => ["follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?", self.id, parent_class_name(self), followable.id, parent_class_name(followable)]) | |
87 | 87 | end |
88 | - | |
88 | + | |
89 | + # Retrieves the parent class name if using STI. | |
90 | + def parent_class_name(obj) | |
91 | + if obj.class.superclass != ActiveRecord::Base | |
92 | + return obj.class.superclass.name | |
93 | + end | |
94 | + | |
95 | + return obj.class.name | |
96 | + end | |
97 | + | |
89 | 98 | end |
90 | 99 | |
91 | 100 | end | ... | ... |