diff options
Diffstat (limited to 'lib/active_relation/primitives/expression.rb')
-rw-r--r-- | lib/active_relation/primitives/expression.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/active_relation/primitives/expression.rb b/lib/active_relation/primitives/expression.rb new file mode 100644 index 0000000000..47658c49da --- /dev/null +++ b/lib/active_relation/primitives/expression.rb @@ -0,0 +1,50 @@ +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 substitute(new_relation) + Expression.new(attribute.substitute(new_relation), function_sql, @alias, self) + end + + def as(aliaz) + # key line -- note self + Expression.new(attribute, function_sql, aliaz, self) + end + + def to_attribute + # key line -- note self + 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 hash + attribute.hash + function_sql.hash + end + + def =~(other) + !(history & other.history).empty? + end + + def history + [self] + (ancestor ? [ancestor, ancestor.history].flatten : []) + end + end +end
\ No newline at end of file |