aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/aggregations.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-05-31 09:56:19 -0400
committerSean Griffin <sean@seantheprogrammer.com>2016-05-31 10:03:26 -0400
commitff4986b917f6cc64398e2ba687f998fc52cf630c (patch)
tree23e0fd76336473d594cafde4b995fbc6581c3c7c /activerecord/lib/active_record/aggregations.rb
parent998bbbcc0e043e16b96613387cdb8f0ee4784ff2 (diff)
downloadrails-ff4986b917f6cc64398e2ba687f998fc52cf630c.tar.gz
rails-ff4986b917f6cc64398e2ba687f998fc52cf630c.tar.bz2
rails-ff4986b917f6cc64398e2ba687f998fc52cf630c.zip
Ensure hashes can be passed to attributes using `composed_of`
This behavior was broken by 36e9be85. When the value is assigned directly, either through mass assignment or directly assigning a hash, the hash gets passed through to this writer method directly. While this is intended to handle certain cases, when an explicit converter has been provided, we should continue to use that instead. The positioning of the added guard caused the new behavior to override that case. Fixes #25210
Diffstat (limited to 'activerecord/lib/active_record/aggregations.rb')
-rw-r--r--activerecord/lib/active_record/aggregations.rb9
1 files changed, 5 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb
index 3ff41ed81b..8bed5bca28 100644
--- a/activerecord/lib/active_record/aggregations.rb
+++ b/activerecord/lib/active_record/aggregations.rb
@@ -256,15 +256,16 @@ module ActiveRecord
def writer_method(name, class_name, mapping, allow_nil, converter)
define_method("#{name}=") do |part|
klass = class_name.constantize
- if part.is_a?(Hash)
- raise ArgumentError unless part.size == part.keys.max
- part = klass.new(*part.sort.map(&:last))
- end
unless part.is_a?(klass) || converter.nil? || part.nil?
part = converter.respond_to?(:call) ? converter.call(part) : klass.send(converter, part)
end
+ if part.is_a?(Hash)
+ raise ArgumentError unless part.size == part.keys.max
+ part = klass.new(*part.sort.map(&:last))
+ end
+
if part.nil? && allow_nil
mapping.each { |key, _| self[key] = nil }
@aggregation_cache[name] = nil