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

Add plucks functionality for followers of an object #102

Open
wants to merge 5 commits 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
8 changes: 8 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions lib/acts_as_follower/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.flat_map { |f| User.where(id: f).pluck(*columns)}
end


private

def block_future_follow(follower)
Expand Down
21 changes: 21 additions & 0 deletions test/acts_as_followable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions test/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion test/schema.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
require 'shoulda_create'
require 'factory_girl'
ActiveSupport::TestCase.extend(ShouldaCreate)
FactoryGirl.find_definitions
FactoryGirl.find_definitions