Skip to content

Commit

Permalink
Fix composed_of with symbol mapping
Browse files Browse the repository at this point in the history
Use `{read,write}_attribute` public API in `composed_of` because
`_read_attribute` internal API should be passed argument as a string
since 7834363.

Fixes #40843.
  • Loading branch information
kamipo committed Dec 15, 2020
1 parent 25c3eab commit 741c699
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/aggregations.rb
Expand Up @@ -244,8 +244,8 @@ def composed_of(part_id, options = {})
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)
Expand All @@ -271,10 +271,10 @@ def writer_method(name, class_name, mapping, allow_nil, converter)
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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/models/customer.rb
Expand Up @@ -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) }
Expand Down

0 comments on commit 741c699

Please sign in to comment.