Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
### Added

### Changed
- Exposed `.find_by` as a public method for more complex querying of models, and to allow people to write
their own custom filters on top of this method

### Fixed

Expand Down
28 changes: 13 additions & 15 deletions lib/testing_record/dsl/builder/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ def exists?(attributes)
find_by(attributes).any?
end

# Finds all entities that match specified attribute values
#
# @return [Array<TestingRecord::Model>]
def find_by(attributes)
pool = all
attributes.each do |key, value|
TestingRecord.logger.debug("Current user pool size: #{pool.length}")
TestingRecord.logger.debug("Filtering User list by #{key}: #{value}")
pool = pool.select { |entity| entity.attributes[key] == value }
end
pool
end

# Finds an entity with the provided email address
# If one is found, set it as the current entity
#
Expand Down Expand Up @@ -50,21 +63,6 @@ def with_primary_key?(primary_key)
def with_primary_key(primary_key)
find_by({ __primary_key => primary_key })&.first&.tap { |entity| entity.class.current = entity }
end

private

# Finds all entities that match specified attribute values
#
# @return [Array<TestingRecord::Model>]
def find_by(attributes)
pool = all
attributes.each do |key, value|
TestingRecord.logger.debug("Current user pool size: #{pool.length}")
TestingRecord.logger.debug("Filtering User list by #{key}: #{value}")
pool = pool.select { |entity| entity.attributes[key] == value }
end
pool
end
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/testing_record/dsl/builder/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@
end
end

describe '.find_by' do
let(:foo_entity) { model_klazz.create({ email_address: 'foo@foo.com', foo: 3, other: :foo }) }
let(:bar_entity) { model_klazz.create({ email_address: 'bar@bar.com', foo: 3, other: :bar }) }
let(:baz_entity) { model_klazz.create({ email_address: 'baz@baz.com', foo: 3, other: :baz }) }

before do
foo_entity
bar_entity
baz_entity
end

context 'with a simple 1 attribute query' do
it 'returns a collection of entities that match the query' do
expect(model_klazz.find_by({ foo: 3 })).to eq([foo_entity, bar_entity, baz_entity])
end

it 'returns a blank collection when no entities match the query' do
expect(model_klazz.find_by({ foo: 4 })).to eq([])
end
end

context 'with a more complex set of attributes as a query' do
it 'returns a collection of entities that match all query attributes' do
expect(model_klazz.find_by({ foo: 3, other: :foo, email_address: 'foo@foo.com' })).to eq([foo_entity])
end

it 'returns a blank collection when no entities match all query attributes' do
expect(model_klazz.find_by({ foo: 3, other: :jeff, email_address: 'foo@foo.com' })).to eq([])
end
end
end

describe '.with_id' do
before { model_klazz.primary_key :id }
after { model_klazz.primary_key :email_address }
Expand Down