Class: BabySqueel::Calculation

Inherits:
Object
  • Object
show all
Defined in:
lib/baby_squeel/calculation.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Calculation

Returns a new instance of Calculation.



5
6
7
# File 'lib/baby_squeel/calculation.rb', line 5

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



3
4
5
# File 'lib/baby_squeel/calculation.rb', line 3

def node
  @node
end

Instance Method Details

#to_sObject

In Active Record 5, we don’t need this class to make calculations work. They happily accept arel. However, when grouping with a calculation, there’s a really, really weird alias name. It calls #to_s on the Arel.

If this were not addressed, it would likely break query caching because the alias would have a unique name every time.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/baby_squeel/calculation.rb', line 23

def to_s
  names = node.map do |child|
    if child.kind_of?(String) || child.kind_of?(Symbol)
      child.to_s
    elsif child.respond_to?(:name)
      child.name.to_s
    end
  end

  names.compact.uniq.join('_')
end

#type_cast_from_database(value) ⇒ Object

This is only used in 4.2. We’re just pretending to be a database column to fake the casting here.



11
12
13
# File 'lib/baby_squeel/calculation.rb', line 11

def type_cast_from_database(value)
  value
end