Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds blocked_by? and blocking? convenience methods #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/acts_as_follower/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/acts_as_follower/follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/acts_as_followable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/acts_as_follower_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down