Class: Canner::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/canner/policy.rb

Direct Known Subclasses

BasePolicy

Instance Method Summary collapse

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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


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_scopeObject

implement in your policy class to auto scope in an action

Raises:

  • (ArgumentError)


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_rolesObject

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