Skip to content

Commit 994bce4

Browse files
committed
Merge PR rails#45698
2 parents f2bbe6e + be155f1 commit 994bce4

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

activesupport/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
* `ActiveSupport::CurrentAttributes.resets` now accepts a method name
2+
3+
The block API is still the recommended approach, but now both APIs are supported:
4+
5+
```ruby
6+
class Current < ActiveSupport::CurrentAttributes
7+
resets { Time.zone = nil }
8+
resets :clear_time_zone
9+
end
10+
```
11+
12+
*Alex Ghiculescu*
13+
114
* Ensure `ActiveSupport::Testing::Isolation::Forking` closes pipes
215

316
Previously, `Forking.run_in_isolation` opened two ends of a pipe. The fork

activesupport/lib/active_support/current_attributes.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ def attribute(*names)
133133
end
134134
end
135135

136-
# Calls this block before #reset is called on the instance. Used for resetting external collaborators that depend on current values.
137-
def before_reset(&block)
138-
set_callback :reset, :before, &block
136+
# Calls this callback before #reset is called on the instance. Used for resetting external collaborators that depend on current values.
137+
def before_reset(*methods, &block)
138+
set_callback :reset, :before, *methods, &block
139139
end
140140

141-
# Calls this block after #reset is called on the instance. Used for resetting external collaborators, like Time.zone.
142-
def resets(&block)
143-
set_callback :reset, :after, &block
141+
# Calls this callback after #reset is called on the instance. Used for resetting external collaborators, like Time.zone.
142+
def resets(*methods, &block)
143+
set_callback :reset, :after, *methods, &block
144144
end
145145
alias_method :after_reset, :resets
146146

activesupport/test/current_attributes_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class Current < ActiveSupport::CurrentAttributes
1717
before_reset { Session.previous = person&.id }
1818

1919
resets do
20-
Time.zone = "UTC"
2120
Session.current = nil
2221
end
2322

23+
resets :clear_time_zone
24+
2425
def account=(account)
2526
super
2627
self.person = Person.new(1, "#{account}'s person")
@@ -52,6 +53,11 @@ def request
5253
def intro
5354
"#{person.name}, in #{time_zone}"
5455
end
56+
57+
private
58+
def clear_time_zone
59+
Time.zone = "UTC"
60+
end
5561
end
5662

5763
class Session < ActiveSupport::CurrentAttributes

0 commit comments

Comments
 (0)