diff --git a/lib/acts_as_follower/follow_scopes.rb b/lib/acts_as_follower/follow_scopes.rb index dac469b..fb1695f 100644 --- a/lib/acts_as_follower/follow_scopes.rb +++ b/lib/acts_as_follower/follow_scopes.rb @@ -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 diff --git a/lib/acts_as_follower/followable.rb b/lib/acts_as_follower/followable.rb index 85e245e..5bbfd19 100644 --- a/lib/acts_as_follower/followable.rb +++ b/lib/acts_as_follower/followable.rb @@ -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 @@ -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 diff --git a/lib/acts_as_follower/version.rb b/lib/acts_as_follower/version.rb index af580be..ad3b945 100644 --- a/lib/acts_as_follower/version.rb +++ b/lib/acts_as_follower/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ActsAsFollower - VERSION = '0.2.1' + VERSION = '0.2.2' end diff --git a/lib/generators/templates/model.rb b/lib/generators/templates/model.rb index 1984a3f..cdfd953 100644 --- a/lib/generators/templates/model.rb +++ b/lib/generators/templates/model.rb @@ -11,4 +11,5 @@ class Follow < ActiveRecord::Base def block! update_attribute(:blocked, true) end + alias restrict! block! end diff --git a/test/acts_as_followable_test.rb b/test/acts_as_followable_test.rb index 94bfc68..93c55b0 100644 --- a/test/acts_as_followable_test.rb +++ b/test/acts_as_followable_test.rb @@ -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 @@ -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 @@ -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 @@ -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) @@ -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) @@ -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 diff --git a/test/acts_as_follower_test.rb b/test/acts_as_follower_test.rb index daa798c..6364579 100644 --- a/test/acts_as_follower_test.rb +++ b/test/acts_as_follower_test.rb @@ -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 @@ -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 @@ -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)