-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Move serialization logic into Serializer and CollectionSerializer #1766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f28e486
Remove Attributes adapter recursion
bf4 41575e3
Moving Attributes#serializable_hash_for_single_resource to Serializer
bf4 516e7da
Move serialization logic into Serializer and CollectionSerializer
bf4 913f396
Move adapter cache properties to class level (where they belong).
bf4 caf4910
Remove controller :assigns warnings/shim
bf4 7254d34
Move Serializer#serialize into Serializer#serializable_hash
bf4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -98,6 +98,22 @@ def self.get_serializer_for(klass) | |
end | ||
end | ||
|
||
# @api private | ||
def self.include_directive_from_options(options) | ||
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 should probably go in AMS or some other serialization-related class |
||
if options[:include_directive] | ||
options[:include_directive] | ||
elsif options[:include] | ||
JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true) | ||
else | ||
ActiveModelSerializers.default_include_directive | ||
end | ||
end | ||
|
||
# @api private | ||
def self.serialization_adapter_instance | ||
@serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes | ||
end | ||
|
||
attr_accessor :object, :root, :scope | ||
|
||
# `scope_name` is set as :current_user by default in the controller. | ||
|
@@ -123,9 +139,7 @@ def success? | |
# associations, similar to how ActiveModel::Serializers::JSON is used | ||
# in ActiveRecord::Base. | ||
# | ||
# TODO: Move to here the Attributes adapter logic for | ||
# +serializable_hash_for_single_resource(options)+ | ||
# and include <tt>ActiveModel::Serializers::JSON</tt>. | ||
# TODO: Include <tt>ActiveModel::Serializers::JSON</tt>. | ||
# So that the below is true: | ||
# @param options [nil, Hash] The same valid options passed to `serializable_hash` | ||
# (:only, :except, :methods, and :include). | ||
|
@@ -149,11 +163,13 @@ def success? | |
# serializer.as_json(include: :posts) | ||
# # Second level and higher order associations work as well: | ||
# serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } }) | ||
def serializable_hash(adapter_opts = nil) | ||
adapter_opts ||= {} | ||
adapter_opts = { include: '*', adapter: :attributes }.merge!(adapter_opts) | ||
adapter = ActiveModelSerializers::Adapter.create(self, adapter_opts) | ||
adapter.serializable_hash(adapter_opts) | ||
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance) | ||
adapter_options ||= {} | ||
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options) | ||
cached_attributes = adapter_options[:cached_attributes] ||= {} | ||
resource = cached_attributes(options[:fields], cached_attributes, adapter_instance) | ||
relationships = resource_relationships(adapter_options, options, adapter_instance) | ||
resource.merge(relationships) | ||
end | ||
alias to_hash serializable_hash | ||
alias to_h serializable_hash | ||
|
@@ -185,6 +201,35 @@ def read_attribute_for_serialization(attr) | |
end | ||
end | ||
|
||
# @api private | ||
def resource_relationships(adapter_options, options, adapter_instance) | ||
relationships = {} | ||
include_directive = options.fetch(:include_directive) | ||
associations(include_directive).each do |association| | ||
adapter_opts = adapter_options.merge(include_directive: include_directive[association.key]) | ||
relationships[association.key] ||= relationship_value_for(association, adapter_opts, adapter_instance) | ||
end | ||
|
||
relationships | ||
end | ||
|
||
# @api private | ||
def relationship_value_for(association, adapter_options, adapter_instance) | ||
return association.options[:virtual_value] if association.options[:virtual_value] | ||
association_serializer = association.serializer | ||
association_object = association_serializer && association_serializer.object | ||
return unless association_object | ||
|
||
relationship_value = association_serializer.serializable_hash(adapter_options, {}, adapter_instance) | ||
|
||
if association.options[:polymorphic] && relationship_value | ||
polymorphic_type = association_object.class.name.underscore | ||
relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value } | ||
end | ||
|
||
relationship_value | ||
end | ||
|
||
protected | ||
|
||
attr_accessor :instance_options | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
missing
@api private
. These methods are being held here for now, but may move around, be renamed, or disappear