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.
820module 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
0 commit comments