Module: Authlogic::ORMAdapters::ActiveRecordAdapter::ActsAsAuthentic::LoggedIn

Defined in:
lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/logged_in.rb

Overview

Logged In

Handles all logic determining if a record is logged in or not. This uses the “last_request_at” field, if this field is not present none of this will be available.

Named Scopes

  • logged_in - returns all records that have a last_request_at value that is > your :logged_in_timeout.ago

  • logged_out - same as logged in but returns users that are logged out, be careful with using this, this can return a lot of users

Instance Methods

  • logged_in? - same as the logged_in named scope, but returns true if the record is logged in

  • logged_out? - opposite of logged_in?

Instance Method Summary collapse

Instance Method Details

#acts_as_authentic_with_logged_in(options = {}) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/logged_in.rb', line 19

def acts_as_authentic_with_logged_in(options = {})
  acts_as_authentic_without_logged_in(options)
  
  validates_numericality_of :login_count, :only_integer => :true, :greater_than_or_equal_to => 0, :allow_nil => true if column_names.include?("login_count")
  
  if column_names.include?("last_request_at")
    class_eval <<-"end_eval", __FILE__, __LINE__
      named_scope :logged_in, lambda { {:conditions => ["last_request_at > ?", #{options[:logged_in_timeout]}.seconds.ago]} }
      named_scope :logged_out, lambda { {:conditions => ["last_request_at is NULL or last_request_at <= ?", #{options[:logged_in_timeout]}.seconds.ago]} }
      
      def logged_in?
        raise "Can not determine the records login state because there is no last_request_at column" if !respond_to?(:last_request_at)
        !last_request_at.nil? && last_request_at > #{options[:logged_in_timeout]}.seconds.ago
      end
    
      def logged_out?
        !logged_in?
      end
    end_eval
  end
end