Class: BasePolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/canner/install/templates/base_policy.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_user, current_branch, method) ⇒ BasePolicy



3
4
5
6
7
8
# File 'lib/generators/canner/install/templates/base_policy.rb', line 3

def initialize(current_user, current_branch, method)
  @current_user = current_user
  @current_branch = current_branch
  @method = method.to_sym
  @roles = fetch_roles
end

Instance Method Details

#can?Boolean

implment in your policy class. return true when the user can access the action or resource and false when they can’t

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/canner/install/templates/base_policy.rb', line 31

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)


18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/canner/install/templates/base_policy.rb', line 18

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

implement this in your policy class expects an array or strings or symbols that represent the user roles

Raises:

  • (ArgumentError)


12
13
14
15
# File 'lib/generators/canner/install/templates/base_policy.rb', line 12

def fetch_roles
  raise ArgumentError.new("YOU NEED TO IMPLEMENT")
  # ex. User.roles
end