From d57f1f1d9c2b12f27ee89c844acb8f9243533e9b Mon Sep 17 00:00:00 2001 From: Ross-Hunter Date: Sun, 25 May 2014 20:46:47 -0400 Subject: [PATCH] Adds methods for checking blocked status --- lib/acts_as_follower/followable.rb | 4 ++++ lib/acts_as_follower/follower.rb | 10 ++++++++++ test/acts_as_followable_test.rb | 7 +++++++ test/acts_as_follower_test.rb | 17 +++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/lib/acts_as_follower/followable.rb b/lib/acts_as_follower/followable.rb index 2c91272..556d908 100644 --- a/lib/acts_as_follower/followable.rb +++ b/lib/acts_as_follower/followable.rb @@ -86,6 +86,10 @@ def followed_by?(follower) self.followings.unblocked.for_follower(follower).first.present? end + def blocked_by?(follower) + follower.followings.blocked.for_follower(self).first.present? + end + def block(follower) get_follow_for(follower) ? block_existing_follow(follower) : block_future_follow(follower) end diff --git a/lib/acts_as_follower/follower.rb b/lib/acts_as_follower/follower.rb index a086ebe..ae79be2 100644 --- a/lib/acts_as_follower/follower.rb +++ b/lib/acts_as_follower/follower.rb @@ -20,11 +20,21 @@ def following?(followable) 0 < Follow.unblocked.for_follower(self).for_followable(followable).count end + # Returns true if this instance is blocking the object passed as an argument. + def blocking?(followable) + 0 < Follow.blocked.for_follower(followable).for_followable(self).count + end + # Returns the number of objects this instance is following. def follow_count Follow.unblocked.for_follower(self).count end + # Returns the number of objects this instance is blocked by. + def block_count + Follow.blocked.for_follower(self).count + end + # Creates a new follow record for this instance to follow the passed object. # Does not allow duplicate records to be created. def follow(followable) diff --git a/test/acts_as_followable_test.rb b/test/acts_as_followable_test.rb index b7416a7..f7f84f6 100644 --- a/test/acts_as_followable_test.rb +++ b/test/acts_as_followable_test.rb @@ -109,6 +109,13 @@ class ActsAsFollowableTest < ActiveSupport::TestCase should "accept AR options" do assert_equal 1, @jon.blocks(:limit => 1).count end + + context "blocked_by" do + should "return blocked status" do + assert_equal false, @jon.blocked_by?(@bob) + assert_equal true, @sam.blocked_by?(@jon) + end + end end context "blocking a follower" do diff --git a/test/acts_as_follower_test.rb b/test/acts_as_follower_test.rb index 535195b..50ce997 100644 --- a/test/acts_as_follower_test.rb +++ b/test/acts_as_follower_test.rb @@ -9,6 +9,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase should "be defined" do assert @sam.respond_to?(:following?) + assert @sam.respond_to?(:blocking?) assert @sam.respond_to?(:follow_count) assert @sam.respond_to?(:follow) assert @sam.respond_to?(:stop_following) @@ -38,6 +39,22 @@ class ActsAsFollowerTest < ActiveSupport::TestCase end end + context "blocking" do + setup do + @jon.block(@sam) + end + + should "return blocking_status" do + assert_equal false, @sam.blocking?(@jon) + assert_equal true, @jon.blocking?(@sam) + end + + should "return block_count" do + assert_equal 0, @jon.block_count + assert_equal 1, @sam.block_count + end + end + context "follow a friend" do setup do @jon.follow(@sam)