Commit d3433c67ff90e5750d9d69df79384b190e95778b
Exists in
master
Merge pull request #26 from petergoldstein/feature/add_respond_to
respond_to? doesn't return true for dynamic methods added by method_missing
Showing
5 changed files
with
33 additions
and
2 deletions
Show diff stats
acts_as_follower.gemspec
... | ... | @@ -21,7 +21,7 @@ Gem::Specification.new do |s| |
21 | 21 | |
22 | 22 | s.add_development_dependency "sqlite3" |
23 | 23 | s.add_development_dependency "shoulda_create" |
24 | - s.add_development_dependency "shoulda", "3.5.0" | |
25 | - s.add_development_dependency "factory_girl", "4.2.0" | |
24 | + s.add_development_dependency "shoulda", ">= 3.5.0" | |
25 | + s.add_development_dependency "factory_girl", ">= 4.2.0" | |
26 | 26 | s.add_development_dependency "rails", "~> 4.0.0" |
27 | 27 | end | ... | ... |
lib/acts_as_follower/followable.rb
... | ... | @@ -55,6 +55,10 @@ module ActsAsFollower #:nodoc: |
55 | 55 | end |
56 | 56 | end |
57 | 57 | |
58 | + def respond_to?(m, include_private = false) | |
59 | + super || m.to_s[/count_(.+)_followers/] || m.to_s[/(.+)_followers/] | |
60 | + end | |
61 | + | |
58 | 62 | def blocked_followers_count |
59 | 63 | self.followings.blocked.count |
60 | 64 | end | ... | ... |
lib/acts_as_follower/follower.rb
... | ... | @@ -97,6 +97,10 @@ module ActsAsFollower #:nodoc: |
97 | 97 | end |
98 | 98 | end |
99 | 99 | |
100 | + def respond_to?(m, include_private = false) | |
101 | + super || m.to_s[/following_(.+)_count/] || m.to_s[/following_(.+)/] | |
102 | + end | |
103 | + | |
100 | 104 | # Returns a follow record for the current instance and followable object. |
101 | 105 | def get_follow(followable) |
102 | 106 | self.follows.unblocked.for_followable(followable).first | ... | ... |
test/acts_as_followable_test.rb
... | ... | @@ -251,6 +251,18 @@ class ActsAsFollowableTest < ActiveSupport::TestCase |
251 | 251 | assert_equal 1, @oasis.count_user_followers |
252 | 252 | end |
253 | 253 | end |
254 | + | |
255 | + context "respond_to?" do | |
256 | + should "advertise that it responds to following methods" do | |
257 | + assert @oasis.respond_to?(:user_followers) | |
258 | + assert @oasis.respond_to?(:user_followers_count) | |
259 | + end | |
260 | + | |
261 | + should "return false when called with a nonexistent method" do | |
262 | + assert (not @oasis.respond_to?(:foobar)) | |
263 | + end | |
264 | + end | |
265 | + | |
254 | 266 | end |
255 | 267 | |
256 | 268 | end | ... | ... |
test/acts_as_follower_test.rb
... | ... | @@ -173,6 +173,17 @@ class ActsAsFollowerTest < ActiveSupport::TestCase |
173 | 173 | end |
174 | 174 | end |
175 | 175 | |
176 | + context "respond_to?" do | |
177 | + should "advertise that it responds to following methods" do | |
178 | + assert @sam.respond_to?(:following_users) | |
179 | + assert @sam.respond_to?(:following_users_count) | |
180 | + end | |
181 | + | |
182 | + should "return false when called with a nonexistent method" do | |
183 | + assert (not @sam.respond_to?(:foobar)) | |
184 | + end | |
185 | + end | |
186 | + | |
176 | 187 | context "destroying follower" do |
177 | 188 | setup do |
178 | 189 | @jon.destroy | ... | ... |