From 656ecd2d2587bc82efcb4c563efd8a2e4ddd6ad2 Mon Sep 17 00:00:00 2001 From: Matt Hodgson Date: Fri, 18 Oct 2013 13:14:54 -0400 Subject: [PATCH 1/2] allow Serializers to provide a root if they want to. --- lib/rocket_pants/controller/respondable.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/rocket_pants/controller/respondable.rb b/lib/rocket_pants/controller/respondable.rb index 036de55..6ec2eb7 100644 --- a/lib/rocket_pants/controller/respondable.rb +++ b/lib/rocket_pants/controller/respondable.rb @@ -5,12 +5,8 @@ module Respondable SerializerWrapper = Struct.new(:serializer, :object) do def serializable_hash(options = {}) - instance = serializer.new(object, options) - if instance.respond_to?(:serializable_hash) - instance.serializable_hash - else - instance.as_json options - end + instance = serializer.new(object, options.except(:root)) + instance.as_json options.except(:root) end end From 0bf0509a524c77f915b2dc17cb0cb062931b33ed Mon Sep 17 00:00:00 2001 From: Matt Hodgson Date: Fri, 18 Oct 2013 13:28:39 -0400 Subject: [PATCH 2/2] Update serializer specs --- .../active_model_serializers_spec.rb | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/spec/integration/active_model_serializers_spec.rb b/spec/integration/active_model_serializers_spec.rb index c96857f..5d9d129 100644 --- a/spec/integration/active_model_serializers_spec.rb +++ b/spec/integration/active_model_serializers_spec.rb @@ -12,13 +12,20 @@ # t.string :token let(:fish) { Fish.create! :name => "Test Fish", :latin_name => "Fishus fishii", :child_number => 1, :token => "xyz" } - after(:each) { Fish.delete_all } + after(:each) do + Fish.delete_all + SerializerB.root = false + end class SerializerA < ActiveModel::Serializer + self.root = false + attributes :name, :latin_name end class SerializerB < ActiveModel::Serializer + self.root = false + attributes :name, :child_number end @@ -34,6 +41,19 @@ class SerializerB < ActiveModel::Serializer end end + it "should respect the serializers setting for including the root in JSON" do + mock(TestController).test_data { fish } + SerializerB.root = true + mock(fish).active_model_serializer { SerializerB } + mock.proxy(SerializerB).new(fish, anything) { |r| r } + get :test_data + content[:response].should be_present + content[:response].should be_a Hash + puts content[:response].inspect + content[:response]["serializer_b"].should be_a Hash + content[:response]["serializer_b"].keys.map(&:to_sym).should =~ [:name, :child_number] + end + it 'should use the active_model_serializer' do mock(TestController).test_data { fish } mock(fish).active_model_serializer { SerializerB } @@ -133,7 +153,6 @@ class SerializerB < ActiveModel::Serializer it 'should default to root being false' do mock(TestController).test_data { [fish] } mock(TestController).test_options { {:each_serializer => SerializerA} } - mock.proxy(SerializerA).new(fish, rr_satisfy { |h| h[:root] == false }) { |r| r } get :test_data content[:response].should be_present content[:response].should be_a Array