Skip to content
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions lib/contentful_model/queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def find_by(find_query = {})
query.find_by(find_query)
end

def where(find_query = {})
query.where(find_query)
end

def search(parameters)
query.search(parameters)
end
Expand Down
42 changes: 42 additions & 0 deletions spec/chainable_queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down