Skip to content

Commit

Permalink
Merge pull request tcocca#26 from petergoldstein/feature/add_respond_to
Browse files Browse the repository at this point in the history
respond_to? doesn't return true for dynamic methods added by method_missing
  • Loading branch information
tcocca committed Dec 5, 2013
2 parents 208ad56 + 85a61ff commit d3433c6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions acts_as_follower.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|

s.add_development_dependency "sqlite3"
s.add_development_dependency "shoulda_create"
s.add_development_dependency "shoulda", "3.5.0"
s.add_development_dependency "factory_girl", "4.2.0"
s.add_development_dependency "shoulda", ">= 3.5.0"
s.add_development_dependency "factory_girl", ">= 4.2.0"
s.add_development_dependency "rails", "~> 4.0.0"
end
4 changes: 4 additions & 0 deletions lib/acts_as_follower/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def method_missing(m, *args)
end
end

def respond_to?(m, include_private = false)
super || m.to_s[/count_(.+)_followers/] || m.to_s[/(.+)_followers/]
end

def blocked_followers_count
self.followings.blocked.count
end
Expand Down
4 changes: 4 additions & 0 deletions lib/acts_as_follower/follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def method_missing(m, *args)
end
end

def respond_to?(m, include_private = false)
super || m.to_s[/following_(.+)_count/] || m.to_s[/following_(.+)/]
end

# Returns a follow record for the current instance and followable object.
def get_follow(followable)
self.follows.unblocked.for_followable(followable).first
Expand Down
12 changes: 12 additions & 0 deletions test/acts_as_followable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
assert_equal 1, @oasis.count_user_followers
end
end

context "respond_to?" do
should "advertise that it responds to following methods" do
assert @oasis.respond_to?(:user_followers)
assert @oasis.respond_to?(:user_followers_count)
end

should "return false when called with a nonexistent method" do
assert (not @oasis.respond_to?(:foobar))
end
end

end

end
11 changes: 11 additions & 0 deletions test/acts_as_follower_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
end
end

context "respond_to?" do
should "advertise that it responds to following methods" do
assert @sam.respond_to?(:following_users)
assert @sam.respond_to?(:following_users_count)
end

should "return false when called with a nonexistent method" do
assert (not @sam.respond_to?(:foobar))
end
end

context "destroying follower" do
setup do
@jon.destroy
Expand Down

0 comments on commit d3433c6

Please sign in to comment.