diff options
-rw-r--r-- | app/helpers/sessions_helper.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 15 |
2 files changed, 17 insertions, 2 deletions
diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 54e4b0d..89e5ef3 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -18,7 +18,7 @@ module SessionsHelper def current_user @token ||= Session.hash_token(cookies[:remember_token]) @session ||= Session.find_by(token: @token) - @current_user ||= (@session.nil? ? nil : @session.user) + @current_user ||= (@session.nil? ? NilUser.new : @session.user) end # checks if someone is currently signed in @@ -30,7 +30,7 @@ module SessionsHelper if signed_in? @session.destroy end - @current_user = nil + @current_user = NilUser.new cookies.delete(:remember_token) end diff --git a/app/models/user.rb b/app/models/user.rb index 277d885..016c155 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -91,3 +91,18 @@ class User < ActiveRecord::Base validates :password, length: { minimum: 6 } end +class NilUser + def nil? + return true + end + def can?(action) + return false + end + def method_missing(name, *args) + # Throw an error if User doesn't have this method + super unless User.new.respond_to?(name) + # User has this method -- return a blank value + # 'false' if the method ends with '?'; 'nil' otherwise. + name.ends_with?('?') ? false : nil + end +end |