Module: CurrentUser

Defined in:
lib/sixarm_ruby_current_user.rb

Instance Method Summary collapse

Instance Method Details

#current_user(ops = {}) ⇒ Object

Get the current user.

This calls User.find_by_id with the current_user_id

The current user is memoized as @current_user. To reload, pass :reload => true

Return the current user, or nil if the current user id is not set, or raise an exception if the current iser id is invalid



28
29
30
31
# File 'lib/sixarm_ruby_current_user.rb', line 28

def current_user(ops={})
  if ops[:reload] then @current_user=nil end
  @current_user ||= (current_user_id(ops) ? User.find_by_id(current_user_id) : nil)
end

#current_user=(user) ⇒ Object

Set the current user Return the current user



44
45
46
47
# File 'lib/sixarm_ruby_current_user.rb', line 44

def current_user=(user)
  self.current_user_id = (user ? user.id : nil)
  @current_user = user
end

#current_user?Boolean

Is there a current user in the Rails session?

Returns:

  • (Boolean)


36
37
38
# File 'lib/sixarm_ruby_current_user.rb', line 36

def current_user?
  !!self.current_user 
end

#current_user_id(ops = {}) ⇒ Object

Get the current user id in the Rails session array.

The current user id is memoized as @current_user_id. To reload, pass :reload => true

Return the session’s current user id.



64
65
66
67
# File 'lib/sixarm_ruby_current_user.rb', line 64

def current_user_id(ops={})
  if ops[:reload] then @current_user_id=nil end
  @current_user_id ||= session[:current_user_id]
end

#current_user_id=(id) ⇒ Object

Set the current user id in the Rails session array. Return the current user id, suitable for chaining.



80
81
82
# File 'lib/sixarm_ruby_current_user.rb', line 80

def current_user_id=(id)
  @current_user_id=session[:current_user_id]=id
end

#current_user_id?Boolean

Is there a current user id in the Rails session array?

Returns:

  • (Boolean)


72
73
74
# File 'lib/sixarm_ruby_current_user.rb', line 72

def current_user_id?
  return session[:current_user_id] ? true : false
end