Class: Policy
- Inherits:
-
Object
- Object
- Policy
- Defined in:
- lib/adapters/model.rb,
lib/egoist/base.rb,
lib/egoist/error.rb,
lib/egoist/error.rb,
lib/egoist/proxy.rb,
lib/adapters/controller.rb
Overview
include Policy::Controller
Defined Under Namespace
Modules: Controller, Model Classes: Error, Proxy
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
- .can(model = nil, user = nil) ⇒ Object
-
.current_user ⇒ Object
try to load current user.
- .error(msg) ⇒ Object
Instance Method Summary collapse
- #can ⇒ Object
-
#can?(action, *args, &block) ⇒ Boolean
pass block if you want to handle errors yourself return true if false if block is passed.
- #error(message) ⇒ Object
-
#initialize(model:, user: nil) ⇒ Policy
constructor
A new instance of Policy.
Constructor Details
#initialize(model:, user: nil) ⇒ Policy
Returns a new instance of Policy.
4 5 6 7 |
# File 'lib/egoist/base.rb', line 4 def initialize model:, user: nil @model = model @user = user || Policy.current_user end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
2 3 4 |
# File 'lib/egoist/base.rb', line 2 def action @action end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
2 3 4 |
# File 'lib/egoist/base.rb', line 2 def model @model end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
2 3 4 |
# File 'lib/egoist/base.rb', line 2 def user @user end |
Class Method Details
.can(model = nil, user = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/egoist/proxy.rb', line 16 def can model = nil, user = nil if model.is_a?(Hash) model, user = model[:model], model[:user] end klass = self # if we are calling can on Policy class, figure out policy class if self == Policy klass = ('%s_policy' % model.class).classify klass = Object.const_defined?('::%s' % klass) ? klass.constantize : raise('Policy class %s not defined' % klass) end klass.new(user: user, model: model).can end |
.current_user ⇒ Object
try to load current user
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/egoist/proxy.rb', line 4 def current_user if Thread.current.key?(:current_user) Thread.current[:current_user] elsif defined?(User) && User.respond_to?(:current) User.current elsif defined?(Current) && Current.respond_to?(:user) Current.user else raise RuntimeError.new('Current user not found in Policy#current_user') end end |
Instance Method Details
#can?(action, *args, &block) ⇒ Boolean
pass block if you want to handle errors yourself return true if false if block is passed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/egoist/base.rb', line 11 def can? action, *args, &block @action = action .to_s .gsub(/[^\w+]/, '') .concat('?') .to_sym # pre check if %i(can).index(@action) raise RuntimeError.new('Method name not allowed') end unless respond_to?(@action) raise NoMethodError.new(%[Policy check "#{@action}" not found in #{self.class}]) end call *args, &block end |