Class: ParamsReady::Query::CustomPredicate
- Inherits:
-
Parameter::AbstractParameter
- Object
- Parameter::AbstractParameter
- ParamsReady::Query::CustomPredicate
- Defined in:
- lib/params_ready/query/custom_predicate.rb
Instance Attribute Summary
Attributes included from Predicate::DelegatingPredicate
Attributes inherited from Parameter::AbstractParameter
Instance Method Summary collapse
- #eligible_for_query?(arel_table, context) ⇒ Boolean
-
#initialize(definition) ⇒ CustomPredicate
constructor
A new instance of CustomPredicate.
- #test(record) ⇒ Object
- #to_query(arel_table, context: Restriction.blanket_permission) ⇒ Object
Methods included from Predicate::HavingChildren
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
Methods included from Extensions::Freezer::InstanceMethods
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
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
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
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.) 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 |