mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Extract#as
should not mutate the receiver
Fixes https://github.com/rails/rails/issues/16913
This commit is contained in:
parent
d23df0ec8a
commit
8c73dad076
3 changed files with 12 additions and 18 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue