Class: Canner::Policy
- Inherits:
-
Object
- Object
- Canner::Policy
- Defined in:
- lib/canner/policy.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#can? ⇒ Boolean
implement in your policy class.
-
#canner_scope ⇒ Object
implement in your policy class to auto scope in an action.
-
#fetch_roles ⇒ Object
if you handle your roles differently you’ll need to override.
-
#initialize(current_user, method, current_branch = nil) ⇒ Policy
constructor
A new instance of Policy.
Constructor Details
#initialize(current_user, method, current_branch = nil) ⇒ Policy
Returns a new instance of Policy.
5 6 7 8 9 10 |
# File 'lib/canner/policy.rb', line 5 def initialize(current_user, method, current_branch=nil) @current_user = current_user @current_branch = current_branch @method = method.to_sym @roles = fetch_roles end |
Instance Method Details
#can? ⇒ Boolean
implement in your policy class. return true when the user can access the action or resource and false when they can’t
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/canner/policy.rb', line 33 def can? raise ArgumentError.new("NOT IMPLEMENTED") # ex: # case @method # when :index, :show # has_role?(:admin) # else # false # end end |
#canner_scope ⇒ Object
implement in your policy class to auto scope in an action
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/canner/policy.rb', line 20 def canner_scope raise ArgumentError.new("NOT IMPLEMENTED") # ex: # case @method # when :index # User.by_branch(@current_branch) # else # User.none # end end |
#fetch_roles ⇒ Object
if you handle your roles differently you’ll need to override. use: rails g canner:fetch_roles expects an array or strings or symbols that represent the user roles
15 16 17 |
# File 'lib/canner/policy.rb', line 15 def fetch_roles @current_user.nil? ? [] : @current_user.roles end |