Commit d2604e8cdc00eb44cfeb987773f78910f906218f
1 parent
e87c7ddf
Exists in
master
and in
3 other branches
Introduce private parent_class_name, to work around STI.
Showing
1 changed file
with
16 additions
and
7 deletions
Show diff stats
lib/acts_as_follower.rb
... | ... | @@ -20,13 +20,13 @@ module ActiveRecord |
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 |
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, :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. |
... | ... | @@ -72,7 +72,7 @@ module ActiveRecord |
72 | 72 | # e.g. following_users == following_by_type('User') |
73 | 73 | def method_missing(m, *args) |
74 | 74 | if m.to_s[/following_(.+)/] |
75 | - following_by_type($1.classify) | |
75 | + following_by_type(parent_class_name($1).classify) | |
76 | 76 | else |
77 | 77 | super |
78 | 78 | end |
... | ... | @@ -82,9 +82,18 @@ module ActiveRecord |
82 | 82 | |
83 | 83 | # Returns a follow record for the current instance and followable object. |
84 | 84 | def get_follow(followable) |
85 | - 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]) | |
85 | + 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)]) | |
86 | 86 | end |
87 | - | |
87 | + | |
88 | + # Retrieves the parent class name if using STI. | |
89 | + def parent_class_name(obj) | |
90 | + if obj.class.superclass != ActiveRecord::Base | |
91 | + return obj.class.superclass.name | |
92 | + end | |
93 | + | |
94 | + return obj.class.name | |
95 | + end | |
96 | + | |
88 | 97 | end |
89 | 98 | |
90 | 99 | end | ... | ... |