mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Move caching to a new module
This commit is contained in:
parent
f179a27ed7
commit
1a8709d71c
@ -1,4 +1,5 @@
|
||||
require 'active_model/serializable'
|
||||
require 'active_model/serializer/caching'
|
||||
require "active_support/core_ext/class/attribute"
|
||||
require 'active_support/dependencies'
|
||||
require 'active_support/descendants_tracker'
|
||||
@ -17,6 +18,7 @@ module ActiveModel
|
||||
extend ActiveSupport::DescendantsTracker
|
||||
|
||||
include ActiveModel::Serializable
|
||||
include ActiveModel::Serializer::Caching
|
||||
|
||||
attr_reader :object, :options
|
||||
|
||||
@ -36,22 +38,11 @@ module ActiveModel
|
||||
@object, @options = object, options
|
||||
end
|
||||
|
||||
def serialize
|
||||
def serialize_object
|
||||
serializable_array
|
||||
end
|
||||
|
||||
def serializable_array
|
||||
if perform_caching?
|
||||
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-array']) do
|
||||
_serializable_array
|
||||
end
|
||||
else
|
||||
_serializable_array
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def _serializable_array
|
||||
@object.map do |item|
|
||||
if @options.has_key? :each_serializer
|
||||
serializer = @options[:each_serializer]
|
||||
@ -70,13 +61,5 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def expand_cache_key(*args)
|
||||
ActiveSupport::Cache.expand_cache_key(args)
|
||||
end
|
||||
|
||||
def perform_caching?
|
||||
perform_caching && cache && respond_to?(:cache_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -36,16 +36,6 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
def to_json(*args)
|
||||
if perform_caching?
|
||||
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
|
||||
super
|
||||
end
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def include_meta(hash)
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
require 'active_model/serializable'
|
||||
require 'active_model/serializer/caching'
|
||||
require "active_support/core_ext/class/attribute"
|
||||
require "active_support/core_ext/module/anonymous"
|
||||
require 'active_support/dependencies'
|
||||
@ -42,6 +43,7 @@ module ActiveModel
|
||||
extend ActiveSupport::DescendantsTracker
|
||||
|
||||
include ActiveModel::Serializable
|
||||
include ActiveModel::Serializer::Caching
|
||||
|
||||
INCLUDE_METHODS = {}
|
||||
INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" }
|
||||
@ -73,7 +75,6 @@ module ActiveModel
|
||||
class_attribute :perform_caching
|
||||
|
||||
class << self
|
||||
# set perform caching like root
|
||||
def cached(value = true)
|
||||
self.perform_caching = value
|
||||
end
|
||||
@ -325,20 +326,17 @@ module ActiveModel
|
||||
super(root: args.fetch(:root, options.fetch(:root, root_name)))
|
||||
end
|
||||
|
||||
def serialize
|
||||
def serialize_object
|
||||
serializable_hash
|
||||
end
|
||||
|
||||
# Returns a hash representation of the serializable
|
||||
# object without the root.
|
||||
def serializable_hash
|
||||
if perform_caching?
|
||||
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-hash']) do
|
||||
_serializable_hash
|
||||
end
|
||||
else
|
||||
_serializable_hash
|
||||
end
|
||||
return nil if @object.nil?
|
||||
@node = attributes
|
||||
include_associations! if _embed
|
||||
@node
|
||||
end
|
||||
|
||||
def include_associations!
|
||||
@ -453,21 +451,6 @@ module ActiveModel
|
||||
|
||||
alias :read_attribute_for_serialization :send
|
||||
|
||||
def _serializable_hash
|
||||
return nil if @object.nil?
|
||||
@node = attributes
|
||||
include_associations! if _embed
|
||||
@node
|
||||
end
|
||||
|
||||
def perform_caching?
|
||||
perform_caching && cache && respond_to?(:cache_key)
|
||||
end
|
||||
|
||||
def expand_cache_key(*args)
|
||||
ActiveSupport::Cache.expand_cache_key(args)
|
||||
end
|
||||
|
||||
# Use ActiveSupport::Notifications to send events to external systems.
|
||||
# The event name is: name.class_name.serializer
|
||||
def instrument(name, payload = {}, &block)
|
||||
|
||||
37
lib/active_model/serializer/caching.rb
Normal file
37
lib/active_model/serializer/caching.rb
Normal file
@ -0,0 +1,37 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Caching
|
||||
def to_json(*args)
|
||||
if caching_enabled?
|
||||
key = expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json'])
|
||||
cache.fetch key do
|
||||
super
|
||||
end
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def serialize(*args)
|
||||
if caching_enabled?
|
||||
key = expand_cache_key([self.class.to_s.underscore, cache_key, 'serialize'])
|
||||
cache.fetch key do
|
||||
serialize_object
|
||||
end
|
||||
else
|
||||
serialize_object
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def caching_enabled?
|
||||
perform_caching && cache && respond_to?(:cache_key)
|
||||
end
|
||||
|
||||
def expand_cache_key(*args)
|
||||
ActiveSupport::Cache.expand_cache_key(args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -68,7 +68,7 @@ class CachingTest < ActiveModel::TestCase
|
||||
|
||||
instance.to_json
|
||||
|
||||
assert_equal(instance.serializable_hash, serializer.cache.read('serializer/Adam/serializable-hash'))
|
||||
assert_equal(instance.serializable_hash, serializer.cache.read('serializer/Adam/serialize'))
|
||||
assert_equal(instance.to_json, serializer.cache.read('serializer/Adam/to-json'))
|
||||
end
|
||||
|
||||
@ -90,7 +90,7 @@ class CachingTest < ActiveModel::TestCase
|
||||
|
||||
instance.to_json
|
||||
|
||||
assert_equal instance.serializable_array, serializer.cache.read('array_serializer/cache-key/serializable-array')
|
||||
assert_equal instance.serializable_array, serializer.cache.read('array_serializer/cache-key/serialize')
|
||||
assert_equal instance.to_json, serializer.cache.read('array_serializer/cache-key/to-json')
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user