Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module ActiveModelSerializers | ||
module LookupChain | ||
# Standard appending of Serializer to the resource name. | ||
# | ||
# Example: | ||
# Author => AuthorSerializer | ||
BY_RESOURCE = lambda do |resource_class, _serializer_class| | ||
serializer_from(resource_class) | ||
end | ||
|
||
# Uses the namespace of the resource to find the serializer= | ||
# | ||
# Example: | ||
# British::Author => British::AuthorSerializer | ||
BY_NAMESPACE = lambda do |resource_class, _serializer_class| | ||
resource_namespace = namespace_for(resource_class) | ||
serializer_name = serializer_from(resource_class) | ||
|
||
"#{resource_namespace}::#{serializer_name}" | ||
end | ||
|
||
# Allows for serializers to be defined in parent serializers | ||
# - useful if a relationship only needs a different set of attributes | ||
# than if it were rendered independently. | ||
# | ||
# Example: | ||
# class BlogSerializer < ActiveModel::Serializer | ||
# class AuthorSerialier < ActiveModel::Serializer | ||
# ... | ||
# end | ||
# | ||
# belongs_to :author | ||
# ... | ||
# end | ||
# | ||
# The belongs_to relationship would be rendered with | ||
# BlogSerializer::AuthorSerialier | ||
BY_PARENT_SERIALIZER = lambda do |resource_class, serializer_class| | ||
return if serializer_class == ActiveModel::Serializer | ||
|
||
serializer_name = serializer_from(resource_class) | ||
"#{serializer_class.name}::#{serializer_name}" | ||
end | ||
|
||
DEFAULT = [ | ||
BY_PARENT_SERIALIZER, | ||
BY_NAMESPACE, | ||
BY_RESOURCE | ||
].freeze | ||
|
||
module_function | ||
|
||
def namespace_for(klass) | ||
klass.name.deconstantize | ||
end | ||
|
||
def resource_class_name(klass) | ||
klass.name.demodulize | ||
end | ||
|
||
def serializer_from_resource_name(name) | ||
"#{name}Serializer" | ||
end | ||
|
||
def serializer_from(klass) | ||
name = resource_class_name(klass) | ||
serializer_from_resource_name(name) | ||
end | ||
end | ||
end |
Empty file.
42 changes: 42 additions & 0 deletions
42
test/feature/configuration/serializer_lookup/using_serializable_resource_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'test_helper' | ||
|
||
describe 'feature' do | ||
describe 'configuration' do | ||
describe 'serializer lookup' do | ||
describe 'using SerializableResource' do | ||
it 'uses the child serializer' do | ||
class Parent < ::Model; end | ||
class Child < ::Model; end | ||
|
||
class ParentSerializer < ActiveModel::Serializer | ||
class ChildSerializer < ActiveModel::Serializer | ||
attributes :name, :child_attr | ||
def child_attr | ||
true | ||
end | ||
end | ||
|
||
attributes :name | ||
belongs_to :child | ||
end | ||
|
||
parent = Parent.new(name: 'parent', child: Child.new(name: 'child')) | ||
resource = ActiveModelSerializers::SerializableResource.new( | ||
parent, | ||
adapter: :attributes | ||
) | ||
|
||
expected = { | ||
name: 'parent', | ||
child: { | ||
name: 'child', | ||
child_attr: true | ||
} | ||
} | ||
|
||
assert_equal expected, resource.serializable_hash | ||
end | ||
end | ||
end | ||
end | ||
end |