diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index bae2cab4579a30c65b8c29b82f55e279d1e51518..af4e42ba9aa0fbfdbdfb3a906069382f80fb3207 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Allow ActiveRecord::Base#as_json to be passed a frozen Hash. + + *Isaac Betesh* + * Deprecate locking records with unpersisted changes. *Marc Schütz* diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb index 5a408e7b8e01a9ce6fadd838290992caa1ff2150..db2bd0b55eddc19ffcbdaa81c1921135703162f6 100644 --- a/activerecord/lib/active_record/serialization.rb +++ b/activerecord/lib/active_record/serialization.rb @@ -9,7 +9,7 @@ module Serialization end def serializable_hash(options = nil) - options = options.try(:clone) || {} + options = options.try(:dup) || {} options[:except] = Array(options[:except]).map(&:to_s) options[:except] |= Array(self.class.inheritance_column) diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb index 155e858822ab669f6bc75c884dbe0896eb914919..5a1d066aefbc5d20d03a60d994a160e3f815b54a 100644 --- a/activerecord/test/cases/json_serialization_test.rb +++ b/activerecord/test/cases/json_serialization_test.rb @@ -101,6 +101,17 @@ def @contact.favorite_quote; "Constraints are liberating"; end assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json end + def test_uses_serializable_hash_with_frozen_hash + def @contact.serializable_hash(options = nil) + super({ only: %w(name) }.freeze) + end + + json = @contact.to_json + assert_match %r{"name":"Konata Izumi"}, json + assert_no_match %r{awesome}, json + assert_no_match %r{age}, json + end + def test_uses_serializable_hash_with_only_option def @contact.serializable_hash(options = nil) super(only: %w(name))