Module: Authlogic::ActsAsAuthentic::EmailToken::Methods

Defined in:
lib/authlogic/acts_as_authentic/email_token.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/authlogic/acts_as_authentic/email_token.rb', line 55

def self.included(klass)
  # Do nothing if the email_token column is missing. If the email_token column
  # is present but not email_token_updated_at, raise.
  if !klass.column_names.map(&:to_s).include? 'email_token'
    return
  elsif !klass.column_names.map(&:to_s).include? 'email_token_updated_at'
    raise(
      ::Authlogic::ActsAsAuthentic::EmailToken::DBStructureError,
      "#{klass.name} has an email_token column but not email_token_updated_at. " +
      " You must add the latter. (Should be :datetime, null: false.)"
    )
  end
        
  klass.class_eval do
    extend ClassMethods
    include InstanceMethods
  
    # If this module is added to an existing app, the email_confirmation column will
    # initially be blank. To avoid errors upon save, we must phase in the new tokens.
    # 
    # Similarly, when new records are created, we must set these values.
    before_save ->(user) {
      if user.email_token.blank? or user.email_token_updated_at.blank?
        user.reset_email_token
      end
    }
  end
end