mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Remove implicit constant lookup in serializers in favor active_model_serializer API.
This commit is contained in:
@@ -78,8 +78,13 @@ class SerializerTest < ActiveModel::TestCase
|
||||
{ :title => @comment.read_attribute_for_serialization(:title) }
|
||||
end
|
||||
|
||||
def as_json(*)
|
||||
{ :comment => serializable_hash }
|
||||
def as_json(options=nil)
|
||||
options ||= {}
|
||||
if options[:root] == false
|
||||
serializable_hash
|
||||
else
|
||||
{ :comment => serializable_hash }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -189,65 +194,17 @@ class SerializerTest < ActiveModel::TestCase
|
||||
}, json)
|
||||
end
|
||||
|
||||
def test_implicit_serializer
|
||||
author_serializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :first_name
|
||||
end
|
||||
|
||||
blog_serializer = Class.new(ActiveModel::Serializer) do
|
||||
const_set(:AuthorSerializer, author_serializer)
|
||||
has_one :author
|
||||
end
|
||||
|
||||
user = User.new
|
||||
blog = Blog.new
|
||||
blog.author = user
|
||||
|
||||
json = blog_serializer.new(blog, user).as_json
|
||||
assert_equal({
|
||||
:author => {
|
||||
:first_name => "Jose"
|
||||
}
|
||||
}, json)
|
||||
end
|
||||
|
||||
def test_implicit_serializer_for_has_many
|
||||
blog_with_posts = Class.new(Blog) do
|
||||
attr_accessor :posts
|
||||
end
|
||||
|
||||
blog_serializer = Class.new(ActiveModel::Serializer) do
|
||||
const_set(:PostSerializer, PostSerializer)
|
||||
has_many :posts
|
||||
end
|
||||
|
||||
user = User.new
|
||||
blog = blog_with_posts.new
|
||||
blog.posts = [Post.new(:title => 'test')]
|
||||
|
||||
json = blog_serializer.new(blog, user).as_json
|
||||
assert_equal({
|
||||
:posts => [{
|
||||
:title => "test",
|
||||
:body => nil,
|
||||
:comments => []
|
||||
}]
|
||||
}, json)
|
||||
end
|
||||
|
||||
def test_overridden_associations
|
||||
author_serializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :first_name
|
||||
end
|
||||
|
||||
blog_serializer = Class.new(ActiveModel::Serializer) do
|
||||
const_set(:PersonSerializer, author_serializer)
|
||||
|
||||
def person
|
||||
object.author
|
||||
end
|
||||
|
||||
has_one :person
|
||||
has_one :person, :serializer => author_serializer
|
||||
end
|
||||
|
||||
user = User.new
|
||||
|
||||
Reference in New Issue
Block a user