-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
a824376
commit 966c233
Showing
12 changed files
with
313 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
module ActiveModel | ||
class Serializer | ||
class FragmentCache | ||
|
||
attr_reader :serializer | ||
|
||
def initialize(adapter, serializer, options, root) | ||
@root = root | ||
@options = options | ||
@adapter = adapter | ||
@serializer = serializer | ||
end | ||
|
||
def fetch | ||
klass = serializer.class | ||
serializers = fragment_serializer(@serializer.object.class.name, klass) | ||
|
||
cached_serializer = serializers[:cached].constantize.new(@serializer.object) | ||
non_cached_serializer = serializers[:non_cached].constantize.new(@serializer.object) | ||
|
||
cached_hash = @adapter.class.new(cached_serializer, @options).serializable_hash | ||
non_cached_hash = @adapter.class.new(non_cached_serializer, @options).serializable_hash | ||
|
||
if @serializer.root && @adapter.class == ActiveModel::Serializer::Adapter::JsonApi | ||
cached_hash_root(cached_hash, non_cached_hash) | ||
else | ||
non_cached_hash.merge cached_hash | ||
end | ||
end | ||
|
||
private | ||
|
||
def cached_hash_root(cached_hash, non_cached_hash) | ||
hash = {} | ||
|
||
core_cached = cached_hash.first | ||
core_non_cached = non_cached_hash.first | ||
no_root_cache = cached_hash.delete_if {|key, value| key == core_cached[0] } | ||
no_root_non_cache = non_cached_hash.delete_if {|key, value| key == core_non_cached[0] } | ||
|
||
if @root | ||
hash[@root] = (core_cached[1]) ? core_cached[1].merge(core_non_cached[1]) : core_non_cached[1] | ||
else | ||
hash = (core_cached[1]) ? core_cached[1].merge(core_non_cached[1]) : core_non_cached[1] | ||
end | ||
hash.merge no_root_non_cache.merge no_root_cache | ||
end | ||
|
||
def fragment_associations(serializers, associations) | ||
associations.each do |association| | ||
options = ",#{association[1][:association_options]}" if association[1].include?(:association_options) | ||
eval("#{serializers[:non_cached]}.#{association[1][:type].to_s}(:#{association[0]}#{options})") | ||
end | ||
end | ||
|
||
def cached_attributes_and_association(klass, serializers) | ||
cached_attr = (klass._cache_only) ? klass._cache_only : @serializer.attributes.keys.delete_if {|attr| klass._cache_except.include?(attr) } | ||
non_cached_attr = @serializer.attributes.keys.delete_if {|attr| cached_attr.include?(attr) } | ||
associations = @serializer.each_association | ||
|
||
cached_attr.each do |attr| | ||
if @serializer.each_association.keys.include?(attr) | ||
associations.delete(attr) | ||
serializers[:cached].constantize.send(@serializer.each_association[attr][:type], attr) | ||
end | ||
end | ||
|
||
cached_attr.each do |attribute| | ||
options = @serializer.class._attributes_keys[attribute] | ||
options ||= {} | ||
serializers[:cached].constantize.attribute(attribute, options) | ||
end | ||
|
||
non_cached_attr.each do |attribute| | ||
options = @serializer.class._attributes_keys[attribute] | ||
options ||= {} | ||
serializers[:non_cached].constantize.attribute(attribute, options) | ||
end | ||
fragment_associations(serializers, associations) | ||
end | ||
|
||
def fragment_serializer(name, klass) | ||
cached = "#{name.capitalize}CachedSerializer" | ||
non_cached = "#{name.capitalize}NonCachedSerializer" | ||
|
||
Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached) | ||
Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached) | ||
|
||
klass._cache_options ||= {} | ||
klass._cache_options[:key] = klass._cache_key if klass._cache_key | ||
cached.constantize.cache(klass._cache_options) | ||
|
||
serializers = {cached: cached, non_cached: non_cached} | ||
cached_attributes_and_association(klass, serializers) | ||
return serializers | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.