|
Revision 4377, 1.0 kB
(checked in by david, 2 years ago)
|
Added a token generator plugin [DHH]
|
| Line | |
|---|
| 1 |
TokenGenerator |
|---|
| 2 |
============== |
|---|
| 3 |
|
|---|
| 4 |
Mix-in for classes that needs to have a token generated using MD5. You can set the length of the token |
|---|
| 5 |
and provide it with an optional block that'll check the validity of the generated token (usually whether it's |
|---|
| 6 |
already taken or not). |
|---|
| 7 |
|
|---|
| 8 |
The two methods are generate_token, which will just return a new token, and set_token, which will assume |
|---|
| 9 |
that there's a writer for the instance variable "token" and that the class has a "find_by_token" method to check |
|---|
| 10 |
validity (if a duplicate exists, generate another token). |
|---|
| 11 |
|
|---|
| 12 |
Examples: |
|---|
| 13 |
|
|---|
| 14 |
class Invitation < ActiveRecord::Base |
|---|
| 15 |
include TokenGenerator |
|---|
| 16 |
before_create :set_token |
|---|
| 17 |
end |
|---|
| 18 |
|
|---|
| 19 |
class ImperialInvitation < ActiveRecord::Base |
|---|
| 20 |
include TokenGenerator |
|---|
| 21 |
before_create :set_token |
|---|
| 22 |
|
|---|
| 23 |
private |
|---|
| 24 |
def set_token |
|---|
| 25 |
self.token = generate_token { |token| complies_to_imperial_standards?(token) } |
|---|
| 26 |
end |
|---|
| 27 |
end |
|---|
| 28 |
|
|---|
| 29 |
Copyright (c) 2005 David Heinemeier Hansson, Marcel Molina Jr. released under the MIT license |
|---|