Class: ParamsReady::Query::ExistsPredicate

Inherits:
StructuredGrouping show all
Includes:
Predicate::HavingAssociations
Defined in:
lib/params_ready/query/exists_predicate.rb

Constant Summary

Constants inherited from Parameter::AbstractStructParameter

Parameter::AbstractStructParameter::EMPTY_HASH

Instance Attribute Summary

Attributes inherited from Parameter::AbstractParameter

#definition

Instance Method Summary collapse

Methods included from Predicate::HavingAssociations

#dig

Methods inherited from StructuredGrouping

#context_for_predicates, #operator, #predicates

Methods included from Parameter::GroupingLike

#eligible_for_query?, #predicate_group, #to_query_if_eligible

Methods included from Marshaller::ParameterModule

#marshal

Methods inherited from Parameter::AbstractStructParameter

#[], #[]=, #find_in_hash, #for_frontend, #for_model, #for_output, #wrap_output

Methods included from Parameter::ComplexParameter

#update_child

Methods inherited from Parameter::Parameter

#allows_undefined?, #definite_default?, #eligible_for_output?, #find_in_hash, #format, #format_self_permitted, #freeze, #hash, #hash_key, #initialize, #inspect_content, #is_default?, #is_definite?, #is_nil?, #is_undefined?, #memo, #memo!, #nil_default?, #populate_other, #set_from_input, #set_value, #to_hash_if_eligible, #unwrap, #unwrap_or, #wrap_output

Methods inherited from Parameter::AbstractParameter

#==, #dup, #initialize, #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

This class inherits a constructor from ParamsReady::Parameter::Parameter

Instance Method Details



29
30
31
32
33
34
# File 'lib/params_ready/query/exists_predicate.rb', line 29

def related(query_table, subquery_table, context)
  return nil if definition.related.nil?

  grouping = definition.related.to_arel(query_table, subquery_table, context, self)
  subquery_table.grouping(grouping)
end

#test(record) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/params_ready/query/exists_predicate.rb', line 36

def test(record)
  return nil unless is_definite?

  collection = dig(record, definition.path_to_collection)

  result = if collection.nil?
    false
  else
    collection.any? do |item|
      super item
    end
  end

  if definition.has_child?(:existence) && self[:existence].unwrap == :none
    !result
  else
    result
  end
end

#to_query(query_table, context: Restriction.blanket_permission) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/params_ready/query/exists_predicate.rb', line 10

def to_query(query_table, context: Restriction.blanket_permission)
  query_table = definition.outer_table || query_table

  subquery_table = self.definition.arel_table
  raise ParamsReadyError, "Arel table for '#{name}' not set" if subquery_table.nil?

  predicates = predicate_group(subquery_table, context: context)

  join_clause = self.related query_table, subquery_table, context
  subquery = GroupingOperator.instance(:and).connect(predicates, join_clause)
  select = subquery_table.where(subquery)
  query = select.take(1).project(Arel.star).exists
  if definition.has_child?(:existence) && self[:existence].unwrap == :none
    query.not
  else
    query
  end
end