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 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
# frozen_string_literal: true
module Arel # :nodoc: all
module Nodes
class Casted < Arel::Nodes::NodeExpression # :nodoc:
attr_reader :value, :attribute
alias :value_before_type_cast :value
def initialize(value, attribute)
@value = value
@attribute = attribute
super()
end
def nil?; value.nil?; end
def value_for_database
if attribute.able_to_type_cast?
attribute.type_cast_for_database(value)
else
value
end
end
def hash
[self.class, value, attribute].hash
end
def eql?(other)
self.class == other.class &&
self.value == other.value &&
self.attribute == other.attribute
end
alias :== :eql?
end
class Quoted < Arel::Nodes::Unary # :nodoc:
alias :value_for_database :value
alias :value_before_type_cast :value
def nil?; value.nil?; end
def infinite?
value.respond_to?(:infinite?) && value.infinite?
end
end
def self.build_quoted(other, attribute = nil)
case other
when Arel::Nodes::Node, Arel::Attributes::Attribute, Arel::Table, Arel::SelectManager, Arel::Nodes::SqlLiteral, ActiveModel::Attribute
other
else
case attribute
when Arel::Attributes::Attribute
Casted.new other, attribute
else
Quoted.new other
end
end
end
end
end
|