Class: Pluckr::Schema::Aggregate
- Includes:
- Conditions
- Defined in:
- lib/pluckr/schema/aggregate.rb
Overview
Two flavours share this node:
count :videos # correlated: association is set
count :users, from: User # independent: model is set
Both compile to a scalar subquery in the SELECT list, never to a JOIN + GROUP BY, so root rows are never multiplied.
Constant Summary collapse
- OPERATIONS =
%i[count sum avg min max exists].freeze
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#where ⇒ Object
readonly
Returns the value of attribute where.
Attributes inherited from Node
Instance Method Summary collapse
- #correlated? ⇒ Boolean
-
#independent? ⇒ Boolean
Independent aggregates are not rooted in the source record.
-
#initialize(output:, operation:, association: nil, model: nil, relation: nil, column: nil, where: nil, scope: nil) ⇒ Aggregate
constructor
A new instance of Aggregate.
-
#relation_scoped? ⇒ Boolean
True when the subquery must be built from an ActiveRecord relation rather than straight Arel.
-
#target_model ⇒ Object
Target model for column validation / table resolution.
Methods included from Conditions
#dynamic_where?, #resolved_where
Methods inherited from Node
Constructor Details
#initialize(output:, operation:, association: nil, model: nil, relation: nil, column: nil, where: nil, scope: nil) ⇒ Aggregate
Returns a new instance of Aggregate.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pluckr/schema/aggregate.rb', line 19 def initialize(output:, operation:, association: nil, model: nil, relation: nil, column: nil, where: nil, scope: nil) super(output: output) @operation = operation.to_sym unless OPERATIONS.include?(@operation) raise ConfigurationError, "unsupported aggregate operation `#{operation}`" end @association = association @model = model @relation = relation @column = column&.to_sym @where = where.respond_to?(:call) ? where : where&.dup&.freeze @scope = scope freeze end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
17 18 19 |
# File 'lib/pluckr/schema/aggregate.rb', line 17 def association @association end |
#column ⇒ Object (readonly)
Returns the value of attribute column.
17 18 19 |
# File 'lib/pluckr/schema/aggregate.rb', line 17 def column @column end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
17 18 19 |
# File 'lib/pluckr/schema/aggregate.rb', line 17 def model @model end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
17 18 19 |
# File 'lib/pluckr/schema/aggregate.rb', line 17 def operation @operation end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
17 18 19 |
# File 'lib/pluckr/schema/aggregate.rb', line 17 def relation @relation end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
17 18 19 |
# File 'lib/pluckr/schema/aggregate.rb', line 17 def scope @scope end |
#where ⇒ Object (readonly)
Returns the value of attribute where.
17 18 19 |
# File 'lib/pluckr/schema/aggregate.rb', line 17 def where @where end |
Instance Method Details
#correlated? ⇒ Boolean
41 42 43 |
# File 'lib/pluckr/schema/aggregate.rb', line 41 def !association.nil? end |
#independent? ⇒ Boolean
Independent aggregates are not rooted in the source record.
37 38 39 |
# File 'lib/pluckr/schema/aggregate.rb', line 37 def independent? !model.nil? || !relation.nil? end |
#relation_scoped? ⇒ Boolean
True when the subquery must be built from an ActiveRecord relation rather than straight Arel.
47 48 49 |
# File 'lib/pluckr/schema/aggregate.rb', line 47 def relation_scoped? !scope.nil? || !where.nil? end |
#target_model ⇒ Object
Target model for column validation / table resolution.
53 54 55 |
# File 'lib/pluckr/schema/aggregate.rb', line 53 def target_model model || relation&.klass || association.klass end |