From f61f85ff6e201dc7781294277498af966f36f098 Mon Sep 17 00:00:00 2001 From: Rachel Green Date: Thu, 19 Oct 2017 21:00:27 -0500 Subject: [PATCH 1/5] Adds functionality to pluck info for followers of an object. --- lib/acts_as_follower/followable.rb | 6 ++++++ test/acts_as_followable_test.rb | 21 +++++++++++++++++++++ test/factories/users.rb | 10 ++++++++++ test/schema.rb | 3 ++- 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/acts_as_follower/followable.rb b/lib/acts_as_follower/followable.rb index dda4c52..ba8b8eb 100644 --- a/lib/acts_as_follower/followable.rb +++ b/lib/acts_as_follower/followable.rb @@ -98,6 +98,12 @@ def get_follow_for(follower) self.followings.for_follower(follower).first end + def pluck_followers(*columns) + follower_ids = self.followings.pluck(:follower_id) + follower_ids.collect {|f| User.where(id: f).pluck(*columns)}.flatten(1) + end + + private def block_future_follow(follower) diff --git a/test/acts_as_followable_test.rb b/test/acts_as_followable_test.rb index 5f95412..e03b0fd 100644 --- a/test/acts_as_followable_test.rb +++ b/test/acts_as_followable_test.rb @@ -279,5 +279,26 @@ class ActsAsFollowableTest < ActiveSupport::TestCase end end + context "returns follower info via pluck" do + setup do + @addy = FactoryGirl.create(:addy) + @claire = FactoryGirl.create(:claire) + @oasis = FactoryGirl.create(:oasis) + @addy.follow(@oasis) + @claire.follow(@oasis) + end + + should "return values when plucking single attribute of followers" do + expected_value = ["addy@example.com", "claire@example.com"] + assert_equal expected_value, @oasis.pluck_followers(:email) + end + + should "return values when plucking multiple attributes of followers" do + expected_value = [["Addison", "addy@example.com"], + ["Claire", "claire@example.com"]] + assert_equal expected_value, @oasis.pluck_followers(:name, :email) + end + end + end end diff --git a/test/factories/users.rb b/test/factories/users.rb index ff43e73..a2c2773 100644 --- a/test/factories/users.rb +++ b/test/factories/users.rb @@ -10,4 +10,14 @@ factory :bob, class: User do |u| u.name 'Bob' end + + factory :addy, class: User do |u| + u.name 'Addison' + u.email 'addy@example.com' + end + + factory :claire, class: User do |u| + u.name 'Claire' + u.email 'claire@example.com' + end end diff --git a/test/schema.rb b/test/schema.rb index b3eb50c..525f3ad 100644 --- a/test/schema.rb +++ b/test/schema.rb @@ -1,4 +1,4 @@ -ActiveRecord::Schema.define version: 0 do +ActiveRecord::Schema.define version: 1 do create_table :follows, force: true do |t| t.integer "followable_id", null: false @@ -12,6 +12,7 @@ create_table :users, force: true do |t| t.column :name, :string + t.column :email, :string end create_table :bands, force: true do |t| From 1b3dbd9e80592aec684121b2f115083043415609 Mon Sep 17 00:00:00 2001 From: Rachel Green Date: Thu, 19 Oct 2017 21:06:41 -0500 Subject: [PATCH 2/5] Updates README with info for pluck_followers. --- README.rdoc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.rdoc b/README.rdoc index 35e0272..560f717 100644 --- a/README.rdoc +++ b/README.rdoc @@ -181,6 +181,14 @@ The following methods take an optional hash parameter of ActiveRecord options (: followers_by_type, followers, blocks --- +To simulate the Activerecord `pluck` method for followers + book.pluck_followers(:name) + +This method, like the original `pluck` method, will also take multiple column names as parameters + book.pluck_followers(:name, :email) + +Note that this method will be applied to all followers, regardless of scope. + === Follow Model The Follow model has a set of named_scope's. In case you want to interface directly with the Follow model you can use them. From 00c6c32c41ebc8782008bd88703be6fd87701ddb Mon Sep 17 00:00:00 2001 From: Rachel Green Date: Sat, 19 May 2018 13:41:29 -0500 Subject: [PATCH 3/5] Uses flat_map rather than collect in pluck_followers method to improve performance. --- lib/acts_as_follower/followable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/acts_as_follower/followable.rb b/lib/acts_as_follower/followable.rb index ba8b8eb..5e60b58 100644 --- a/lib/acts_as_follower/followable.rb +++ b/lib/acts_as_follower/followable.rb @@ -100,7 +100,7 @@ def get_follow_for(follower) def pluck_followers(*columns) follower_ids = self.followings.pluck(:follower_id) - follower_ids.collect {|f| User.where(id: f).pluck(*columns)}.flatten(1) + follower_ids.flat_map { |f| User.where(id: f).pluck(*columns)} end From 2c68a3b6ccbe130609cb90a2586ecdc7c8e589fe Mon Sep 17 00:00:00 2001 From: Rachel Green Date: Sat, 19 May 2018 14:16:20 -0500 Subject: [PATCH 4/5] In CI, the tests fail due to a bug within Minitest concerning the reports plugin. This addition addresses that bug without changing the versions listed in the Gemfile or gemspec. --- test/test_helper.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_helper.rb b/test/test_helper.rb index c26c441..612b9f3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,3 +16,5 @@ require 'factory_girl' ActiveSupport::TestCase.extend(ShouldaCreate) FactoryGirl.find_definitions + +Minitest::Reporters.use! \ No newline at end of file From c37894d0194e3a86c39bece7f94db719ff8d69b3 Mon Sep 17 00:00:00 2001 From: Rachel Green Date: Sat, 19 May 2018 14:25:29 -0500 Subject: [PATCH 5/5] Removes earlier fix for Minitest bug. --- test/test_helper.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 612b9f3..b0ea33f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -15,6 +15,4 @@ require 'shoulda_create' require 'factory_girl' ActiveSupport::TestCase.extend(ShouldaCreate) -FactoryGirl.find_definitions - -Minitest::Reporters.use! \ No newline at end of file +FactoryGirl.find_definitions \ No newline at end of file