Skip to content

Commit

Permalink
Aliases block with restrict
Browse files Browse the repository at this point in the history
  • Loading branch information
broskoski committed Apr 5, 2022
1 parent a2fec35 commit fa95b7b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/acts_as_follower/follow_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def descending
def unblocked
where(blocked: false)
end
alias :unrestricted :unblocked

# returns blocked Follow records.
def blocked
where(blocked: true)
end
alias :restricted :blocked
end
end
9 changes: 9 additions & 0 deletions lib/acts_as_follower/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def respond_to?(m, include_private = false)
def blocked_followers_count
followings.blocked.count
end
alias :restricted_followers_count :blocked_followers_count

# Returns the followings records scoped
def followers_scoped
Expand All @@ -77,6 +78,12 @@ def followers(options = {})
# blocked_followers_scope.to_a.collect{|f| f.follower}
# end

def restricts(options={})
blocked_followers_scope = followers_scoped.blocked
blocked_followers_scope = apply_options_to_scope(blocked_followers_scope, options)
blocked_followers_scope.to_a.collect{|f| f.follower}
end

# Returns true if the current instance is followed by the passed record
# Returns false if the current instance is blocked by the passed record or no follow is found
def followed_by?(follower)
Expand All @@ -86,10 +93,12 @@ def followed_by?(follower)
def block(follower)
get_follow_for(follower) ? block_existing_follow(follower) : block_future_follow(follower)
end
alias :restrict :block

def unblock(follower)
get_follow_for(follower).try(:delete)
end
alias :unrestrict :unblock

def get_follow_for(follower)
followings.for_follower(follower).first
Expand Down
1 change: 1 addition & 0 deletions lib/generators/templates/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class Follow < ActiveRecord::Base
def block!
update_attribute(:blocked, true)
end
alias :restrict! :block!
end
98 changes: 95 additions & 3 deletions test/acts_as_followable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
# end
end

context 'restricts' do
setup do
@bob = FactoryBot.create(:bob)
@jon.restrict(@sam)
@jon.restrict(@bob)
end

should "accept AR options" do
assert_equal 1, @jon.restricts(limit: 1).count
end
end

context 'blocking a follower' do
context 'in my following list' do
setup do
Expand Down Expand Up @@ -166,6 +178,59 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
end
end

context 'restricting a follower' do
context 'in my following list' do
setup do
@jon.restrict(@sam)
end

should 'remove him from followers' do
assert_equal 0, @jon.followers_count
end

should 'add him to the blocked followers' do
assert_equal 1, @jon.restricted_followers_count
end

should 'not be able to follow again' do
@jon.follow(@sam)
assert_equal 0, @jon.followers_count
end

should 'not be present when listing followers' do
assert_equal [], @jon.followers
end

should "be in the list of blocks" do
assert_equal [@sam], @jon.restricts
end
end

context 'not in my following list' do
setup do
@sam.restrict(@jon)
end

should 'add him to the restricted followers' do
assert_equal 1, @sam.restricted_followers_count
end

should 'not be able to follow again' do
@sam.follow(@jon)
assert_equal 0, @sam.followers_count
end

should 'not be present when listing followers' do
assert_equal [], @sam.followers
end

should "be in the list of restricts" do
assert_equal [@jon], @sam.restricts
end
end
end


context 'unblocking a blocked follow' do
setup do
@jon.block(@sam)
Expand All @@ -182,6 +247,22 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
end
end

context 'unrestricting a restricted follow' do
setup do
@jon.restrict(@sam)
@jon.unrestrict(@sam)
end

should 'not include the unblocked user in the list of followers' do
assert_equal [], @jon.followers
end

should 'remove him from the blocked followers' do
assert_equal 0, @jon.restricted_followers_count
# assert_equal [], @jon.blocks
end
end

context 'unblock a non-existent follow' do
setup do
@sam.stop_following(@jon)
Expand All @@ -195,10 +276,21 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
should 'not be in the blocked followers count' do
assert_equal 0, @jon.blocked_followers_count
end
end

# should "not be in the blocks list" do
# assert_equal [], @jon.blocks
# end
context 'unrestrict a non-existent follow' do
setup do
@sam.stop_following(@jon)
@jon.unrestrict(@sam)
end

should 'not be in the list of followers' do
assert_equal [], @jon.followers
end

should 'not be in the blocked followers count' do
assert_equal 0, @jon.restricted_followers_count
end
end

context 'followers_by_type' do
Expand Down
8 changes: 4 additions & 4 deletions test/acts_as_follower_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
assert_equal 2, @sam.following_by_type_count('Band')
assert_equal 1, @sam.following_by_type_count('User')
assert_equal 0, @jon.following_by_type_count('Band')
@jon.block(@sam)
@jon.restrict(@sam)
assert_equal 0, @sam.following_by_type_count('User')
end
end
Expand Down Expand Up @@ -202,9 +202,9 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
should_change('@sam.follow_count', by: -1) { @sam.follow_count }
end

context 'blocked by followable' do
context 'restricted by followable' do
setup do
@jon.block(@sam)
@jon.restrict(@sam)
@user_follow = FactoryBot.create(:user)
end

Expand All @@ -216,7 +216,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase
assert_equal 1, @sam.follow_count
end

should 'not return record of the blocked follows' do
should 'not return record of the restricted follows' do
assert_equal 1, @sam.all_follows.size
assert !@sam.all_follows.include?(@user_follow)
assert !@sam.all_following.include?(@jon)
Expand Down

0 comments on commit fa95b7b

Please sign in to comment.