Skip to content

Add support for root keys #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2014
Merged
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
2 changes: 1 addition & 1 deletion lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Serialization

if serializer
# omg hax
object = serializer.new(resource)
object = serializer.new(resource, options)
adapter = ActiveModel::Serializer.adapter.new(object)

super(adapter, options)
Expand Down
37 changes: 29 additions & 8 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def self.serializer_for(resource)

def self.adapter
adapter_class = case config.adapter
when Symbol
class_name = "ActiveModel::Serializer::Adapter::#{config.adapter.to_s.classify}"
class_name.safe_constantize
when Class
config.adapter
end
when Symbol
class_name = "ActiveModel::Serializer::Adapter::#{config.adapter.to_s.classify}"
class_name.safe_constantize
when Class
config.adapter
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how embarassing :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just using the SublimeText plugin, Beautify Ruby

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant how embarrassing that I committed a bunch of tabs! This is a good patch.

unless adapter_class
valid_adapters = Adapter.constants.map { |klass| ":#{klass.to_s.downcase}" }
raise ArgumentError, "Unknown adapter: #{config.adapter}. Valid adapters are: #{valid_adapters}"
Expand All @@ -87,10 +87,31 @@ def self.adapter
adapter_class
end

attr_accessor :object
def self._root
@@root ||= false
end

def self._root=(root)
@@root = root
end

def self.root_name
name.demodulize.underscore.sub(/_serializer$/, '') if name
end

def initialize(object)
attr_accessor :object, :root

def initialize(object, options = {})
@object = object
@root = options[:root] || (self.class._root ? self.class.root_name : false)
end

def json_key
if root == true || root.nil?
self.class.root_name
else
root
end
end

def attributes(options = {})
Expand Down
12 changes: 9 additions & 3 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ class Adapter

attr_reader :serializer

def initialize(serializer)
def initialize(serializer, options = {})
@serializer = serializer
end

def serializable_hash(options = {})
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
end

def to_json(options={})
serializable_hash(options).to_json
def to_json(options = {})
result = serializable_hash(options)

if root = options.fetch(:root, serializer.json_key)
result = { root => result }
end

result.to_json
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def serializable_hash(options = {})
@hash[name] = association.attributes(options)
end
end

@hash
end
end
Expand Down
17 changes: 14 additions & 3 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ module Serialization
class ImplicitSerializerTest < ActionController::TestCase
class MyController < ActionController::Base
def render_using_implicit_serializer
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
end

def render_using_custom_root
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "custom_root"
end
end

Expand All @@ -18,8 +24,13 @@ def test_render_using_implicit_serializer
assert_equal 'application/json', @response.content_type
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
end

def test_render_using_custom_root
get :render_using_custom_root

assert_equal 'application/json', @response.content_type
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
end
end
end
end