Skip to content

Commit dd60a37

Browse files
committed
Simplify caching of value of config.perform_caching
1 parent 1230dd9 commit dd60a37

File tree

1 file changed

+7
-23
lines changed

1 file changed

+7
-23
lines changed

lib/active_model/serializer/caching.rb

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,34 +99,18 @@ def cache(options = {})
9999
self._cache_options = options.empty? ? nil : options
100100
end
101101

102+
# Value is from ActiveModelSerializers.config.perform_caching. Is used to
103+
# globally enable or disable all serializer caching, just like
104+
# Rails.configuration.action_controller.perform_caching, which is its
105+
# default value in a Rails application.
102106
# @return [true, false]
103-
# We're using class variables here because it is a class attribute
104-
# that is globally true for the `ActiveModel::Serializer` class; i.e. neither per subclass nor inherited.
105-
#
106-
# We're not using a class_attribute because of the special behavior in
107-
# `perform_caching` setting itself to `ActiveModelSerializers.config.perform_caching`
108-
# when first called if it wasn't first set.
109-
#
110-
# This is to allow us to have a global config that can be set any time before
111-
# `perform_caching` is called.
112-
#
113-
# One downside of this, is that subsequent setting of the global config will not change
114-
# `ActiveModel::Serializer.perform_caching`, but that should be an edge case that
115-
# is easily handled.
116-
#
117-
# If you, reading this, can figure out how to have ActiveModel::Serializer always delegate
118-
# `perform_caching` and `perform_caching=` to the global config, that would make a nice PR.
107+
# Memoizes value of config first time it is called with a non-nil value.
119108
# rubocop:disable Style/ClassVars
120109
def perform_caching
121-
return @@perform_caching if defined?(@@perform_caching)
122-
self.perform_caching = ActiveModelSerializers.config.perform_caching
110+
return @@perform_caching if defined?(@@perform_caching) && !@@perform_caching.nil?
111+
@@perform_caching = ActiveModelSerializers.config.perform_caching
123112
end
124113
alias perform_caching? perform_caching
125-
126-
# @param [true, false]
127-
def perform_caching=(perform_caching)
128-
@@perform_caching = perform_caching
129-
end
130114
# rubocop:enable Style/ClassVars
131115

132116
# The canonical method for getting the cache store for the serializer.

0 commit comments

Comments
 (0)