mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Provide convenience serializer_class for all the self.class calls
per groyoh https://github.com/rails-api/active_model_serializers/pull/1781#discussion_r66021340
This commit is contained in:
parent
5375e009e2
commit
b599360ae3
@ -18,7 +18,7 @@ module ActiveModel
|
|||||||
# race_condition_ttl
|
# race_condition_ttl
|
||||||
# Passed to ::_cache as
|
# Passed to ::_cache as
|
||||||
# serializer.cache_store.fetch(cache_key, @klass._cache_options)
|
# serializer.cache_store.fetch(cache_key, @klass._cache_options)
|
||||||
# Passed as second argument to serializer.cache_store.fetch(cache_key, self.class._cache_options)
|
# Passed as second argument to serializer.cache_store.fetch(cache_key, serializer_class._cache_options)
|
||||||
serializer.class_attribute :_cache_digest_file_path # @api private : Derived at inheritance
|
serializer.class_attribute :_cache_digest_file_path # @api private : Derived at inheritance
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -203,23 +203,23 @@ module ActiveModel
|
|||||||
|
|
||||||
### INSTANCE METHODS
|
### INSTANCE METHODS
|
||||||
def fetch_attributes(fields, cached_attributes, adapter_instance)
|
def fetch_attributes(fields, cached_attributes, adapter_instance)
|
||||||
if self.class.cache_enabled?
|
if serializer_class.cache_enabled?
|
||||||
key = cache_key(adapter_instance)
|
key = cache_key(adapter_instance)
|
||||||
cached_attributes.fetch(key) do
|
cached_attributes.fetch(key) do
|
||||||
self.class.cache_store.fetch(key, self.class._cache_options) do
|
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
||||||
attributes(fields, true)
|
attributes(fields, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elsif self.class.fragment_cache_enabled?
|
elsif serializer_class.fragment_cache_enabled?
|
||||||
fetch_attributes_fragment(adapter_instance)
|
fetch_attributes_fragment(adapter_instance)
|
||||||
else
|
else
|
||||||
attributes(fields, true)
|
attributes(fields, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch(adapter_instance, cache_options = self.class._cache_options)
|
def fetch(adapter_instance, cache_options = serializer_class._cache_options)
|
||||||
if self.class.cache_store
|
if serializer_class.cache_store
|
||||||
self.class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
|
serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@ -231,9 +231,9 @@ module ActiveModel
|
|||||||
# 2. Get non_cached_fields and fetch cache_fields
|
# 2. Get non_cached_fields and fetch cache_fields
|
||||||
# 3. Merge the two hashes using adapter_instance#fragment_cache
|
# 3. Merge the two hashes using adapter_instance#fragment_cache
|
||||||
def fetch_attributes_fragment(adapter_instance)
|
def fetch_attributes_fragment(adapter_instance)
|
||||||
self.class._cache_options ||= {}
|
serializer_class._cache_options ||= {}
|
||||||
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
|
serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
|
||||||
fields = self.class.fragmented_attributes
|
fields = serializer_class.fragmented_attributes
|
||||||
|
|
||||||
non_cached_fields = fields[:non_cached].dup
|
non_cached_fields = fields[:non_cached].dup
|
||||||
non_cached_hash = attributes(non_cached_fields, true)
|
non_cached_hash = attributes(non_cached_fields, true)
|
||||||
@ -243,7 +243,7 @@ module ActiveModel
|
|||||||
cached_fields = fields[:cached].dup
|
cached_fields = fields[:cached].dup
|
||||||
key = cache_key(adapter_instance)
|
key = cache_key(adapter_instance)
|
||||||
cached_hash =
|
cached_hash =
|
||||||
self.class.cache_store.fetch(key, self.class._cache_options) do
|
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
||||||
hash = attributes(cached_fields, true)
|
hash = attributes(cached_fields, true)
|
||||||
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
||||||
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
||||||
@ -259,7 +259,7 @@ module ActiveModel
|
|||||||
parts = []
|
parts = []
|
||||||
parts << object_cache_key
|
parts << object_cache_key
|
||||||
parts << adapter_instance.cache_key
|
parts << adapter_instance.cache_key
|
||||||
parts << self.class._cache_digest unless self.class._skip_digest?
|
parts << serializer_class._cache_digest unless serializer_class._skip_digest?
|
||||||
@cache_key = parts.join('/')
|
@cache_key = parts.join('/')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -268,14 +268,18 @@ module ActiveModel
|
|||||||
def object_cache_key
|
def object_cache_key
|
||||||
if object.respond_to?(:cache_key)
|
if object.respond_to?(:cache_key)
|
||||||
object.cache_key
|
object.cache_key
|
||||||
elsif (serializer_cache_key = (self.class._cache_key || self.class._cache_options[:key]))
|
elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class._cache_options[:key]))
|
||||||
object_time_safe = object.updated_at
|
object_time_safe = object.updated_at
|
||||||
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
|
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
|
||||||
"#{serializer_cache_key}/#{object.id}-#{object_time_safe}"
|
"#{serializer_cache_key}/#{object.id}-#{object_time_safe}"
|
||||||
else
|
else
|
||||||
fail UndefinedCacheKey, "#{object.class} must define #cache_key, or the 'key:' option must be passed into '#{self.class}.cache'"
|
fail UndefinedCacheKey, "#{object.class} must define #cache_key, or the 'key:' option must be passed into '#{serializer_class}.cache'"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def serializer_class
|
||||||
|
@serializer_class ||= self.class
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user