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.



227
228
229
230
231
# File 'lib/activerecord/summarize.rb', line 227

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

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



225
226
227
# File 'lib/activerecord/summarize.rb', line 225

def column
  @column
end

#methodObject (readonly)

Returns the value of attribute method.



225
226
227
# File 'lib/activerecord/summarize.rb', line 225

def method
  @method
end

#relationObject (readonly)

Returns the value of attribute relation.



225
226
227
# File 'lib/activerecord/summarize.rb', line 225

def relation
  @relation
end

Instance Method Details

#functionObject



248
249
250
251
252
253
254
# File 'lib/activerecord/summarize.rb', line 248

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



233
234
235
236
237
238
# File 'lib/activerecord/summarize.rb', line 233

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

#unmatch_valueObject



240
241
242
243
244
245
246
# File 'lib/activerecord/summarize.rb', line 240

def unmatch_value
  case method
  when "sum" then 0
  when "count" then nil
  else raise "Unknown calculation method"
  end
end