-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate serialization in ActiveModel::Serializer::Builder
Usage: ActiveModel::Serializer.build(resource, options)
- Loading branch information
Showing
7 changed files
with
125 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require "set" | ||
module ActiveModel | ||
class Serializer | ||
class Builder | ||
extend ActiveSupport::Autoload | ||
|
||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :root, :adapter]) | ||
|
||
def initialize(resource, options = {}) | ||
@resource = resource | ||
@adapter_opts, @serializer_opts = | ||
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] } | ||
end | ||
|
||
def serialization_scope=(scope) | ||
serializer_opts[:scope] = scope | ||
end | ||
|
||
def serialization_scope | ||
serializer_opts[:scope] | ||
end | ||
|
||
def serialization_scope_name=(scope_name) | ||
serializer_opts[:scope_name] = scope_name | ||
end | ||
|
||
def adapter | ||
@adapter ||= ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts) | ||
end | ||
alias_method :adapter_instance, :adapter | ||
|
||
def serializer_instance | ||
@serializer_instance ||= serializer.new(resource, serializer_opts) | ||
end | ||
|
||
# Get serializer either explicitly :serializer or implicitly from resource | ||
# Remove :serializer key from serializer_opts | ||
# Replace :serializer key with :each_serializer if present | ||
def serializer | ||
@serializer ||= | ||
begin | ||
@serializer = serializer_opts.delete(:serializer) | ||
@serializer ||= ActiveModel::Serializer.serializer_for(resource) | ||
|
||
if serializer_opts.key?(:each_serializer) | ||
serializer_opts[:serializer] = serializer_opts.delete(:each_serializer) | ||
end | ||
@serializer | ||
end | ||
end | ||
alias_method :serializer_class, :serializer | ||
|
||
# True when no explicit adapter given, or explicit appear is truthy (non-nil) | ||
# False when explicit adapter is falsy (nil or false) | ||
def use_adapter? | ||
!(adapter_opts.key?(:adapter) && !adapter_opts[:adapter]) | ||
end | ||
|
||
def serializer? | ||
use_adapter? && !!(serializer) | ||
end | ||
|
||
private | ||
|
||
attr_reader :resource, :adapter_opts, :serializer_opts | ||
|
||
end | ||
end | ||
end |
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