Sha256: c817f10ff75c53d5249c1d4220e8ee0877c0af0353c308b7a4dfe19257d53a32

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require File.join(File.dirname(__FILE__), '..', 'notifier')

module AuthHelpers
  module Model

    # Adds remember_me to the model. The token is valid for two weeks, you can
    # can change this by overwriting the token_expiration_interval method.
    #
    module Rememberable
      def self.included(base)
        base.extend ClassMethods
        base.class_eval do
          attr_accessor   :remember_me
          attr_accessible :remember_me
          alias :remember_me? :remember_me
        end
      end

      # Call to set and save a remember me token
      #
      def remember_me!
        self.token = AuthHelpers.generate_unique_string_for(self.class, :token, 40)
        self.token_expires_at = token_expiration_interval
        self.save(false)
      end

      # Call to forget and save the token
      #
      def forget_me!
        self.token = nil
        self.token_expires_at = nil
        self.save(false)
      end

      # Change if you want to set another token_expiration_interval or add
      # custom logic (admin has one day token, clients have 2 weeks).
      #
      def token_expiration_interval
        2.weeks.from_now
      end

      module ClassMethods
        # Find the user with the given token only if it has not expired at.
        #
        def find_by_remember_me_token(token)
          self.find(:first, :conditions => [ "token = ? AND token_expires_at > CURRENT_TIMESTAMP", token ])
        end

        # Overwrites find and authenticate to deal with the remember me key.
        #
        def find_and_authenticate(options={})
          rememberable, remember_me = options.key?(:remember_me), options.delete(:remember_me)
          authenticable = super(options)

          if rememberable && authenticable.errors.empty?
            remember_me == '1' ? authenticable.remember_me! : authenticable.forget_me!
          end

          authenticable
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
josevalim-auth_helpers-0.1.0 lib/auth_helpers/model/rememberable.rb
josevalim-auth_helpers-0.1.1 lib/auth_helpers/model/rememberable.rb