From 472feea1cfc98ec7a1878f866eba870dcc98ce8a Mon Sep 17 00:00:00 2001 From: ashleyHutton Date: Thu, 25 Jul 2024 09:41:29 -0500 Subject: [PATCH] Add support for a block argument on `Relation#count` --- lib/active_hash/relation.rb | 1 + spec/active_hash/relation_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/active_hash/relation.rb b/lib/active_hash/relation.rb index e24d1ac0..59d57b93 100644 --- a/lib/active_hash/relation.rb +++ b/lib/active_hash/relation.rb @@ -135,6 +135,7 @@ def find_by_id(id) end def count + return super if block_given? length end diff --git a/spec/active_hash/relation_spec.rb b/spec/active_hash/relation_spec.rb index e40c7d0f..bf892695 100644 --- a/spec/active_hash/relation_spec.rb +++ b/spec/active_hash/relation_spec.rb @@ -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)