Sha256: 2756c0f729ec30e3d0ae90cd6e8e044e19bc7547df7506d0d895ee4f4f803f26

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

= SixArm.com » Ruby » CurrentUser module with current_user methods

Author:: Joel Parker Henderson, joel@joelparkerhenderson.com
Copyright:: Copyright (c) 2005-2011 Joel Parker Henderson
License:: See LICENSE.txt file

Get and set the current user in the Rails session array.

When you set the current user, this does:
 - @current_user = user
 - @current_user_id = user.id
 - session[:current_user_id] = user.id


== Example code

   joe = User.find(123)
   self.current_user = joe
   => 
   @current_user == joe
   @current_user_id == 123
   session[:current_user_id] == 123


== Example controller

  class MyController < ApplicationController

    def sign_in(user)
      self.current_user = user
    end

    def sign_out
      self.current_user = nil
    end

    def is_anyone_using_this?
      current_user?
    end

  end


== Example of reloading

For fast speed, we memoize current_user and current_user_id: 
we use fast instance variables @current_user and @current_user_id
rather than reading the slower session[:current_user_id] each time.

To reload @current_user and @current_user_id from session[:current_user_id], 
we use the :reload parameter like this:

   current_user(:reload => true)


== Why use the self prefix?

When we set variables, we must use the "self" prefix because Ruby uses this to do method dispatch.

Right:
   self.current_user = joe

Wrong:
   current_user = joe








Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sixarm_ruby_current_user-1.4.6 README.rdoc
sixarm_ruby_current_user-1.4.4 README.rdoc