2009-09-18 08:01:29 -04:00
|
|
|
module Devise
|
2009-09-20 09:35:26 -04:00
|
|
|
module Perishable
|
2009-09-18 08:01:29 -04:00
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
2009-10-07 21:59:55 -04:00
|
|
|
extend ClassMethods
|
|
|
|
|
2009-09-18 09:20:45 -04:00
|
|
|
before_create :reset_perishable_token
|
2009-09-18 08:01:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-18 09:20:45 -04:00
|
|
|
# Generates a new random token for confirmation, based on actual Time and salt
|
|
|
|
#
|
|
|
|
def reset_perishable_token
|
|
|
|
self.perishable_token = secure_digest(Time.now.utc, random_string, password)
|
|
|
|
end
|
2009-09-18 08:01:29 -04:00
|
|
|
|
2009-09-18 09:20:45 -04:00
|
|
|
# Resets the perishable token with and save the record without validating
|
|
|
|
#
|
|
|
|
def reset_perishable_token!
|
|
|
|
reset_perishable_token and save(false)
|
|
|
|
end
|
2009-10-07 21:59:55 -04:00
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
# Attempt to find a user by and incoming perishable_token. If no user is
|
|
|
|
# found, initialize a new one and adds an :invalid error to perishable_token
|
|
|
|
#
|
|
|
|
def find_or_initialize_with_error_by_perishable_token(perishable_token)
|
|
|
|
perishable = find_or_initialize_by_perishable_token(perishable_token)
|
|
|
|
if perishable.new_record?
|
|
|
|
perishable.errors.add(:perishable_token, :invalid, :default => "invalid confirmation")
|
|
|
|
end
|
|
|
|
perishable
|
|
|
|
end
|
|
|
|
end
|
2009-09-18 08:01:29 -04:00
|
|
|
end
|
|
|
|
end
|