@@ -35,6 +35,24 @@ def resolve_token(token)
35
35
end
36
36
end
37
37
38
+ module RelationMethods
39
+ # Finds a record using a given +token+ for a predefined +purpose+. Returns
40
+ # +nil+ if the token is invalid or the record was not found.
41
+ def find_by_token_for ( purpose , token )
42
+ raise UnknownPrimaryKey . new ( self ) unless model . primary_key
43
+ model . token_definitions . fetch ( purpose ) . resolve_token ( token ) { |id | find_by ( model . primary_key => id ) }
44
+ end
45
+
46
+ # Finds a record using a given +token+ for a predefined +purpose+. Raises
47
+ # ActiveSupport::MessageVerifier::InvalidSignature if the token is invalid
48
+ # (e.g. expired, bad format, etc). Raises ActiveRecord::RecordNotFound if
49
+ # the token is valid but the record was not found.
50
+ def find_by_token_for! ( purpose , token )
51
+ model . token_definitions . fetch ( purpose ) . resolve_token ( token ) { |id | find ( id ) } ||
52
+ ( raise ActiveSupport ::MessageVerifier ::InvalidSignature )
53
+ end
54
+ end
55
+
38
56
module ClassMethods
39
57
# Defines the behavior of tokens generated for a specific +purpose+.
40
58
# A token can be generated by calling TokenFor#generate_token_for on a
@@ -85,20 +103,12 @@ def generates_token_for(purpose, expires_in: nil, &block)
85
103
self . token_definitions = token_definitions . merge ( purpose => TokenDefinition . new ( self , purpose , expires_in , block ) )
86
104
end
87
105
88
- # Finds a record using a given +token+ for a predefined +purpose+. Returns
89
- # +nil+ if the token is invalid or the record was not found.
90
- def find_by_token_for ( purpose , token )
91
- raise UnknownPrimaryKey . new ( self ) unless primary_key
92
- token_definitions . fetch ( purpose ) . resolve_token ( token ) { |id | find_by ( primary_key => id ) }
106
+ def find_by_token_for ( purpose , token ) # :nodoc:
107
+ all . find_by_token_for ( purpose , token )
93
108
end
94
109
95
- # Finds a record using a given +token+ for a predefined +purpose+. Raises
96
- # ActiveSupport::MessageVerifier::InvalidSignature if the token is invalid
97
- # (e.g. expired, bad format, etc). Raises ActiveRecord::RecordNotFound if
98
- # the token is valid but the record was not found.
99
- def find_by_token_for! ( purpose , token )
100
- token_definitions . fetch ( purpose ) . resolve_token ( token ) { |id | find ( id ) } ||
101
- ( raise ActiveSupport ::MessageVerifier ::InvalidSignature )
110
+ def find_by_token_for! ( purpose , token ) # :nodoc:
111
+ all . find_by_token_for! ( purpose , token )
102
112
end
103
113
end
104
114
0 commit comments