-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Namespaced serializers #1338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Namespaced serializers #1338
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,10 @@ def self.serializer_lookup_chain_for(klass) | |
resource_namespace = klass.name.deconstantize | ||
serializer_class_name = "#{resource_class_name}Serializer" | ||
|
||
chain.push("#{name}::#{serializer_class_name}") if self != ActiveModel::Serializer | ||
if self != ActiveModel::Serializer | ||
chain.push("#{name}::#{serializer_class_name}") | ||
chain.push(*name.deconstantize.split('::').each_with_object([]) { |element, array| array.push((array.last ? array.last + '::' : '') + element) }.reverse.map { |k| k + "::#{serializer_class_name}" }) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @beauby another reason why search should be a detect (first found) rather than functional style without a stream to take from :) as it is, it's making more calculations than it needs to. |
||
chain.push("#{resource_namespace}::#{serializer_class_name}") | ||
|
||
chain | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,6 +262,49 @@ def false | |
assert_equal(expected, hash) | ||
end | ||
end | ||
|
||
class NamespaceNestedSerializersTest < Minitest::Test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This snippet from the PR description would also make a great test showing the old behavior and the behavior this PR introduces. It wasn't obvious to me from the examples why you'd want to 'step back' one level in the serializer lookup.
|
||
Post = Class.new(::Model) | ||
Comment = Class.new(::Model) | ||
Author = Class.new(::Model) | ||
Description = Class.new(::Model) | ||
|
||
module SerializerNamespace | ||
class PostSerializer < ActiveModel::Serializer | ||
has_many :comments | ||
belongs_to :author | ||
has_one :description | ||
end | ||
CommentSerializer = Class.new(ActiveModel::Serializer) | ||
AuthorSerializer = Class.new(ActiveModel::Serializer) | ||
DescriptionSerializer = Class.new(ActiveModel::Serializer) | ||
end | ||
|
||
def setup | ||
@comment = Comment.new | ||
@author = Author.new | ||
@description = Description.new | ||
@post = Post.new(comments: [@comment], | ||
author: @author, | ||
description: @description) | ||
@post_serializer = SerializerNamespace::PostSerializer.new(@post) | ||
end | ||
|
||
def test_associations_namespaced_resources_new | ||
@post_serializer.associations.each do |association| | ||
case association.key | ||
when :comments | ||
assert_instance_of(SerializerNamespace::CommentSerializer, association.serializer.first) | ||
when :author | ||
assert_instance_of(SerializerNamespace::AuthorSerializer, association.serializer) | ||
when :description | ||
assert_instance_of(SerializerNamespace::DescriptionSerializer, association.serializer) | ||
else | ||
flunk "Unknown association: #{key}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A slightly shorter version that leads to the same result: