From 16a5b1da6c9926f4cede1109df769e558884842e Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 6 Apr 2011 14:11:48 -0700 Subject: [PATCH] ActiveModel::Serializers::JSON#as_json should call #as_json on the serializable_hash. [#4520 state:resolved] --- activemodel/lib/active_model/serializers/json.rb | 2 +- .../cases/serializers/json_serialization_test.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/activemodel/lib/active_model/serializers/json.rb b/activemodel/lib/active_model/serializers/json.rb index 0bfbf2a..91f7a77 100644 --- a/activemodel/lib/active_model/serializers/json.rb +++ b/activemodel/lib/active_model/serializers/json.rb @@ -88,7 +88,7 @@ module ActiveModel hash = { custom_root || self.class.model_name.element => hash } end - hash + hash.as_json end def from_json(json) diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb index 500a5c5..2290475 100644 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ b/activemodel/test/cases/serializers/json_serialization_test.rb @@ -131,10 +131,18 @@ class JsonSerializationTest < ActiveModel::TestCase assert_kind_of Hash, json assert_kind_of Hash, json['contact'] %w(name age created_at awesome preferences).each do |field| - assert_equal @contact.send(field), json['contact'][field] + assert_equal @contact.send(field).as_json, json['contact'][field] end end + test "as_json should call as_json on serializable_hash" do + def @contact.serializable_hash(options = nil) + {:name => "John", :created_at => Time.utc(2006, 8, 1)} + end + + assert_equal({"contact" => @contact.serializable_hash.as_json}, @contact.as_json) + end + test "custom as_json should be honored when generating json" do def @contact.as_json(options); { :name => name, :created_at => created_at }; end json = @contact.to_json -- 1.7.4.3