1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib/arel/nodes/homogeneous_in.rb
Ryuta Kamizono 6d06a657fc Fix binds logging for HomogeneousIn
Make bind attribute if the bind collector is used.

Before:

```
User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?, ?, ?, ?)  [[nil, 1], [nil, 2], [nil, 3], [nil, 4], [nil, 5]]
```

After:

```
User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?, ?, ?, ?)  [["id", 1], ["id", 2], ["id", 3], ["id", 4], ["id", 5]]
```

(cherry picked from commit cd1a3705c5f18e431e5311c5dc123ed765752ecf)
2021-02-25 20:37:20 -05:00

76 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Arel # :nodoc: all
module Nodes
class HomogeneousIn < Node
attr_reader :attribute, :values, :type
def initialize(values, attribute, type)
@values = values
@attribute = attribute
@type = type
end
def hash
ivars.hash
end
def eql?(other)
super || (self.class == other.class && self.ivars == other.ivars)
end
alias :== :eql?
def equality?
type == :in
end
def invert
Arel::Nodes::HomogeneousIn.new(values, attribute, type == :in ? :notin : :in)
end
def left
attribute
end
def right
attribute.quoted_array(values)
end
def table_name
attribute.relation.table_alias || attribute.relation.name
end
def column_name
attribute.name
end
def casted_values
type = attribute.type_caster
casted_values = values.map do |raw_value|
type.serialize(raw_value) if type.serializable?(raw_value)
end
casted_values.compact!
casted_values
end
def proc_for_binds
-> value { ActiveModel::Attribute.with_cast_value(attribute.name, value, attribute.type_caster) }
end
def fetch_attribute(&block)
if attribute
yield attribute
else
expr.fetch_attribute(&block)
end
end
protected
def ivars
[@attribute, @values, @type]
end
end
end
end