mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
add support for root keys
remove debugging gem fix white space
This commit is contained in:
parent
97023db904
commit
7338b62b02
@ -12,7 +12,7 @@ module ActionController
|
|||||||
|
|
||||||
if serializer
|
if serializer
|
||||||
# omg hax
|
# omg hax
|
||||||
object = serializer.new(resource)
|
object = serializer.new(resource, options)
|
||||||
adapter = ActiveModel::Serializer.adapter.new(object)
|
adapter = ActiveModel::Serializer.adapter.new(object)
|
||||||
|
|
||||||
super(adapter, options)
|
super(adapter, options)
|
||||||
|
|||||||
@ -87,10 +87,31 @@ module ActiveModel
|
|||||||
adapter_class
|
adapter_class
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :object
|
def self._root
|
||||||
|
@@root ||= false
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(object)
|
def self._root=(root)
|
||||||
|
@@root = root
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.root_name
|
||||||
|
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_accessor :object, :root
|
||||||
|
|
||||||
|
def initialize(object, options = {})
|
||||||
@object = object
|
@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
|
end
|
||||||
|
|
||||||
def attributes(options = {})
|
def attributes(options = {})
|
||||||
|
|||||||
@ -8,7 +8,7 @@ module ActiveModel
|
|||||||
|
|
||||||
attr_reader :serializer
|
attr_reader :serializer
|
||||||
|
|
||||||
def initialize(serializer)
|
def initialize(serializer, options = {})
|
||||||
@serializer = serializer
|
@serializer = serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -17,7 +17,13 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def to_json(options = {})
|
def to_json(options = {})
|
||||||
serializable_hash(options).to_json
|
result = serializable_hash(options)
|
||||||
|
|
||||||
|
if root = options.fetch(:root, serializer.json_key)
|
||||||
|
result = { root => result }
|
||||||
|
end
|
||||||
|
|
||||||
|
result.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -13,6 +13,7 @@ module ActiveModel
|
|||||||
@hash[name] = association.attributes(options)
|
@hash[name] = association.attributes(options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@hash
|
@hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,7 +5,13 @@ module ActionController
|
|||||||
class ImplicitSerializerTest < ActionController::TestCase
|
class ImplicitSerializerTest < ActionController::TestCase
|
||||||
class MyController < ActionController::Base
|
class MyController < ActionController::Base
|
||||||
def render_using_implicit_serializer
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -18,8 +24,13 @@ module ActionController
|
|||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
|
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user