Class: Pluckr::Schema::Aggregate

Inherits:
Node
  • Object
show all
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

Attributes inherited from Node

#output

Instance Method Summary collapse

Methods included from Conditions

#dynamic_where?, #resolved_where

Methods inherited from Node

#scalar?

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

#associationObject (readonly)

Returns the value of attribute association.



17
18
19
# File 'lib/pluckr/schema/aggregate.rb', line 17

def association
  @association
end

#columnObject (readonly)

Returns the value of attribute column.



17
18
19
# File 'lib/pluckr/schema/aggregate.rb', line 17

def column
  @column
end

#modelObject (readonly)

Returns the value of attribute model.



17
18
19
# File 'lib/pluckr/schema/aggregate.rb', line 17

def model
  @model
end

#operationObject (readonly)

Returns the value of attribute operation.



17
18
19
# File 'lib/pluckr/schema/aggregate.rb', line 17

def operation
  @operation
end

#relationObject (readonly)

Returns the value of attribute relation.



17
18
19
# File 'lib/pluckr/schema/aggregate.rb', line 17

def relation
  @relation
end

#scopeObject (readonly)

Returns the value of attribute scope.



17
18
19
# File 'lib/pluckr/schema/aggregate.rb', line 17

def scope
  @scope
end

#whereObject (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

Returns:

  • (Boolean)


41
42
43
# File 'lib/pluckr/schema/aggregate.rb', line 41

def correlated?
  !association.nil?
end

#independent?Boolean

Independent aggregates are not rooted in the source record.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


47
48
49
# File 'lib/pluckr/schema/aggregate.rb', line 47

def relation_scoped?
  !scope.nil? || !where.nil?
end

#target_modelObject

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