Class: ActiveRecord::Summarize::CalculationResult
- Inherits:
-
Object
- Object
- ActiveRecord::Summarize::CalculationResult
- Defined in:
- lib/activerecord/summarize.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
- #function ⇒ Object
-
#initialize(relation, method, column) ⇒ CalculationResult
constructor
A new instance of CalculationResult.
- #select_value(base_relation) ⇒ Object
- #unmatch_value ⇒ Object
Constructor Details
#initialize(relation, method, column) ⇒ CalculationResult
Returns a new instance of CalculationResult.
231 232 233 234 235 |
# File 'lib/activerecord/summarize.rb', line 231 def initialize(relation, method, column) @relation = relation @method = method @column = column end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column.
229 230 231 |
# File 'lib/activerecord/summarize.rb', line 229 def column @column end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
229 230 231 |
# File 'lib/activerecord/summarize.rb', line 229 def method @method end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
229 230 231 |
# File 'lib/activerecord/summarize.rb', line 229 def relation @relation end |
Instance Method Details
#function ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/activerecord/summarize.rb', line 252 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
237 238 239 240 241 242 |
# File 'lib/activerecord/summarize.rb', line 237 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_value) unless where.empty? function.new([for_select]).tap { |f| f.distinct = relation.distinct_value } end |
#unmatch_value ⇒ Object
244 245 246 247 248 249 250 |
# File 'lib/activerecord/summarize.rb', line 244 def unmatch_value case method when "sum" then 0 when "count" then nil else raise "Unknown calculation method" end end |