Rename FlattenJson to Attributes (allow plural adapter names)

This commit is contained in:
Benjamin Fleischer
2015-09-04 04:22:10 -05:00
parent ceef214f1e
commit c6f8d0f5f2
15 changed files with 36 additions and 35 deletions

View File

@@ -5,11 +5,11 @@ module ActiveModel
ADAPTER_MAP = {}
private_constant :ADAPTER_MAP if defined?(private_constant)
extend ActiveSupport::Autoload
autoload :Attributes
autoload :Null
autoload :FragmentCache
autoload :Json
autoload :JsonApi
autoload :Null
autoload :FlattenJson
autoload :CachedSerializer
def self.create(resource, options = {})
@@ -74,7 +74,8 @@ module ActiveModel
# @api private
def find_by_name(adapter_name)
adapter_name = adapter_name.to_s.classify.tr('API', 'Api')
ActiveModel::Serializer::Adapter.const_get(adapter_name.to_sym) or # rubocop:disable Style/AndOr
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize ||
"ActiveModel::Serializer::Adapter::#{adapter_name.pluralize}".safe_constantize or # rubocop:disable Style/AndOr
fail UnknownAdapterError
end
private :find_by_name

View File

@@ -1,8 +1,8 @@
class ActiveModel::Serializer::Adapter::FlattenJson < ActiveModel::Serializer::Adapter::Json
class ActiveModel::Serializer::Adapter::Attributes < ActiveModel::Serializer::Adapter
def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
result = serializer.map { |s| Attributes.new(s).serializable_hash(options) }
else
hash = {}
@@ -43,7 +43,7 @@ class ActiveModel::Serializer::Adapter::FlattenJson < ActiveModel::Serializer::A
private
# no-op: FlattenJson adapter does not include meta data, because it does not support root.
# no-op: Attributes adapter does not include meta data, because it does not support root.
def include_meta(json)
json
end

View File

@@ -4,7 +4,7 @@ class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter
def serializable_hash(options = nil)
options ||= {}
{ root => FlattenJson.new(serializer).serializable_hash(options) }
{ root => Attributes.new(serializer).serializable_hash(options) }
end
private

View File

@@ -6,7 +6,7 @@ module ActiveModel
included do |base|
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
base.config.adapter = :flatten_json
base.config.adapter = :attributes
base.config.jsonapi_resource_type = :plural
end
end