mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Adding Fragment Cache to AMS
It's an upgrade based on the new Cache implementation #693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
require 'active_model/serializer/adapter/json/fragment_cache'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
@@ -6,31 +8,45 @@ module ActiveModel
|
||||
if serializer.respond_to?(:each)
|
||||
@result = serializer.map{|s| self.class.new(s).serializable_hash }
|
||||
else
|
||||
@result = cached_object do
|
||||
@hash = serializer.attributes(options)
|
||||
serializer.each_association do |name, association, opts|
|
||||
if association.respond_to?(:each)
|
||||
array_serializer = association
|
||||
@hash[name] = array_serializer.map { |item| item.attributes(opts) }
|
||||
else
|
||||
if association
|
||||
@hash[name] = association.attributes(options)
|
||||
else
|
||||
@hash[name] = nil
|
||||
@hash = {}
|
||||
|
||||
@core = cache_check(serializer) do
|
||||
serializer.attributes(options)
|
||||
end
|
||||
|
||||
serializer.each_association do |name, association, opts|
|
||||
if association.respond_to?(:each)
|
||||
array_serializer = association
|
||||
@hash[name] = array_serializer.map do |item|
|
||||
cache_check(item) do
|
||||
item.attributes(opts)
|
||||
end
|
||||
end
|
||||
else
|
||||
if association
|
||||
@hash[name] = cache_check(association) do
|
||||
association.attributes(options)
|
||||
end
|
||||
elsif opts[:virtual_value]
|
||||
@hash[name] = opts[:virtual_value]
|
||||
else
|
||||
@hash[name] = nil
|
||||
end
|
||||
end
|
||||
@hash
|
||||
end
|
||||
@result = @core.merge @hash
|
||||
end
|
||||
|
||||
if root = options.fetch(:root, serializer.json_key)
|
||||
@result = { root => @result }
|
||||
end
|
||||
|
||||
@result
|
||||
end
|
||||
end
|
||||
|
||||
def fragment_cache(cached_hash, non_cached_hash)
|
||||
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user