summaryrefslogtreecommitdiff
path: root/app/models/user.rb
diff options
context:
space:
mode:
authorshumakl <shumakl@purdue.edu>2014-04-03 16:44:29 -0400
committershumakl <shumakl@purdue.edu>2014-04-03 16:45:00 -0400
commitdfe5dbd2ada1841b09f70bfd742c10ba878f74fe (patch)
tree1bfd64e9f20f1299fe570130d2660e58d14e2dad /app/models/user.rb
parent9f19d0e16d7920e07255c0fbe596c518d1aa415f (diff)
Use the null object pattern for current_user when not logged in
Diffstat (limited to 'app/models/user.rb')
-rw-r--r--app/models/user.rb15
1 files changed, 15 insertions, 0 deletions
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