aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2017-02-08 14:37:18 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2017-02-08 14:37:18 -0300
commite2b425bc741d19308f98fed1103bf7d66c8cad2d (patch)
treeef66a0fc75d07b2a44e1e4cbe67b1b994c6d8709 /activerecord
parent4bb65a8f6d289907c509dceef43fa33833c96362 (diff)
parentc01f31f43c870bd9a828ae96207de0c2cdd8fd08 (diff)
downloadrails-e2b425bc741d19308f98fed1103bf7d66c8cad2d.tar.gz
rails-e2b425bc741d19308f98fed1103bf7d66c8cad2d.tar.bz2
rails-e2b425bc741d19308f98fed1103bf7d66c8cad2d.zip
Merge pull request #27945 from betesh/allow-frozen-hashes-to-as_json
Allow ActiveRecord::Base.as_json to accept a frozen Hash
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/serialization.rb2
-rw-r--r--activerecord/test/cases/json_serialization_test.rb11
3 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index f8fa71c60b..c70de5a370 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*
+
* Fix inspection behavior when the :id column is not primary key.
*namusyaka*
diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb
index 5a408e7b8e..db2bd0b55e 100644
--- a/activerecord/lib/active_record/serialization.rb
+++ b/activerecord/lib/active_record/serialization.rb
@@ -9,7 +9,7 @@ module ActiveRecord #:nodoc:
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 155e858822..5a1d066aef 100644
--- a/activerecord/test/cases/json_serialization_test.rb
+++ b/activerecord/test/cases/json_serialization_test.rb
@@ -101,6 +101,17 @@ class JsonSerializationTest < ActiveRecord::TestCase
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))