Improve comments; move caching concern to caching.rb

This commit is contained in:
Benjamin Fleischer 2017-03-16 10:14:18 -05:00
parent 36b4eac79b
commit 2e71bc47f4
2 changed files with 20 additions and 15 deletions

View File

@ -108,8 +108,8 @@ module ActiveModel
@serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes @serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes
end end
# Configuration options may also be set in # Preferred interface is ActiveModelSerializers.config
# Serializers and Adapters # BEGIN DEFAULT CONFIGURATION
config.collection_serializer = ActiveModel::Serializer::CollectionSerializer config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
config.serializer_lookup_enabled = true config.serializer_lookup_enabled = true
@ -159,6 +159,7 @@ module ActiveModel
config.serializer_lookup_chain = ActiveModelSerializers::LookupChain::DEFAULT.dup config.serializer_lookup_chain = ActiveModelSerializers::LookupChain::DEFAULT.dup
config.schema_path = 'test/support/schemas' config.schema_path = 'test/support/schemas'
# END DEFAULT CONFIGURATION
with_options instance_writer: false, instance_reader: false do |serializer| with_options instance_writer: false, instance_reader: false do |serializer|
serializer.class_attribute :_attributes_data # @api private serializer.class_attribute :_attributes_data # @api private
@ -180,12 +181,14 @@ module ActiveModel
base._links = _links.dup base._links = _links.dup
end end
# keys of attributes # @return [Array<Symbol>] Key names of declared attributes
# @see Serializer::attribute # @see Serializer::attribute
def self._attributes def self._attributes
_attributes_data.keys _attributes_data.keys
end end
# BEGIN SERIALIZER MACROS
# @example # @example
# class AdminAuthorSerializer < ActiveModel::Serializer # class AdminAuthorSerializer < ActiveModel::Serializer
# attributes :id, :name, :recent_edits # attributes :id, :name, :recent_edits
@ -214,18 +217,6 @@ module ActiveModel
_attributes_data[key] = Attribute.new(attr, options, block) _attributes_data[key] = Attribute.new(attr, options, block)
end end
# @api private
# maps attribute value to explicit key name
# @see Serializer::attribute
# @see ActiveModel::Serializer::Caching#fragmented_attributes
def self._attributes_keys
_attributes_data
.each_with_object({}) do |(key, attr), hash|
next if key == attr.name
hash[attr.name] = { key: key }
end
end
# @param [Symbol] name of the association # @param [Symbol] name of the association
# @param [Hash<Symbol => any>] options for the reflection # @param [Hash<Symbol => any>] options for the reflection
# @return [void] # @return [void]
@ -302,6 +293,8 @@ module ActiveModel
self._type = type && type.to_s self._type = type && type.to_s
end end
# END SERIALIZER MACROS
attr_accessor :object, :root, :scope attr_accessor :object, :root, :scope
# `scope_name` is set as :current_user by default in the controller. # `scope_name` is set as :current_user by default in the controller.

View File

@ -68,6 +68,18 @@ module ActiveModel
_cache_options && _cache_options[:skip_digest] _cache_options && _cache_options[:skip_digest]
end end
# @api private
# maps attribute value to explicit key name
# @see Serializer::attribute
# @see Serializer::fragmented_attributes
def _attributes_keys
_attributes_data
.each_with_object({}) do |(key, attr), hash|
next if key == attr.name
hash[attr.name] = { key: key }
end
end
def fragmented_attributes def fragmented_attributes
cached = _cache_only ? _cache_only : _attributes - _cache_except cached = _cache_only ? _cache_only : _attributes - _cache_except
cached = cached.map! { |field| _attributes_keys.fetch(field, field) } cached = cached.map! { |field| _attributes_keys.fetch(field, field) }