|
| 1 | +module ActiveModelSerializers |
| 2 | + class FragmentCache |
| 3 | + attr_reader :serializer |
| 4 | + |
| 5 | + def initialize(adapter, serializer, options) |
| 6 | + @instance_options = options |
| 7 | + @adapter = adapter |
| 8 | + @serializer = serializer |
| 9 | + end |
| 10 | + |
| 11 | + # TODO: Use Serializable::Resource |
| 12 | + # TODO: call +constantize+ less |
| 13 | + # 1. Create a CachedSerializer and NonCachedSerializer from the serializer class |
| 14 | + # 2. Serialize the above two with the given adapter |
| 15 | + # 3. Pass their serializations to the adapter +::fragment_cache+ |
| 16 | + def fetch |
| 17 | + klass = serializer.class |
| 18 | + # It will split the serializer into two, one that will be cached and one that will not |
| 19 | + serializers = fragment_serializer(serializer.object.class.name, klass) |
| 20 | + |
| 21 | + # Instantiate both serializers |
| 22 | + cached_serializer = serializers[:cached].constantize.new(serializer.object) |
| 23 | + non_cached_serializer = serializers[:non_cached].constantize.new(serializer.object) |
| 24 | + |
| 25 | + cached_adapter = adapter.class.new(cached_serializer, instance_options) |
| 26 | + non_cached_adapter = adapter.class.new(non_cached_serializer, instance_options) |
| 27 | + |
| 28 | + # Get serializable hash from both |
| 29 | + cached_hash = cached_adapter.serializable_hash |
| 30 | + non_cached_hash = non_cached_adapter.serializable_hash |
| 31 | + |
| 32 | + # Merge both results |
| 33 | + adapter.fragment_cache(cached_hash, non_cached_hash) |
| 34 | + end |
| 35 | + |
| 36 | + protected |
| 37 | + |
| 38 | + attr_reader :instance_options, :adapter |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + # Given a serializer class and a hash of its cached and non-cached serializers |
| 43 | + # 1. Determine cached attributes from serializer class options |
| 44 | + # 2. Add cached attributes to cached Serializer |
| 45 | + # 3. Add non-cached attributes to non-cached Serializer |
| 46 | + def cached_attributes(klass, serializers) |
| 47 | + attributes = serializer.class._attributes |
| 48 | + cached_attributes = (klass._cache_only) ? klass._cache_only : attributes.reject { |attr| klass._cache_except.include?(attr) } |
| 49 | + non_cached_attributes = attributes - cached_attributes |
| 50 | + |
| 51 | + cached_attributes.each do |attribute| |
| 52 | + options = serializer.class._attributes_keys[attribute] |
| 53 | + options ||= {} |
| 54 | + # Add cached attributes to cached Serializer |
| 55 | + serializers[:cached].constantize.attribute(attribute, options) |
| 56 | + end |
| 57 | + |
| 58 | + non_cached_attributes.each do |attribute| |
| 59 | + options = serializer.class._attributes_keys[attribute] |
| 60 | + options ||= {} |
| 61 | + # Add non-cached attributes to non-cached Serializer |
| 62 | + serializers[:non_cached].constantize.attribute(attribute, options) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + # Given a resource name and its serializer's class |
| 67 | + # 1. Dyanmically creates a CachedSerializer and NonCachedSerializer |
| 68 | + # for a given class 'name' |
| 69 | + # 2. Call |
| 70 | + # CachedSerializer.cache(serializer._cache_options) |
| 71 | + # CachedSerializer.fragmented(serializer) |
| 72 | + # NonCachedSerializer.cache(serializer._cache_options) |
| 73 | + # 3. Build a hash keyed to the +cached+ and +non_cached+ serializers |
| 74 | + # 4. Call +cached_attributes+ on the serializer class and the above hash |
| 75 | + # 5. Return the hash |
| 76 | + # |
| 77 | + # @example |
| 78 | + # When +name+ is <tt>User::Admin</tt> |
| 79 | + # creates the Serializer classes (if they don't exist). |
| 80 | + # User_AdminCachedSerializer |
| 81 | + # User_AdminNonCachedSerializer |
| 82 | + # |
| 83 | + def fragment_serializer(name, klass) |
| 84 | + cached = "#{to_valid_const_name(name)}CachedSerializer" |
| 85 | + non_cached = "#{to_valid_const_name(name)}NonCachedSerializer" |
| 86 | + |
| 87 | + Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached) |
| 88 | + Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached) |
| 89 | + |
| 90 | + klass._cache_options ||= {} |
| 91 | + klass._cache_options[:key] = klass._cache_key if klass._cache_key |
| 92 | + |
| 93 | + cached.constantize.cache(klass._cache_options) |
| 94 | + |
| 95 | + cached.constantize.fragmented(serializer) |
| 96 | + non_cached.constantize.fragmented(serializer) |
| 97 | + |
| 98 | + serializers = { cached: cached, non_cached: non_cached } |
| 99 | + cached_attributes(klass, serializers) |
| 100 | + serializers |
| 101 | + end |
| 102 | + |
| 103 | + def to_valid_const_name(name) |
| 104 | + name.gsub('::', '_') |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments