Sha256: 2547402e7df77656444df2eaf4725b2e594a5742e3b9b0f52b461cb0a7ff2e88

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 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
        include Janus::Models::Base unless include?(Janus::Models::Base)

        begin
          attr_protected :confirmation_token, :confirmation_sent_at, :confirmed_at
        rescue
        end
        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

6 entries across 6 versions & 1 rubygems

Version Path
janus-0.10.1 lib/janus/models/confirmable.rb
janus-0.10.0 lib/janus/models/confirmable.rb
janus-0.9.1 lib/janus/models/confirmable.rb
janus-0.9.0 lib/janus/models/confirmable.rb
janus-0.8.1 lib/janus/models/confirmable.rb
janus-0.8.0 lib/janus/models/confirmable.rb