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
36
37
38
39
40
41
42
43
44
45
46
47
48
|
module ActiveRelation
class Expression
include Sql::Quoting
attr_reader :attribute, :function_sql, :alias, :ancestor
delegate :relation, :to => :attribute
def initialize(attribute, function_sql, aliaz = nil, ancestor = nil)
@attribute, @function_sql, @alias, @ancestor = attribute, function_sql, aliaz, ancestor
end
module Transformations
def bind(new_relation)
new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self)
end
def as(aliaz)
Expression.new(attribute, function_sql, aliaz, self)
end
def to_attribute
Attribute.new(relation, @alias, nil, self)
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 and ancestor == other.ancestor and @alias == other.alias
end
alias_method :eql?, :==
def =~(other)
!(history & other.send(:history)).empty?
end
def hash
attribute.hash + function_sql.hash
end
def history
[self] + (ancestor ? [ancestor, ancestor.send(:history)].flatten : [])
end
end
end
|