Class: ActiveRecord::Refined::AST::GroupingSets

Inherits:
Node
  • Object
show all
Defined in:
lib/active_record/refined/ast.rb

Overview

GROUP BY GROUPING SETS / ROLLUP / CUBE: several groupings asked for at once, the totals of each coming back beside the rows. PostgreSQL has all three and the MySQL family rollup alone; the block raises for the rest before it gets this far.

Each set is a list of its own, so grouping_sets takes lists and rollup and cube take the columns themselves.

Constant Summary collapse

KINDS =
{
  grouping_sets: Arel::Nodes::GroupingSet,
  rollup: Arel::Nodes::RollUp,
  cube: Arel::Nodes::Cube,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#as, #asc, #desc

Constructor Details

#initialize(kind, sets) ⇒ GroupingSets

Returns a new instance of GroupingSets.

Raises:

  • (ArgumentError)


1205
1206
1207
1208
1209
# File 'lib/active_record/refined/ast.rb', line 1205

def initialize(kind, sets)
  raise ArgumentError, "#{kind} needs something to group by" if sets.empty?
  @kind = kind
  @sets = sets
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



1203
1204
1205
# File 'lib/active_record/refined/ast.rb', line 1203

def kind
  @kind
end

#setsObject (readonly)

Returns the value of attribute sets.



1203
1204
1205
# File 'lib/active_record/refined/ast.rb', line 1203

def sets
  @sets
end

Instance Method Details

#to_arel(table, model) ⇒ Object



1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'lib/active_record/refined/ast.rb', line 1211

def to_arel(table, model)
  return with_rollup(table, model) if AST.adapter_family(model) == :mysql

  KINDS.fetch(kind).new(
    if kind == :grouping_sets
      sets.map do |set|
        Arel::Nodes::GroupingElement.new(
          Array(set).map { |column| to_arel_operand(column, table, model) })
      end
    else
      sets.map { |column| to_arel_operand(column, table, model) }
    end)
end