From 92dbf98339dd421bdbb4342f4980728e5f4ba3a4 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Thu, 8 Oct 2015 18:03:09 -0400 Subject: [PATCH] Null object pattern for User as GuestUser --- app/controllers/application_controller.rb | 3 ++- app/models/user.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 803363f..2c0b2b9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,9 +10,10 @@ class ApplicationController < ActionController::Base def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] + @current_user ||= GuestUser.new end def logged_in? - @logged_in ||= !!current_user + @logged_in ||= current_user.class.name.demodulize == 'User' end end diff --git a/app/models/user.rb b/app/models/user.rb index 6480f19..458c5c9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -26,3 +26,13 @@ class User < ActiveRecord::Base self.add_role :admin end end + +class GuestUser + def admin? + false + end + + def moderator? + false + end +end