mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
This adds namespace lookup to serializer_for (#1968)
* This adds namespace lookup to serializer_for * address rubocop issue * address @bf4's feedback * add docs * update docs, add more tests * apparently rails master doesn't have before filter * try to address serializer cache issue between tests * update cache for serializer lookup to include namespace in the key, and fix the tests for explicit namespace * update docs, and use better cache key creation method * update docs [ci skip] * update docs [ci skip] * add to changelog [ci skip]
This commit is contained in:
committed by
GitHub
parent
b709cd41e6
commit
b29395b0ac
@@ -16,6 +16,12 @@ module ActionController
|
||||
included do
|
||||
class_attribute :_serialization_scope
|
||||
self._serialization_scope = :current_user
|
||||
|
||||
attr_writer :namespace_for_serializer
|
||||
end
|
||||
|
||||
def namespace_for_serializer
|
||||
@namespace_for_serializer ||= self.class.parent unless self.class.parent == Object
|
||||
end
|
||||
|
||||
def serialization_scope
|
||||
@@ -30,6 +36,9 @@ module ActionController
|
||||
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
|
||||
options[:adapter] = false
|
||||
end
|
||||
|
||||
options.fetch(:namespace) { options[:namespace] = namespace_for_serializer }
|
||||
|
||||
serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)
|
||||
serializable_resource.serialization_scope ||= options.fetch(:scope) { serialization_scope }
|
||||
serializable_resource.serialization_scope_name = options.fetch(:scope_name) { _serialization_scope }
|
||||
|
||||
@@ -44,7 +44,7 @@ module ActiveModel
|
||||
elsif resource.respond_to?(:to_ary)
|
||||
config.collection_serializer
|
||||
else
|
||||
options.fetch(:serializer) { get_serializer_for(resource.class) }
|
||||
options.fetch(:serializer) { get_serializer_for(resource.class, options[:namespace]) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -59,13 +59,14 @@ module ActiveModel
|
||||
end
|
||||
|
||||
# @api private
|
||||
def self.serializer_lookup_chain_for(klass)
|
||||
def self.serializer_lookup_chain_for(klass, namespace = nil)
|
||||
chain = []
|
||||
|
||||
resource_class_name = klass.name.demodulize
|
||||
resource_namespace = klass.name.deconstantize
|
||||
serializer_class_name = "#{resource_class_name}Serializer"
|
||||
|
||||
chain.push("#{namespace}::#{serializer_class_name}") if namespace
|
||||
chain.push("#{name}::#{serializer_class_name}") if self != ActiveModel::Serializer
|
||||
chain.push("#{resource_namespace}::#{serializer_class_name}")
|
||||
|
||||
@@ -84,11 +85,14 @@ module ActiveModel
|
||||
# 1. class name appended with "Serializer"
|
||||
# 2. try again with superclass, if present
|
||||
# 3. nil
|
||||
def self.get_serializer_for(klass)
|
||||
def self.get_serializer_for(klass, namespace = nil)
|
||||
return nil unless config.serializer_lookup_enabled
|
||||
serializers_cache.fetch_or_store(klass) do
|
||||
|
||||
cache_key = ActiveSupport::Cache.expand_cache_key(klass, namespace)
|
||||
serializers_cache.fetch_or_store(cache_key) do
|
||||
# NOTE(beauby): When we drop 1.9.3 support we can lazify the map for perfs.
|
||||
serializer_class = serializer_lookup_chain_for(klass).map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }
|
||||
lookup_chain = serializer_lookup_chain_for(klass, namespace)
|
||||
serializer_class = lookup_chain.map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }
|
||||
|
||||
if serializer_class
|
||||
serializer_class
|
||||
|
||||
@@ -106,6 +106,10 @@ module ActiveModel
|
||||
#
|
||||
def build_association(parent_serializer, parent_serializer_options, include_slice = {})
|
||||
reflection_options = options.dup
|
||||
|
||||
# Pass the parent's namespace onto the child serializer
|
||||
reflection_options[:namespace] ||= parent_serializer_options[:namespace]
|
||||
|
||||
association_value = value(parent_serializer, include_slice)
|
||||
serializer_class = parent_serializer.class.serializer_for(association_value, reflection_options)
|
||||
reflection_options[:include_data] = include_data?(include_slice)
|
||||
|
||||
@@ -55,7 +55,7 @@ module ActiveModelSerializers
|
||||
@serializer ||=
|
||||
begin
|
||||
@serializer = serializer_opts.delete(:serializer)
|
||||
@serializer ||= ActiveModel::Serializer.serializer_for(resource)
|
||||
@serializer ||= ActiveModel::Serializer.serializer_for(resource, serializer_opts)
|
||||
|
||||
if serializer_opts.key?(:each_serializer)
|
||||
serializer_opts[:serializer] = serializer_opts.delete(:each_serializer)
|
||||
|
||||
Reference in New Issue
Block a user