Sha256: 2a9c05a06571064522cacee8ddc196b92a9ab4a41e8ae90f4df27d1069f0b44e

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module Janus
  module Models
    # = Confirmable
    # 
    # Confirms an account's email by sending an email with an unique token.
    # This is necessary to be sure the user can be contacted on that email.
    # 
    # IMPROVE: reconfirm whenever email changes.
    module Confirmable
      extend ActiveSupport::Concern

      included do
        attr_protected :confirmation_token, :confirmation_sent_at, :confirmed_at
        janus_config(:confirmation_key)
        
        before_create :generate_confirmation_token
#        before_update :generate_confirmation_token, :if => :email_changed?
      end

      # Generates the confirmation token, but won't save the record.
      def generate_confirmation_token
        self.confirmation_token = self.class.generate_token(:confirmation_token)
        self.confirmation_sent_at = Time.now
      end

      # Confirms the record.
      def confirm!
        self.confirmation_token = self.confirmation_sent_at = nil
        self.confirmed_at = Time.now
        save
      end

      # Checks wether the email of this user if confirmed, or not.
      def confirmed?
        confirmed_at?
      end

      module ClassMethods
        def find_for_confirmation(token)
          where(:confirmation_token => token).first unless token.blank?
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
janus-0.6.0 lib/janus/models/confirmable.rb
janus-0.5.0 lib/janus/models/confirmable.rb