mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 14:29:31 +00:00
Fixed #911
This commit is contained in:
parent
46ae776175
commit
a794a06fa5
@ -14,7 +14,7 @@ module ActiveModel
|
|||||||
:serializer,
|
:serializer,
|
||||||
ActiveModel::Serializer.serializer_for(object)
|
ActiveModel::Serializer.serializer_for(object)
|
||||||
)
|
)
|
||||||
serializer_class.new(object, options)
|
serializer_class.new(object, options.except(:serializer))
|
||||||
end
|
end
|
||||||
@meta = options[:meta]
|
@meta = options[:meta]
|
||||||
@meta_key = options[:meta_key]
|
@meta_key = options[:meta_key]
|
||||||
|
|||||||
@ -22,6 +22,20 @@ module ActiveModel
|
|||||||
ActionController::Base.cache_store.clear
|
ActionController::Base.cache_store.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_with_serializer_option
|
||||||
|
@blog.special_attribute = "Special"
|
||||||
|
@blog.articles = [@first_post, @second_post]
|
||||||
|
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
|
||||||
|
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
||||||
|
|
||||||
|
expected = [{
|
||||||
|
id: 1,
|
||||||
|
special_attribute: "Special",
|
||||||
|
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
|
||||||
|
}]
|
||||||
|
assert_equal expected, @adapter.serializable_hash
|
||||||
|
end
|
||||||
|
|
||||||
def test_include_multiple_posts
|
def test_include_multiple_posts
|
||||||
expected = [{
|
expected = [{
|
||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
|
|||||||
@ -25,6 +25,12 @@ module ActiveModel
|
|||||||
assert_equal serializers.last.custom_options[:some], :options
|
assert_equal serializers.last.custom_options[:some], :options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_serializer_option_not_passed_to_each_serializer
|
||||||
|
serializers = ArraySerializer.new([@post], {serializer: PostSerializer}).to_a
|
||||||
|
|
||||||
|
refute serializers.first.custom_options.key?(:serializer)
|
||||||
|
end
|
||||||
|
|
||||||
def test_meta_and_meta_key_attr_readers
|
def test_meta_and_meta_key_attr_readers
|
||||||
meta_content = {meta: "the meta", meta_key: "the meta key"}
|
meta_content = {meta: "the meta", meta_key: "the meta key"}
|
||||||
@serializer = ArraySerializer.new([@comment, @post], meta_content)
|
@serializer = ArraySerializer.new([@comment, @post], meta_content)
|
||||||
|
|||||||
7
test/fixtures/poro.rb
vendored
7
test/fixtures/poro.rb
vendored
@ -178,6 +178,13 @@ AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
attribute :name, key: :title
|
attribute :name, key: :title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
CustomBlogSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attribute :id
|
||||||
|
attribute :special_attribute
|
||||||
|
|
||||||
|
has_many :articles
|
||||||
|
end
|
||||||
|
|
||||||
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
attributes :id
|
attributes :id
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user