Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions lib/rocket_pants/controller/respondable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions spec/integration/active_model_serializers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 }
Expand Down Expand Up @@ -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
Expand Down