class ParamsReady::Query::CustomPredicate

Public Class Methods

new(definition) click to toggle source
# File lib/params_ready/query/custom_predicate.rb, line 11
def initialize(definition)
  super definition
  @data = definition.type.create
end

Public Instance Methods

eligible_for_query?(arel_table, context) click to toggle source
# File lib/params_ready/query/custom_predicate.rb, line 16
def eligible_for_query?(arel_table, context)
  return false unless context.permitted? self
  eligibility_test = definition.eligibility_test
  return true if eligibility_test.nil?

  instance_exec(arel_table, context, &eligibility_test)
end
test(record) click to toggle source
# File lib/params_ready/query/custom_predicate.rb, line 41
def test(record)
  test = definition.test
  raise ParamsReadyError, "Method 'test' unimplemented in '#{name}'" if test.nil?
  self.instance_exec(record, &test)
end
to_query(arel_table, context: Restriction.blanket_permission) click to toggle source
# File lib/params_ready/query/custom_predicate.rb, line 24
def to_query(arel_table, context: Restriction.blanket_permission)
  return unless eligible_for_query?(arel_table, context)

  to_query = definition.to_query
  raise ParamsReadyError, "Method 'to_query' unimplemented in '#{name}'" if to_query.nil?
  result = instance_exec(arel_table, context, &to_query)

  case result
  when Arel::Nodes::Node, nil
    result
  else
    literal = Arel::Nodes::SqlLiteral.new(result)
    grouping = Arel::Nodes::Grouping.new(literal)
    grouping
  end
end