aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/primitives/aggregation.rb
blob: 51ceee6e6694fe78f2b90fc3ac3789b1a1edfb2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module ActiveRelation
  class Aggregation
    include Sql::Quoting
    
    attr_reader :attribute, :function_sql, :alias
    delegate :relation, :to => :attribute
    
    def initialize(attribute, function_sql, aliaz = nil)
      @attribute, @function_sql, @alias = attribute, function_sql, aliaz
    end

    module Transformations
      def substitute(new_relation)
        Aggregation.new(attribute.substitute(new_relation), function_sql, @alias)
      end
    
      def as(aliaz)
        Aggregation.new(attribute, function_sql, aliaz)
      end
      
      def to_attribute
        Attribute.new(relation, @alias)
      end
    end
    include Transformations
    
    def to_sql(strategy = nil)
      "#{function_sql}(#{attribute.to_sql})" + (@alias ? " AS #{quote_column_name(@alias)}" : '')
    end
    
    def ==(other)
      self.class == other.class and attribute == other.attribute and function_sql == other.function_sql
    end
  end
end