Skip to content

Commit 2e71bc4

Browse files
committed
Improve comments; move caching concern to caching.rb
1 parent 36b4eac commit 2e71bc4

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

lib/active_model/serializer.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ def self.serialization_adapter_instance
108108
@serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes
109109
end
110110

111-
# Configuration options may also be set in
112-
# Serializers and Adapters
111+
# Preferred interface is ActiveModelSerializers.config
112+
# BEGIN DEFAULT CONFIGURATION
113113
config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
114114
config.serializer_lookup_enabled = true
115115

@@ -159,6 +159,7 @@ def config.array_serializer
159159
config.serializer_lookup_chain = ActiveModelSerializers::LookupChain::DEFAULT.dup
160160

161161
config.schema_path = 'test/support/schemas'
162+
# END DEFAULT CONFIGURATION
162163

163164
with_options instance_writer: false, instance_reader: false do |serializer|
164165
serializer.class_attribute :_attributes_data # @api private
@@ -180,12 +181,14 @@ def self.inherited(base)
180181
base._links = _links.dup
181182
end
182183

183-
# keys of attributes
184+
# @return [Array<Symbol>] Key names of declared attributes
184185
# @see Serializer::attribute
185186
def self._attributes
186187
_attributes_data.keys
187188
end
188189

190+
# BEGIN SERIALIZER MACROS
191+
189192
# @example
190193
# class AdminAuthorSerializer < ActiveModel::Serializer
191194
# attributes :id, :name, :recent_edits
@@ -214,18 +217,6 @@ def self.attribute(attr, options = {}, &block)
214217
_attributes_data[key] = Attribute.new(attr, options, block)
215218
end
216219

217-
# @api private
218-
# maps attribute value to explicit key name
219-
# @see Serializer::attribute
220-
# @see ActiveModel::Serializer::Caching#fragmented_attributes
221-
def self._attributes_keys
222-
_attributes_data
223-
.each_with_object({}) do |(key, attr), hash|
224-
next if key == attr.name
225-
hash[attr.name] = { key: key }
226-
end
227-
end
228-
229220
# @param [Symbol] name of the association
230221
# @param [Hash<Symbol => any>] options for the reflection
231222
# @return [void]
@@ -302,6 +293,8 @@ def self.type(type)
302293
self._type = type && type.to_s
303294
end
304295

296+
# END SERIALIZER MACROS
297+
305298
attr_accessor :object, :root, :scope
306299

307300
# `scope_name` is set as :current_user by default in the controller.

lib/active_model/serializer/concerns/caching.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ def _skip_digest?
6868
_cache_options && _cache_options[:skip_digest]
6969
end
7070

71+
# @api private
72+
# maps attribute value to explicit key name
73+
# @see Serializer::attribute
74+
# @see Serializer::fragmented_attributes
75+
def _attributes_keys
76+
_attributes_data
77+
.each_with_object({}) do |(key, attr), hash|
78+
next if key == attr.name
79+
hash[attr.name] = { key: key }
80+
end
81+
end
82+
7183
def fragmented_attributes
7284
cached = _cache_only ? _cache_only : _attributes - _cache_except
7385
cached = cached.map! { |field| _attributes_keys.fetch(field, field) }

0 commit comments

Comments
 (0)