1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix composed_of with symbol mapping

Use `{read,write}_attribute` public API in `composed_of` because
`_read_attribute` internal API should be passed argument as a string
since 7834363bbf.

Fixes #40843.
This commit is contained in:
Ryuta Kamizono 2020-12-15 20:06:23 +09:00
parent 25c3eab0dc
commit 741c699683
2 changed files with 5 additions and 5 deletions

View file

@ -244,8 +244,8 @@ module ActiveRecord
private
def reader_method(name, class_name, mapping, allow_nil, constructor)
define_method(name) do
if @aggregation_cache[name].nil? && (!allow_nil || mapping.any? { |key, _| !_read_attribute(key).nil? })
attrs = mapping.collect { |key, _| _read_attribute(key) }
if @aggregation_cache[name].nil? && (!allow_nil || mapping.any? { |key, _| !read_attribute(key).nil? })
attrs = mapping.collect { |key, _| read_attribute(key) }
object = constructor.respond_to?(:call) ?
constructor.call(*attrs) :
class_name.constantize.send(constructor, *attrs)
@ -271,10 +271,10 @@ module ActiveRecord
end
if part.nil? && allow_nil
mapping.each { |key, _| self[key] = nil }
mapping.each { |key, _| write_attribute(key, nil) }
@aggregation_cache[name] = nil
else
mapping.each { |key, value| self[key] = part.send(value) }
mapping.each { |key, value| write_attribute(key, part.send(value)) }
@aggregation_cache[name] = part.freeze
end
end

View file

@ -4,7 +4,7 @@ class Customer < ActiveRecord::Base
cattr_accessor :gps_conversion_was_run
composed_of :address, mapping: [ %w(address_street street), %w(address_city city), %w(address_country country) ], allow_nil: true
composed_of :balance, class_name: "Money", mapping: %w(balance amount)
composed_of :balance, class_name: "Money", mapping: %i(balance amount)
composed_of :gps_location, allow_nil: true
composed_of :non_blank_gps_location, class_name: "GpsLocation", allow_nil: true, mapping: %w(gps_location gps_location),
converter: lambda { |gps| self.gps_conversion_was_run = true; gps.blank? ? nil : GpsLocation.new(gps) }