mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Allow as_json(true) to be given.
This commit is contained in:
parent
e7d66ee423
commit
97ff4d28e6
@ -201,6 +201,9 @@ module ActiveModel
|
|||||||
# methods, provided by default by ActiveRecord. You can implement these
|
# methods, provided by default by ActiveRecord. You can implement these
|
||||||
# methods on your custom models if you want the serializer's schema method
|
# methods on your custom models if you want the serializer's schema method
|
||||||
# to work.
|
# to work.
|
||||||
|
#
|
||||||
|
# TODO: This is currently coupled to Active Record. We need to
|
||||||
|
# figure out a way to decouple those two.
|
||||||
def schema
|
def schema
|
||||||
klass = model_class
|
klass = model_class
|
||||||
columns = klass.columns_hash
|
columns = klass.columns_hash
|
||||||
@ -241,7 +244,6 @@ module ActiveModel
|
|||||||
|
|
||||||
def inherited(klass) #:nodoc:
|
def inherited(klass) #:nodoc:
|
||||||
return if klass.anonymous?
|
return if klass.anonymous?
|
||||||
|
|
||||||
name = klass.name.demodulize.underscore.sub(/_serializer$/, '')
|
name = klass.name.demodulize.underscore.sub(/_serializer$/, '')
|
||||||
|
|
||||||
klass.class_eval do
|
klass.class_eval do
|
||||||
@ -260,8 +262,9 @@ module ActiveModel
|
|||||||
|
|
||||||
# Returns a json representation of the serializable
|
# Returns a json representation of the serializable
|
||||||
# object including the root.
|
# object including the root.
|
||||||
def as_json(*)
|
def as_json(options=nil)
|
||||||
if root = @options[:root] || _root
|
options ||= {}
|
||||||
|
if root = options.fetch(:root, @options.fetch(:root, _root))
|
||||||
@hash = hash = {}
|
@hash = hash = {}
|
||||||
hash.merge!(root => serializable_hash)
|
hash.merge!(root => serializable_hash)
|
||||||
hash
|
hash
|
||||||
|
|||||||
@ -703,7 +703,19 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
author = author_class.new(:id => 5, :name => "Tom Dale")
|
author = author_class.new(:id => 5, :name => "Tom Dale")
|
||||||
post.author = author
|
post.author = author
|
||||||
|
|
||||||
hash = serializer_class.new(post, nil, :root => :blog_post)
|
assert_equal({
|
||||||
|
:blog_post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "It's a new post!",
|
||||||
|
:author => { :id => 5, :name => "Tom Dale" }
|
||||||
|
}
|
||||||
|
}, serializer_class.new(post, nil, :root => :blog_post).as_json)
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "It's a new post!",
|
||||||
|
:author => { :id => 5, :name => "Tom Dale" }
|
||||||
|
}, serializer_class.new(post, nil, :root => false).as_json)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:blog_post => {
|
:blog_post => {
|
||||||
@ -711,7 +723,13 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:body => "It's a new post!",
|
:body => "It's a new post!",
|
||||||
:author => { :id => 5, :name => "Tom Dale" }
|
:author => { :id => 5, :name => "Tom Dale" }
|
||||||
}
|
}
|
||||||
}, hash.as_json)
|
}, serializer_class.new(post, nil).as_json(:root => :blog_post))
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "It's a new post!",
|
||||||
|
:author => { :id => 5, :name => "Tom Dale" }
|
||||||
|
}, serializer_class.new(post, nil).as_json(:root => false))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_serializer_has_access_to_root_object
|
def test_serializer_has_access_to_root_object
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user