Skip to content

Commit

Permalink
Add support for a block argument on Relation#count
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyHutton committed Jul 25, 2024
1 parent a90148f commit 472feea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/active_hash/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def find_by_id(id)
end

def count
return super if block_given?
length
end

Expand Down
10 changes: 10 additions & 0 deletions spec/active_hash/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
end
end

describe '#count' do
it 'supports a block arg' do
expect(subject.count { |s| s.name == "US" }).to eq(1)
end

it 'returns the correct number of items of the relation' do
expect(subject.count).to eq(2)
end
end

describe '#size' do
it 'returns an Integer' do
expect(subject.size).to be_an(Integer)
Expand Down

0 comments on commit 472feea

Please sign in to comment.