diff options
-rw-r--r-- | lib/arel/nodes/extract.rb | 13 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 9 | ||||
-rw-r--r-- | test/nodes/test_extract.rb | 8 |
3 files changed, 12 insertions, 18 deletions
diff --git a/lib/arel/nodes/extract.rb b/lib/arel/nodes/extract.rb index 64f5c3ff0f..7c69deadef 100644 --- a/lib/arel/nodes/extract.rb +++ b/lib/arel/nodes/extract.rb @@ -1,20 +1,14 @@ module Arel module Nodes class Extract < Arel::Nodes::Unary + include Arel::AliasPredication include Arel::Predications attr_accessor :field - attr_accessor :alias - def initialize expr, field, aliaz = nil + def initialize expr, field super(expr) @field = field - @alias = aliaz && SqlLiteral.new(aliaz) - end - - def as aliaz - self.alias = SqlLiteral.new(aliaz) - self end def hash @@ -23,8 +17,7 @@ module Arel def eql? other super && - self.field == other.field && - self.alias == other.alias + self.field == other.field end alias :== :eql? end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index ae1b7930af..ca09373b64 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -475,14 +475,7 @@ module Arel def visit_Arel_Nodes_Extract o, collector collector << "EXTRACT(#{o.field.to_s.upcase} FROM " - collector = visit o.expr, collector - collector << ")" - if o.alias - collector << " AS " - visit o.alias, collector - else - collector - end + visit(o.expr, collector) << ")" end def visit_Arel_Nodes_Count o, collector diff --git a/test/nodes/test_extract.rb b/test/nodes/test_extract.rb index 80bb465f24..eb98553268 100644 --- a/test/nodes/test_extract.rb +++ b/test/nodes/test_extract.rb @@ -15,6 +15,14 @@ describe Arel::Nodes::Extract do EXTRACT(DATE FROM "users"."timestamp") AS foo } end + + it 'should not mutate the extract' do + table = Arel::Table.new :users + extract = table[:timestamp].extract('date') + before = extract.dup + extract.as('foo') + assert_equal extract, before + end end describe 'equality' do |