Class: ActiveRecord::Summarize::CalculationResult

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/summarize.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, method, column) ⇒ CalculationResult

Returns a new instance of CalculationResult.



256
257
258
259
260
# File 'lib/activerecord/summarize.rb', line 256

def initialize(relation, method, column)
  @relation = relation
  @method = method
  @column = column
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



254
255
256
# File 'lib/activerecord/summarize.rb', line 254

def column
  @column
end

#methodObject (readonly)

Returns the value of attribute method.



254
255
256
# File 'lib/activerecord/summarize.rb', line 254

def method
  @method
end

#relationObject (readonly)

Returns the value of attribute relation.



254
255
256
# File 'lib/activerecord/summarize.rb', line 254

def relation
  @relation
end

Instance Method Details

#functionObject



277
278
279
280
281
282
283
# File 'lib/activerecord/summarize.rb', line 277

def function
  case method
  when "sum" then Arel::Nodes::Sum
  when "count" then Arel::Nodes::Count
  else raise "Unknown calculation method"
  end
end

#select_value(base_relation) ⇒ Object



262
263
264
265
266
267
# File 'lib/activerecord/summarize.rb', line 262

def select_value(base_relation)
  where = relation.where_clause - base_relation.where_clause
  for_select = column
  for_select = Arel::Nodes::Case.new(where.ast).when(true, for_select).else(unmatch_arel_node) unless where.empty?
  function.new([for_select]).tap { |f| f.distinct = relation.distinct_value }
end

#unmatch_arel_nodeObject



269
270
271
272
273
274
275
# File 'lib/activerecord/summarize.rb', line 269

def unmatch_arel_node
  case method
  when "sum" then 0 # Adding zero to a sum does nothing
  when "count" then nil # In SQL, null is no value and is not counted
  else raise "Unknown calculation method"
  end
end