File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -49,10 +49,10 @@ module ClassMethods
49
49
#
50
50
# travel_back
51
51
# User.find_signed signed_id, purpose: :password_reset # => User.first
52
- def find_signed ( signed_id , purpose : nil )
52
+ def find_signed ( signed_id , purpose : nil , on_rotation : nil )
53
53
raise UnknownPrimaryKey . new ( self ) if primary_key . nil?
54
54
55
- if id = signed_id_verifier . verified ( signed_id , purpose : combine_signed_id_purposes ( purpose ) )
55
+ if id = signed_id_verifier . verified ( signed_id , purpose : combine_signed_id_purposes ( purpose ) , on_rotation : on_rotation )
56
56
find_by primary_key => id
57
57
end
58
58
end
@@ -69,8 +69,8 @@ def find_signed(signed_id, purpose: nil)
69
69
# signed_id = User.first.signed_id
70
70
# User.first.destroy
71
71
# User.find_signed! signed_id # => ActiveRecord::RecordNotFound
72
- def find_signed! ( signed_id , purpose : nil )
73
- if id = signed_id_verifier . verify ( signed_id , purpose : combine_signed_id_purposes ( purpose ) )
72
+ def find_signed! ( signed_id , purpose : nil , on_rotation : nil )
73
+ if id = signed_id_verifier . verify ( signed_id , purpose : combine_signed_id_purposes ( purpose ) , on_rotation : on_rotation )
74
74
find ( id )
75
75
end
76
76
end
Original file line number Diff line number Diff line change @@ -192,6 +192,23 @@ def set_signed_id
192
192
Account . signed_id_verifier = old_verifier
193
193
end
194
194
195
+ test "on_rotation callback using find_signed & find_signed!" do
196
+ old_verifier = Account . signed_id_verifier
197
+
198
+ Account . signed_id_verifier = ActiveSupport ::MessageVerifier . new ( "old secret" )
199
+ old_account_signed_id = @account . signed_id
200
+ Account . signed_id_verifier = ActiveSupport ::MessageVerifier . new ( "new secret" )
201
+ Account . signed_id_verifier . rotate ( "old secret" )
202
+ on_rotation_is_called = false
203
+ assert Account . find_signed ( old_account_signed_id , on_rotation : -> { on_rotation_is_called = true } )
204
+ assert on_rotation_is_called
205
+ on_rotation_is_called = false
206
+ assert Account . find_signed! ( old_account_signed_id , on_rotation : -> { on_rotation_is_called = true } )
207
+ assert on_rotation_is_called
208
+ ensure
209
+ Account . signed_id_verifier = old_verifier
210
+ end
211
+
195
212
test "cannot get a signed ID for a new record" do
196
213
assert_raises ArgumentError , match : /Cannot get a signed_id for a new record/ do
197
214
Account . new . signed_id
You can’t perform that action at this time.
0 commit comments