module Authlogic::ActsAsAuthentic::EmailToken::Methods::ClassMethods

Public Instance Methods

find_using_email_token(token, age = self.email_token_valid_for) click to toggle source

Use this method to find a record with an email confirmation token. This method does 2 things for you:

  1. It ignores blank tokens

  2. It enforces the +email_token_valid_for configuration+ option.

If you want to use a different timeout value, just pass it as the second parameter:

User.find_using_email_token(token, 1.hour)

This method is very similar to, and based heavily off of, Authlogic’s #find_using_perishable_token method.

# File lib/authlogic/acts_as_authentic/email_token.rb, line 98
def find_using_email_token(token, age = self.email_token_valid_for)
  return if token.blank?
  age = age.to_i
  
  # Authlogic builds its SQL by hand, but I prefer Arel. The logic is the same.
  # No need to add an IS NOT NULL clause, because above, we return if the given
  # token is blank.
  t = arel_table
  conditions = t[:email_token].eq(token)
  if age > 0
    conditions = conditions.and(
      t[:email_token_updated_at].gt(age.seconds.ago)
    )
  end
  
  where(conditions).first
end
find_using_email_token!(token, age = self.email_token_valid_for) click to toggle source

This method will raise ActiveRecord::RecordNotFound if no record is found.

# File lib/authlogic/acts_as_authentic/email_token.rb, line 117
def find_using_email_token!(token, age = self.email_token_valid_for)
  find_using_email_token(token, age) || raise(ActiveRecord::RecordNotFound)
end