From da914fa35c014b7753e609e25c6a45d3cda11274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Hock=20Isaza?= Date: Thu, 29 Sep 2011 20:07:19 -0500 Subject: Fixing `as_json` method for ActiveRecord models. When you've got an AR Model and you override the `as_json` method, you should be able to add default options to the renderer, like this: class User < ActiveRecord::Base def as_json(options = {}) super(options.merge(:except => [:password_digest])) end end This was not possible before this commit. See the added test case. --- activemodel/test/cases/serializers/json_serialization_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activemodel/test/cases/serializers') diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb index 40fdcf20ca..a754d610b9 100644 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ b/activemodel/test/cases/serializers/json_serialization_test.rb @@ -206,4 +206,14 @@ class JsonSerializationTest < ActiveModel::TestCase assert_no_match %r{"preferences":}, json end + test "custom as_json options should be extendible" do + def @contact.as_json(options = {}); super(options.merge(:only => [:name])); end + json = @contact.to_json + + assert_match %r{"name":"Konata Izumi"}, json + assert_no_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json + assert_no_match %r{"awesome":}, json + assert_no_match %r{"preferences":}, json + end + end -- cgit v1.2.3