Skip to content

Commit

Permalink
Merge pull request #3 from aredotna/add-restrict
Browse files Browse the repository at this point in the history
Alias block with restrict
  • Loading branch information
broskoski authored Apr 5, 2022
2 parents a2fec35 + 0f74e1d commit 9f70d78
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 8 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
15 changes: 15 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,19 +78,33 @@ 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(&: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)
followings.unblocked.for_follower(follower).first.present?
end

# Returns true if the current instance is blocked by the passed record
# Returns false if the current instance is not blocked by the passed record or no follow is found
def restricted_by?(follower)
followings.blocked.for_follower(follower).first.present?
end

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
2 changes: 1 addition & 1 deletion lib/acts_as_follower/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ActsAsFollower
VERSION = '0.2.1'
VERSION = '0.2.2'
end
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
109 changes: 106 additions & 3 deletions test/acts_as_followable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
assert @sam.respond_to?(:followers_count)
assert @sam.respond_to?(:followers)
assert @sam.respond_to?(:followed_by?)
assert @sam.respond_to?(:restricted_by?)
end
end

Expand Down Expand Up @@ -78,6 +79,17 @@ class ActsAsFollowableTest < ActiveSupport::TestCase
end
end

context 'restricted_by' do
setup do
@jon.restrict(@sam)
end

should 'return_restricted_status' do
assert_equal true, @jon.restricted_by?(@sam)
assert_equal false, @sam.restricted_by?(@jon)
end
end

context 'destroying a followable' do
setup do
@jon.destroy
Expand Down Expand Up @@ -114,6 +126,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 +190,58 @@ 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 +258,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 +287,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 9f70d78

Please sign in to comment.