Skip to content

Commit

Permalink
Update respond_to? so that it recognizes the dynamic methods added by…
Browse files Browse the repository at this point in the history
… this gem.
  • Loading branch information
petergoldstein committed Dec 5, 2013
1 parent 86cb22d commit e150855
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
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 e150855

Please sign in to comment.