From 049d51524e0cc28b8af6bf0fbdfebf2ea5d3e9bd Mon Sep 17 00:00:00 2001 From: David Pickart Date: Tue, 31 Mar 2020 11:28:28 -0500 Subject: [PATCH 1/2] Expose `where` query on model base --- README.md | 3 +++ lib/contentful_model/queries.rb | 2 ++ spec/chainable_queries_spec.rb | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/README.md b/README.md index df17826..3614a3e 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,9 @@ Foo.find_by(someField: [searchquery1, searchquery2], someOtherField: "bar").load You'll see from the example above that it accepts an array of search terms which will invoke an 'in' query. +### `where([hash])` +An alias for `find_by`. + ### `params({object})` Includes the specified parameters in the Contentful API call. diff --git a/lib/contentful_model/queries.rb b/lib/contentful_model/queries.rb index f0a8eaf..ecc746f 100644 --- a/lib/contentful_model/queries.rb +++ b/lib/contentful_model/queries.rb @@ -75,6 +75,8 @@ def find_by(find_query = {}) query.find_by(find_query) end + alias where find_by + def search(parameters) query.search(parameters) end diff --git a/spec/chainable_queries_spec.rb b/spec/chainable_queries_spec.rb index 1685643..5b43bb8 100644 --- a/spec/chainable_queries_spec.rb +++ b/spec/chainable_queries_spec.rb @@ -145,6 +145,48 @@ def invalid? end end + describe '::where' do + it 'when value is an array' do + query = subject.where(foo: [1, 2, 3]) + expect(query.parameters).to include('fields.foo[in]' => '1,2,3') + end + + it 'when value is a string' do + query = subject.where(foo: 'bar') + expect(query.parameters).to include('fields.foo' => 'bar') + end + + it 'when value is a number' do + query = subject.where(foo: 1) + expect(query.parameters).to include('fields.foo' => 1) + end + + it 'when value is a boolean' do + query = subject.where(foo: true) + expect(query.parameters).to include('fields.foo' => true) + end + + it 'when value is a hash' do + query = subject.where(foo: {gte: 123}) + expect(query.parameters).to include('fields.foo[gte]' => 123) + end + + it 'when multiple fields' do + query = subject.where(foo: true, bar: 123) + expect(query.parameters).to include('fields.foo' => true , 'fields.bar' => 123) + end + + it 'supports sys fields' do + query = subject.where('sys.id': 'foo') + expect(query.parameters).to include('sys.id' => 'foo') + end + + it 'support sys properties without prefix' do + query = subject.where('updatedAt' => 'foo') + expect(query.parameters).to include('sys.updatedAt' => 'foo') + end + end + describe '::paginate' do it 'defaults to first page and 100 items and sort by updatedAt' do query = subject.paginate From be0db000359878c7750499ab5a7a338147203a5c Mon Sep 17 00:00:00 2001 From: David Pickart Date: Tue, 31 Mar 2020 11:59:18 -0500 Subject: [PATCH 2/2] Call where directly --- lib/contentful_model/queries.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/contentful_model/queries.rb b/lib/contentful_model/queries.rb index ecc746f..45818af 100644 --- a/lib/contentful_model/queries.rb +++ b/lib/contentful_model/queries.rb @@ -75,7 +75,9 @@ def find_by(find_query = {}) query.find_by(find_query) end - alias where find_by + def where(find_query = {}) + query.where(find_query) + end def search(parameters) query.search(parameters)