Null object pattern for User as GuestUser
This commit is contained in:
parent
d9bea7f39a
commit
92dbf98339
2 changed files with 12 additions and 1 deletions
|
@ -10,9 +10,10 @@ class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
@current_user ||= User.find(session[:user_id]) if session[:user_id]
|
@current_user ||= User.find(session[:user_id]) if session[:user_id]
|
||||||
|
@current_user ||= GuestUser.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def logged_in?
|
def logged_in?
|
||||||
@logged_in ||= !!current_user
|
@logged_in ||= current_user.class.name.demodulize == 'User'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,3 +26,13 @@ class User < ActiveRecord::Base
|
||||||
self.add_role :admin
|
self.add_role :admin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class GuestUser
|
||||||
|
def admin?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def moderator?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue