Module: ParamsReady::Parameter::GroupingLike

Included in:
Query::ArrayGrouping, Query::StructuredGrouping
Defined in:
lib/params_ready/query/grouping.rb

Instance Method Summary collapse

Instance Method Details

#eligible_for_query?(table, context) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/params_ready/query/grouping.rb', line 74

def eligible_for_query?(table, context)
  return false unless context.permitted? self
  return false unless is_definite?
  return true unless respond_to?(:to_query?)

  to_query?(table, context)
end

#predicate_group(arel_table, context: Restriction.blanket_permission) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/params_ready/query/grouping.rb', line 60

def predicate_group(arel_table, context: Restriction.blanket_permission)
  subqueries = predicates.reduce(nil) do |acc, predicate|
    query = predicate.to_query_if_eligible(arel_table, context: context)
    # This duplicates the operator logic
    # but we want operator to be optional
    # for single predicate groupings
    next query if acc.nil?

    operator.connect(acc, query)
  end
  return nil if subqueries.nil?
  arel_table.grouping(subqueries)
end

#test(record) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/params_ready/query/grouping.rb', line 93

def test(record)
  return nil unless is_definite?

  predicates = self.predicates
  return nil if predicates.empty?

  operator.test(record, predicates)
end

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



89
90
91
# File 'lib/params_ready/query/grouping.rb', line 89

def to_query(arel_table, context: Restriction.blanket_permission)
  self.predicate_group(arel_table, context: context)
end

#to_query_if_eligible(arel_table, context:) ⇒ Object



82
83
84
85
86
87
# File 'lib/params_ready/query/grouping.rb', line 82

def to_query_if_eligible(arel_table, context:)
  return nil unless eligible_for_query?(arel_table, context)

  context = context_for_predicates(context)
  to_query(arel_table, context: context)
end