mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Fix model attributes accessors
This commit is contained in:
@@ -4,6 +4,10 @@ module ActiveModelSerializers
|
||||
module Adapter
|
||||
class Json
|
||||
class HasManyTestTest < ActiveSupport::TestCase
|
||||
class ModelWithoutSerializer < ::Model
|
||||
attributes :id, :name
|
||||
end
|
||||
|
||||
def setup
|
||||
ActionController::Base.cache_store.clear
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@@ -16,7 +20,7 @@ module ActiveModelSerializers
|
||||
@second_comment.post = @post
|
||||
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
||||
@post.blog = @blog
|
||||
@tag = Tag.new(id: 1, name: '#hash_tag')
|
||||
@tag = ModelWithoutSerializer.new(id: 1, name: '#hash_tag')
|
||||
@post.tags = [@tag]
|
||||
end
|
||||
|
||||
@@ -30,7 +34,11 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_has_many_with_no_serializer
|
||||
serializer = PostWithTagsSerializer.new(@post)
|
||||
post_serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id
|
||||
has_many :tags
|
||||
end
|
||||
serializer = post_serializer_class.new(@post)
|
||||
adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
|
||||
assert_equal({
|
||||
id: 42,
|
||||
|
||||
@@ -4,6 +4,10 @@ module ActiveModelSerializers
|
||||
module Adapter
|
||||
class JsonApi
|
||||
class HasManyTest < ActiveSupport::TestCase
|
||||
class ModelWithoutSerializer < ::Model
|
||||
attributes :id, :name
|
||||
end
|
||||
|
||||
def setup
|
||||
ActionController::Base.cache_store.clear
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@@ -26,7 +30,7 @@ module ActiveModelSerializers
|
||||
@blog.articles = [@post]
|
||||
@post.blog = @blog
|
||||
@post_without_comments.blog = nil
|
||||
@tag = Tag.new(id: 1, name: '#hash_tag')
|
||||
@tag = ModelWithoutSerializer.new(id: 1, name: '#hash_tag')
|
||||
@post.tags = [@tag]
|
||||
@serializer = PostSerializer.new(@post)
|
||||
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
|
||||
@@ -129,7 +133,11 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def test_has_many_with_no_serializer
|
||||
serializer = PostWithTagsSerializer.new(@post)
|
||||
post_serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id
|
||||
has_many :tags
|
||||
end
|
||||
serializer = post_serializer_class.new(@post)
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
||||
|
||||
assert_equal({
|
||||
|
||||
@@ -14,11 +14,21 @@ module ActiveModel
|
||||
[{ foo: 'bar' }]
|
||||
end
|
||||
end
|
||||
class Tag < ::Model
|
||||
attributes :id, :name
|
||||
end
|
||||
|
||||
class TagSerializer < ActiveModel::Serializer
|
||||
type 'tags'
|
||||
attributes :id, :name
|
||||
end
|
||||
|
||||
class PostWithTagsSerializer < ActiveModel::Serializer
|
||||
type 'posts'
|
||||
attributes :id
|
||||
has_many :tags
|
||||
end
|
||||
|
||||
class IncludeParamAuthorSerializer < ActiveModel::Serializer
|
||||
class_attribute :comment_loader
|
||||
|
||||
|
||||
Reference in New Issue
Block a user