Skip to content

Commit ed19043

Browse files
committed
Document AMS::Model::Caching
1 parent 097d7d8 commit ed19043

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/active_model_serializers/model.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
# Caching support is by-default mixed in when subclassing.
66
# To disable mixing in any caching support,
77
# set ActiveModelSerializers::Model.add_caching_support = false
8+
#
9+
# To generally use caching but have a noncaching subclass, you may:
10+
# ActiveModelSerializers::Model.add_caching_support = false
11+
# class NonCachingPoro < ActiveModelSerializers::Model
12+
# end
13+
# ActiveModelSerializers::Model.add_caching_support = true
14+
#
15+
# All sublasses of NonCachingPoro will have +add_caching_support+ false
16+
# while subclass of ActiveModelSerializers::Model will continue to mix in the
17+
# caching module.
18+
#
19+
# Future version of ActiveModelSerializers may change the default o make this simpler.
820
module ActiveModelSerializers
921
class Model
1022
include ActiveModel::Serializers::JSON
@@ -13,8 +25,11 @@ class Model
1325
autoload :Caching
1426

1527
class_attribute :add_caching_support
28+
# By default, include caching support methods in all subclasses.
1629
self.add_caching_support = true
1730
class_attribute :attribute_names
31+
# Initialize +attribute_names+ for all subclasses. The array is usually
32+
# mutated in the +attributes+ method, but can be set directly, as well.
1833
self.attribute_names = []
1934

2035
def self.attributes(*names)
@@ -39,6 +54,9 @@ def initialize(attributes = {})
3954
super
4055
end
4156

57+
# The the fields in +attribute_names+ determines the returned hash.
58+
# +attributes+ are returned frozen to prevent any expectations that mutation affects
59+
# the actual values in the model.
4260
def attributes
4361
attribute_names.each_with_object({}) do |attribute_name, result|
4462
result[attribute_name] = public_send(attribute_name).freeze

lib/active_model_serializers/model/caching.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@ module Caching
44
extend ActiveSupport::Concern
55

66
included do
7+
# NOTE that +updated_at+ isn't included in +attribute_names+,
8+
# which means it won't show up in +attributes+ unless a subclass has
9+
# either <tt>attributes :updated_at</tt> which will redefine the methods
10+
# or <tt>attribute_names << :updated_at</tt>.
711
attr_writer :updated_at
12+
# NOTE that +id+ will always be in +attributes+.
813
attributes :id
914
end
1015

11-
# Defaults to the downcased model name and updated_at
16+
# To customize model behavior, this method must be redefined. However,
17+
# there are other ways of setting the +cache_key+ a serializer uses.
1218
def cache_key
1319
ActiveSupport::Cache.expand_cache_key([
1420
self.class.model_name.name.downcase,
1521
"#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
1622
].compact)
1723
end
1824

19-
# Defaults to the time the serializer file was modified.
25+
# When no set, defaults to the time the file was modified.
26+
# See NOTE in included block.
2027
def updated_at
2128
defined?(@updated_at) ? @updated_at : File.mtime(__FILE__)
2229
end

0 commit comments

Comments
 (0)