Class: ParamsReady::Query::CustomPredicate

Inherits:
Parameter::AbstractParameter show all
Includes:
Predicate::DelegatingPredicate, Predicate::HavingChildren
Defined in:
lib/params_ready/query/custom_predicate.rb

Instance Attribute Summary

Attributes included from Predicate::DelegatingPredicate

#data

Attributes inherited from Parameter::AbstractParameter

#definition

Instance Method Summary collapse

Methods included from Predicate::HavingChildren

#context_for_predicates

Methods included from Predicate::DelegatingPredicate

included, #to_query_if_eligible

Methods inherited from Parameter::AbstractParameter

#==, #dup, #inspect, intent_for_children, #match?, #populate, #to_hash, #update_if_applicable, #update_in

Methods included from Extensions::Freezer

#freeze_variable, #freeze_variables, #variables_to_freeze

Methods included from Parameter::FromHash

#set_from_hash

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

#initialize(definition) ⇒ CustomPredicate

Returns a new instance of CustomPredicate.



11
12
13
14
# File 'lib/params_ready/query/custom_predicate.rb', line 11

def initialize(definition)
  super definition
  @data = definition.type.create
end

Instance Method Details

#eligible_for_query?(arel_table, context) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# 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) ⇒ Object

Raises:



41
42
43
44
45
# 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) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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