Sha256: 9761ba73c6d9266603a50c1bd839c441325201a050ef020c1f9e590efd5cf82d
Contents?: true
Size: 1.14 KB
Versions: 1
Compression:
Stored size: 1.14 KB
Contents
module ActiveRecord module QueryMethods # Store chain for jsonb columns. class JsonbChain < KeyStoreChain # 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 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 "#{@store_name}#{op}", path => val end private def flatten_hash(hash) case hash when Hash hash.flat_map { |k, v| [k, *flatten_hash(v)] } when Array [hash] else hash end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pgrel-0.1.2 | lib/pgrel/active_record/store_chain/jsonb_chain.rb |