Class: ActiveRecord::QueryMethods::JsonbChain
- Inherits:
-
KeyStoreChain
- Object
- StoreChain
- KeyStoreChain
- ActiveRecord::QueryMethods::JsonbChain
- Defined in:
- lib/pgrel/active_record/store_chain/jsonb_chain.rb
Overview
Store chain for jsonb columns.
Constant Summary collapse
- OPERATORS =
{contains: "@>", overlap: "&&"}.freeze
Instance Attribute Summary
Attributes inherited from StoreChain
#quoted_store_name, #store_name
Instance Method Summary collapse
-
#contains_values(*values) ⇒ Object
Contains values.
-
#overlap_values(*values) ⇒ Object
Overlap values.
-
#path(*args) ⇒ Object
Query by value in path.
Methods inherited from KeyStoreChain
Methods inherited from StoreChain
#contained, #contains, #initialize, #not, #where
Constructor Details
This class inherits a constructor from ActiveRecord::QueryMethods::StoreChain
Instance Method Details
#contains_values(*values) ⇒ Object
Contains values
Example
Model.create!(name: 'first', store: {a: 1, b: 2})
Model.create!(name: 'second', store: {b: 1, c: 3})
Model.store(:store).contains_values(1, 2).all #=> [Model(name: 'first', ...)]
57 58 59 |
# File 'lib/pgrel/active_record/store_chain/jsonb_chain.rb', line 57 def contains_values(*values) update_scope(value_query(:contains), cast_values(values)) end |
#overlap_values(*values) ⇒ Object
Overlap values
Example
Model.create!(name: 'first', store: {a: 1, b: 2})
Model.create!(name: 'second', store: {b: 1, c: 3})
Model.store(:store).overlap_values(1, 2).all
#=>[Model(name: 'first', ...), Model(name: 'second')]
46 47 48 |
# File 'lib/pgrel/active_record/store_chain/jsonb_chain.rb', line 46 def overlap_values(*values) update_scope(value_query(:overlap), cast_values(values)) end |
#path(*args) ⇒ Object
Query by value in path.
Example:
Model.create!(name: 'first', store: {b: 1, c: { d: 3 } })
Model.create!(name: 'second', store: {b: 2, c: { d: 1 }})
Model.store(:store).path(c: {d: 3}).all #=> [Model(name: 'first', ...)]
Model.store(:store).path('c', 'd', [1, 3]).size #=> 2
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pgrel/active_record/store_chain/jsonb_chain.rb', line 17 def path(*args) args = flatten_hash(args.first) if args.size == 1 val = args.pop path = "{#{args.join(",")}}" case val when Hash op = "#>" val = ::ActiveSupport::JSON.encode(val) when Array op = "#>>" val = val.map(&:to_s) else op = "#>>" val = val.to_s end where_with_prefix "#{quoted_store_name}#{op}", path => val end |