mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Fixed #911
This commit is contained in:
parent
46ae776175
commit
a794a06fa5
@ -14,7 +14,7 @@ module ActiveModel
|
||||
:serializer,
|
||||
ActiveModel::Serializer.serializer_for(object)
|
||||
)
|
||||
serializer_class.new(object, options)
|
||||
serializer_class.new(object, options.except(:serializer))
|
||||
end
|
||||
@meta = options[:meta]
|
||||
@meta_key = options[:meta_key]
|
||||
|
||||
@ -22,6 +22,20 @@ module ActiveModel
|
||||
ActionController::Base.cache_store.clear
|
||||
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
|
||||
expected = [{
|
||||
title: "Hello!!",
|
||||
|
||||
@ -25,6 +25,12 @@ module ActiveModel
|
||||
assert_equal serializers.last.custom_options[:some], :options
|
||||
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
|
||||
meta_content = {meta: "the meta", meta_key: "the meta key"}
|
||||
@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
|
||||
end
|
||||
|
||||
CustomBlogSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attribute :id
|
||||
attribute :special_attribute
|
||||
|
||||
has_many :articles
|
||||
end
|
||||
|
||||
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user