-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Move SerializableResource to ActiveModelSerializers namespace #1608
Merged
NullVoxPopuli
merged 1 commit into
rails-api:master
from
groyoh:move_serializable_resource
Mar 30, 2016
Merged
Changes from all commits
Commits
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 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
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 |
---|---|---|
@@ -1,81 +1,11 @@ | ||
require 'set' | ||
require 'active_model_serializers/adapter' | ||
|
||
module ActiveModel | ||
class SerializableResource | ||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links]) | ||
include ActiveModelSerializers::Logging | ||
|
||
delegate :serializable_hash, :as_json, :to_json, to: :adapter | ||
notify :serializable_hash, :render | ||
notify :as_json, :render | ||
notify :to_json, :render | ||
|
||
# Primary interface to composing a resource with a serializer and adapter. | ||
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash. | ||
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 | ||
|
||
# NOTE: if no adapter is available, returns the resource itself. (i.e. adapter is a no-op) | ||
def adapter | ||
@adapter ||= find_adapter | ||
end | ||
alias adapter_instance adapter | ||
class << self | ||
extend ActiveModelSerializers::Deprecate | ||
|
||
def find_adapter | ||
return resource unless serializer? | ||
ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts) | ||
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError | ||
resource | ||
delegate_and_deprecate :new, ActiveModelSerializers::SerializableResource | ||
end | ||
|
||
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 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 | ||
|
||
protected | ||
|
||
attr_reader :resource, :adapter_opts, :serializer_opts | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,24 @@ | ||
require 'active_model_serializers/adapter' | ||
require 'active_model_serializers/deprecate' | ||
|
||
module ActiveModel | ||
class Serializer | ||
# @deprecated Use ActiveModelSerializers::Adapter instead | ||
module Adapter | ||
class << self | ||
extend ActiveModelSerializers::Deprecate | ||
|
||
def self.delegate_and_deprecate(method) | ||
delegate method, to: ActiveModelSerializers::Adapter | ||
deprecate method, 'ActiveModelSerializers::Adapter.' | ||
DEPRECATED_METHODS = [:create, :adapter_class, :adapter_map, :adapters, :register, :lookup].freeze | ||
DEPRECATED_METHODS.each do |method| | ||
delegate_and_deprecate method, ActiveModelSerializers::Adapter | ||
end | ||
private_class_method :delegate_and_deprecate | ||
|
||
delegate_and_deprecate :create | ||
delegate_and_deprecate :adapter_class | ||
delegate_and_deprecate :adapter_map | ||
delegate_and_deprecate :adapters | ||
delegate_and_deprecate :register | ||
delegate_and_deprecate :lookup | ||
end | ||
|
||
require 'active_model/serializer/adapter/base' | ||
require 'active_model/serializer/adapter/null' | ||
require 'active_model/serializer/adapter/attributes' | ||
require 'active_model/serializer/adapter/json' | ||
require 'active_model/serializer/adapter/json_api' | ||
end | ||
end | ||
end | ||
|
||
require 'active_model/serializer/adapter/base' | ||
require 'active_model/serializer/adapter/null' | ||
require 'active_model/serializer/adapter/attributes' | ||
require 'active_model/serializer/adapter/json' | ||
require 'active_model/serializer/adapter/json_api' |
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
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,81 @@ | ||
require 'set' | ||
|
||
module ActiveModelSerializers | ||
class SerializableResource | ||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links]) | ||
include ActiveModelSerializers::Logging | ||
|
||
delegate :serializable_hash, :as_json, :to_json, to: :adapter | ||
notify :serializable_hash, :render | ||
notify :as_json, :render | ||
notify :to_json, :render | ||
|
||
# Primary interface to composing a resource with a serializer and adapter. | ||
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash. | ||
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 | ||
|
||
# NOTE: if no adapter is available, returns the resource itself. (i.e. adapter is a no-op) | ||
def adapter | ||
@adapter ||= find_adapter | ||
end | ||
alias adapter_instance adapter | ||
|
||
def find_adapter | ||
return resource unless serializer? | ||
ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts) | ||
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError | ||
resource | ||
end | ||
|
||
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 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.nil? | ||
end | ||
|
||
protected | ||
|
||
attr_reader :resource, :adapter_opts, :serializer_opts | ||
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
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.
I figured out that the relative urls links to rails-api repo instead of mine so the link does not work.
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.
link with this change looks fine
https://github.com/groyoh/active_model_serializers/blob/move_serializable_resource/lib/active_model_serializers/serializable_resource.rb#L5