Module: Role

Included in:
Policy::Base
Defined in:
lib/pundit_roles/policy/role.rb,
lib/pundit_roles/policy/role/option_builder.rb

Overview

Extended by Policy::Base. Defines the methods necessary for declaring roles.

Defined Under Namespace

Classes: OptionBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#permissionsObject

Returns the value of attribute permissions.



5
6
7
# File 'lib/pundit_roles/policy/role.rb', line 5

def permissions
  @permissions
end

#role_associationsObject

Returns the value of attribute role_associations.



6
7
8
# File 'lib/pundit_roles/policy/role.rb', line 6

def role_associations
  @role_associations
end

#scopesObject

Returns the value of attribute scopes.



7
8
9
# File 'lib/pundit_roles/policy/role.rb', line 7

def scopes
  @scopes
end

Instance Method Details

#role(*opts) ⇒ Object

Builds a new role by saving it into the #permissions class instance variable Valid options are :attributes, :associations, :associated_as, :scope

Parameters:

  • *opts (Array)

    the roles, and the options which define the roles

Raises:

  • (ArgumentError)

    if the options are incorrectly defined, or no options are present



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pundit_roles/policy/role.rb', line 14

def role(*opts)
  role_opts = opts.extract_options!.dup
  options = role_opts.slice(*_role_default_keys)

  raise ArgumentError, 'Please provide at least one role' unless opts.present?
  raise_if_options_are_invalid(options)

  @permissions = {} if @permissions.nil?
  @scopes = {} if @scopes.nil?
  @role_associations = {} if @role_associations.nil?

  opts.each do |role|
    raise ArgumentError, "Expected Symbol for #{role}, got #{role.class}" unless role.is_a? Symbol

    if options[:associated_as].present?
      build_associated_roles(role, options[:associated_as])
    end

    @permissions[role] = OptionBuilder.new(self, options[:attributes], options[:associations],  options[:scope]).permitted
    @scopes[role] = options[:scope]
  end
end