Skip to content

Commit 50b041c

Browse files
committed
Merge pull request #37 from fotinakis/explicit-serializer-discovery
Support for explicit serializer discovery.
2 parents 1df09c7 + 7a208ca commit 50b041c

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

lib/jsonapi-serializers/serializer.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ def evaluate_attr_or_block(attribute_name, attr_or_block)
211211
end
212212

213213
def self.find_serializer_class_name(object)
214+
if object.respond_to?(:jsonapi_serializer_class_name)
215+
return object.jsonapi_serializer_class_name.to_s
216+
end
214217
"#{object.class.name}Serializer"
215218
end
216219

spec/serializer_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def serialize_primary(object, options = {})
423423
expect(JSONAPI::Serializer.serialize(post, include: ['author'])).to eq({
424424
'data' => expected_primary_data,
425425
'included' => [
426-
serialize_primary(post.author, {serializer: MyApp::UserSerializer}),
426+
serialize_primary(post.author, {serializer: MyAppOtherNamespace::UserSerializer}),
427427
],
428428
})
429429
end
@@ -518,7 +518,7 @@ def serialize_primary(object, options = {})
518518
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
519519
'included' => [
520520
# Only the author is included:
521-
serialize_primary(post.author, {serializer: MyApp::UserSerializer}),
521+
serialize_primary(post.author, {serializer: MyAppOtherNamespace::UserSerializer}),
522522
],
523523
}
524524
includes = ['long-comments.post.author']
@@ -542,8 +542,8 @@ def serialize_primary(object, options = {})
542542
# Same note about primary data linkages as above.
543543
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
544544
'included' => [
545-
serialize_primary(first_user, {serializer: MyApp::UserSerializer}),
546-
serialize_primary(second_user, {serializer: MyApp::UserSerializer}),
545+
serialize_primary(first_user, {serializer: MyAppOtherNamespace::UserSerializer}),
546+
serialize_primary(second_user, {serializer: MyAppOtherNamespace::UserSerializer}),
547547
],
548548
}
549549
includes = ['long-comments.user']
@@ -571,7 +571,7 @@ def serialize_primary(object, options = {})
571571
include_linkages: ['user'],
572572
}),
573573
# Note: post.author does not show up here because it was not included.
574-
serialize_primary(comment_user, {serializer: MyApp::UserSerializer}),
574+
serialize_primary(comment_user, {serializer: MyAppOtherNamespace::UserSerializer}),
575575
],
576576
}
577577
includes = ['long-comments', 'long-comments.user']
@@ -598,7 +598,7 @@ def serialize_primary(object, options = {})
598598
'included' => [
599599
serialize_primary(long_comments.first, {serializer: MyApp::LongCommentSerializer}),
600600
serialize_primary(long_comments.last, {serializer: MyApp::LongCommentSerializer}),
601-
serialize_primary(post.author, {serializer: MyApp::UserSerializer}),
601+
serialize_primary(post.author, {serializer: MyAppOtherNamespace::UserSerializer}),
602602
],
603603
}
604604
# Also test that it handles string include arguments.

spec/support/serializers.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class LongComment
1717
class User
1818
attr_accessor :id
1919
attr_accessor :name
20+
21+
def jsonapi_serializer_class_name
22+
'MyAppOtherNamespace::UserSerializer'
23+
end
2024
end
2125

2226
class PostSerializer
@@ -41,12 +45,6 @@ class LongCommentSerializer
4145
has_one :post
4246
end
4347

44-
class UserSerializer
45-
include JSONAPI::Serializer
46-
47-
attribute :name
48-
end
49-
5048
# More customized, one-off serializers to test particular behaviors:
5149

5250
class SimplestPostSerializer
@@ -141,3 +139,13 @@ class MultipleAttributesSerializer
141139
attributes :title, :body
142140
end
143141
end
142+
143+
# Test the `jsonapi_serializer_class_name` override method for serializers in different namespaces.
144+
# There is no explicit test for this, just implicit tests that correctly serialize User objects.
145+
module MyAppOtherNamespace
146+
class UserSerializer
147+
include JSONAPI::Serializer
148+
149+
attribute :name
150+
end
151+
end

0 commit comments

Comments
 (0)