From 2e71bc47f42a63389ea54fd5d2925d0b7dabc45e Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 16 Mar 2017 10:14:18 -0500 Subject: [PATCH] Improve comments; move caching concern to caching.rb --- lib/active_model/serializer.rb | 23 +++++++------------ .../serializer/concerns/caching.rb | 12 ++++++++++ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 597b34c8..d77eb7e3 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -108,8 +108,8 @@ module ActiveModel @serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes end - # Configuration options may also be set in - # Serializers and Adapters + # Preferred interface is ActiveModelSerializers.config + # BEGIN DEFAULT CONFIGURATION config.collection_serializer = ActiveModel::Serializer::CollectionSerializer config.serializer_lookup_enabled = true @@ -159,6 +159,7 @@ module ActiveModel config.serializer_lookup_chain = ActiveModelSerializers::LookupChain::DEFAULT.dup config.schema_path = 'test/support/schemas' + # END DEFAULT CONFIGURATION with_options instance_writer: false, instance_reader: false do |serializer| serializer.class_attribute :_attributes_data # @api private @@ -180,12 +181,14 @@ module ActiveModel base._links = _links.dup end - # keys of attributes + # @return [Array] Key names of declared attributes # @see Serializer::attribute def self._attributes _attributes_data.keys end + # BEGIN SERIALIZER MACROS + # @example # class AdminAuthorSerializer < ActiveModel::Serializer # attributes :id, :name, :recent_edits @@ -214,18 +217,6 @@ module ActiveModel _attributes_data[key] = Attribute.new(attr, options, block) 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 [Hash any>] options for the reflection # @return [void] @@ -302,6 +293,8 @@ module ActiveModel self._type = type && type.to_s end + # END SERIALIZER MACROS + attr_accessor :object, :root, :scope # `scope_name` is set as :current_user by default in the controller. diff --git a/lib/active_model/serializer/concerns/caching.rb b/lib/active_model/serializer/concerns/caching.rb index c8340787..3238adc8 100644 --- a/lib/active_model/serializer/concerns/caching.rb +++ b/lib/active_model/serializer/concerns/caching.rb @@ -68,6 +68,18 @@ module ActiveModel _cache_options && _cache_options[:skip_digest] 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 cached = _cache_only ? _cache_only : _attributes - _cache_except cached = cached.map! { |field| _attributes_keys.fetch(field, field) }