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 support for a block argument on Relation#count #317

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
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
Loading