Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ test/version_tmp
tmp
vendor
.byebug_history
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Country.european.republics.part_of_nato.order(id: :desc)
### Supported calculation methods

- count
- pick
- pluck
- ids
- minimum
Expand Down
2 changes: 1 addition & 1 deletion lib/frozen_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def current_scope=(scope)
end

delegate :each, :find_each, :where, :first, :first!, :last, :last!,
:pluck, :ids, :order, :limit, :offset, :minimum, :maximum, :average, :sum, :count,
:pluck, :pick, :ids, :order, :limit, :offset, :minimum, :maximum, :average, :sum, :count,
to: :current_scope

def file_path
Expand Down
6 changes: 6 additions & 0 deletions lib/frozen_record/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def pluck(*attributes)
end
end

def pick(*attributes)
raise NotImplementedError, '`.pick` without arguments is not supported yet' if attributes.empty?

Comment thread
RDIL marked this conversation as resolved.
Outdated
limit(1).pluck(*attributes).first
end

def ids
pluck(primary_key)
end
Expand Down
78 changes: 78 additions & 0 deletions spec/scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,84 @@

end

describe '.pick' do

context 'when called with a single argument' do

it 'returns the value of the first record' do
name = Country.pick(:name)
expect(name).to be == 'Canada'
end

end

context 'when called with multiple arguments' do

it 'returns an array of the values of the first record' do
attributes = Country.pick(:id, :name)
expect(attributes).to be == [1, 'Canada']
end

end

context 'when called without arguments' do

it 'raises a NotImplementedError' do
expect { Country.pick }.to raise_error(NotImplementedError)
end

end

context 'when called on a scope' do

it 'returns the attribute of the first matching record' do
name = Country.where(continent: 'Europe').pick(:name)
expect(name).to be == 'France'
end

it 'honors the ordering' do
name = Country.order(name: :desc).pick(:name)
expect(name).to be == 'France'
end

it 'honors the offset' do
name = Country.offset(1).pick(:name)
expect(name).to be == 'France'
end

it 'does not alter the receiver scope' do
scope = Country.where(nato: true)
scope.pick(:name)
expect(scope.length).to be == 2
end

end

context 'when no record matches' do

it 'returns nil when called with a single argument' do
name = Country.where(name: 'not existing').pick(:name)
expect(name).to be_nil
end

it 'returns nil when called with multiple arguments' do
attributes = Country.where(name: 'not existing').pick(:id, :name)
expect(attributes).to be_nil
end

end

context 'when passed an argument that is not an attribute' do

it 'returns the result of calling the given method name' do
reverse_name = Country.pick(:reverse_name)
expect(reverse_name).to be == 'adanaC'
end

end

end

describe '.ids' do

context 'when called with no arguments' do
Expand Down