class Kms::EntryDrop::Scope

overriding methods cause we work with 'json' column

Public Instance Methods

find_all_by(_, fields={}) click to toggle source
# File lib/drops/kms/entry_drop.rb, line 17
def find_all_by(_, fields={})
  fields, = Liquor::Drop.unwrap_scope_arguments([ fields ])

  plain_fields, json_fields = fields_partition(fields)
  result = @source.where(fields_query(fields), *(json_fields.values + plain_fields.values).map(&:to_s))
  Liquor::DropDelegation.wrap_scope(result)
end
find_by(_, fields={}) click to toggle source
# File lib/drops/kms/entry_drop.rb, line 9
def find_by(_, fields={})
  fields, = Liquor::Drop.unwrap_scope_arguments([ fields ])

  plain_fields, json_fields = fields_partition(fields)
  result = @source.where(fields_query(fields), *(json_fields.values + plain_fields.values).map(&:to_s)).first
  Liquor::DropDelegation.wrap_element result if result
end
order(*args) click to toggle source
# File lib/drops/kms/entry_drop.rb, line 25
def order(*args)
  args = Liquor::Drop.unwrap_scope_arguments(args)
  parsed_args = args.map do |arg|
    order_clause = arg.split(' ')
    if order_clause[0].in? Kms::Entry.column_names - ['values']
      arg
    else
      ["values ->> '#{order_clause[0]}'", order_clause[1].to_s].join(' ')
    end
  end
  # we use reorder because by default we order by position
  Liquor::DropDelegation.wrap_scope @source.reorder(*parsed_args)
end

Private Instance Methods

fields_partition(fields) click to toggle source
# File lib/drops/kms/entry_drop.rb, line 41
def fields_partition(fields)
  fields.partition {|name, _| (Kms::Entry.column_names - ['values']).include? name.to_s}.map(&:to_h)
end
fields_query(fields) click to toggle source
# File lib/drops/kms/entry_drop.rb, line 45
def fields_query(fields)
  plain_fields, json_fields = fields_partition(fields)
  json_fields_query = json_fields.map {|name, _| "values ->> '#{name}' = ?" }.join(" AND ")
  plain_fields_query = plain_fields.map {|name, _| "#{name} = ?"}.join(" AND ")
  [json_fields_query, plain_fields_query].reject(&:empty?).join(' OR ')
end