Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport b63d532f1ed to 5.0 #26063

Merged
merged 1 commit into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* Hashes can once again be passed to setters of `composed_of`, if all of the
mapping methods are methods implemented on `Hash`.

Fixes #25978.

*Sean Griffin*

* Fix the SELECT statement in `#table_comment` for MySQL.

*Takeshi Akima*
Expand Down
6 changes: 4 additions & 2 deletions activerecord/lib/active_record/aggregations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ def writer_method(name, class_name, mapping, allow_nil, converter)
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
hash_from_multiparameter_assignment = part.is_a?(Hash) &&
part.each_key.all? { |k| k.is_a?(Integer) }
if hash_from_multiparameter_assignment
raise ArgumentError unless part.size == part.each_key.max
part = klass.new(*part.sort.map(&:last))
end

Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/aggregations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ def test_assigning_hash_to_custom_converter
customers(:barney).fullname = { first: "Barney", last: "Stinson" }
assert_equal "Barney STINSON", customers(:barney).name
end

def test_assigning_hash_without_custom_converter
customers(:barney).fullname_no_converter = { first: "Barney", last: "Stinson" }
assert_equal({ first: "Barney", last: "Stinson" }.to_s, customers(:barney).name)
end
end

class OverridingAggregationsTest < ActiveRecord::TestCase
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Customer < ActiveRecord::Base
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)}
composed_of :fullname, :mapping => %w(name to_s), :constructor => Proc.new { |name| Fullname.parse(name) }, :converter => :parse
composed_of :fullname_no_converter, :mapping => %w(name to_s), class_name: "Fullname"
end

class Address
Expand Down