Skip to content

Commit f49f68b

Browse files
committed
Fix rotate(on_on_rotation:) and #on_rotation
Ref: rails#44179 Fix: rails#54357 `MessageVerifier` and `MessageEncryptor` were intended to accept an `on_rotation` callback from the `#rotate` method or from the `#on_rotation` method. This is particularly useful for codecs that aren't created by the user, but by the framework, like `ActiveRecord::Base.signed_id_verifier`
1 parent bcebc09 commit f49f68b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

activesupport/lib/active_support/messages/rotator.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ def initialize(*args, on_rotation: nil, **options)
1111
@on_rotation = on_rotation
1212
end
1313

14-
def rotate(*args, **options)
14+
def rotate(*args, on_rotation: nil, **options)
15+
@on_rotation = on_rotation if on_rotation
1516
fall_back_to build_rotation(*args, **options)
1617
end
1718

19+
def on_rotation(&on_rotation)
20+
@on_rotation = on_rotation
21+
self
22+
end
23+
1824
def fall_back_to(fallback)
1925
@rotations << fallback
2026
self

activesupport/test/messages/message_rotator_tests.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ def self.load(*); raise Class.new(StandardError); end
4545
assert called
4646
end
4747

48+
test "rotate(on_rotation:) is called on successful rotation" do
49+
called = nil
50+
codec = make_codec(secret("new")).rotate(secret("old"), on_rotation: proc { called = true })
51+
old_codec = make_codec(secret("old"))
52+
old_message = encode(DATA, old_codec)
53+
assert_equal DATA, decode(old_message, codec)
54+
assert called
55+
end
56+
57+
test "rotate().on_rotation is called on successful rotation" do
58+
called = nil
59+
codec = make_codec(secret("new")).rotate(secret("old")).on_rotation do
60+
called = true
61+
end
62+
old_codec = make_codec(secret("old"))
63+
old_message = encode(DATA, old_codec)
64+
assert_equal DATA, decode(old_message, codec)
65+
assert called
66+
end
67+
4868
test "on_rotation is not called when no rotation is necessary" do
4969
called = nil
5070
assert_rotate [secret("same"), on_rotation: proc { called = true }], [secret("same")]

0 commit comments

Comments
 (0)