From 2ce8455cc994c788a44f5b414769cb2dbee4577d Mon Sep 17 00:00:00 2001 From: Rosa Gutierrez Date: Sun, 27 Jan 2019 11:18:40 +0100 Subject: Support before_reset callback in CurrentAttributes This is useful when we need to do some work associated to `Current.reset` but that work depends on the values of the current attributes themselves. This cannot be done in the supported `resets` callback because when the block is executed, CurrentAttributes's instance has already been reset. For symmetry, `after_reset` is defined as alias of `resets`. --- activesupport/lib/active_support/current_attributes.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/current_attributes.rb b/activesupport/lib/active_support/current_attributes.rb index 3145ff87a1..67ebe102d7 100644 --- a/activesupport/lib/active_support/current_attributes.rb +++ b/activesupport/lib/active_support/current_attributes.rb @@ -119,10 +119,16 @@ module ActiveSupport end end + # Calls this block before #reset is called on the instance. Used for resetting external collaborators that depend on current values. + def before_reset(&block) + set_callback :reset, :before, &block + end + # Calls this block after #reset is called on the instance. Used for resetting external collaborators, like Time.zone. def resets(&block) set_callback :reset, :after, &block end + alias_method :after_reset, :resets delegate :set, :reset, to: :instance -- cgit v1.2.3