module Role
Extended by Policy::Base
. Defines the methods necessary for declaring roles.
Attributes
permissions[RW]
role_associations[RW]
scopes[RW]
Public Instance Methods
role(*opts)
click to toggle source
Builds a new role by saving it into the permissions
class instance variable Valid options are :attributes, :associations, :associated_as, :scope
@param *opts [Array] the roles, and the options which define the roles @raise [ArgumentError] if the options are incorrectly defined, or no options are present
# 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
Private Instance Methods
_role_default_keys()
click to toggle source
@api private
# File lib/pundit_roles/policy/role.rb, line 61 def _role_default_keys [:attributes, :associations, :associated_as, :scope] end
_role_option_validations()
click to toggle source
@api private
# File lib/pundit_roles/policy/role.rb, line 66 def _role_option_validations {attributes: [Hash, Symbol], associations: [Hash, Symbol], associated_as: [Hash], scope: [Proc]} end
build_associated_roles(role, associated_as)
click to toggle source
@api private
# File lib/pundit_roles/policy/role.rb, line 38 def build_associated_roles(role, associated_as) associated_as.each do |key, value| unless associated_as[key].is_a? Array associated_as[key] = [value] end end @role_associations[role] = associated_as end
raise_if_options_are_invalid(options)
click to toggle source
@api private
# File lib/pundit_roles/policy/role.rb, line 48 def raise_if_options_are_invalid(options) options.each do |key, value| if value.present? will_raise = true _role_option_validations[key].each do |type| will_raise = false if value.is_a? type end raise ArgumentError, "Expected #{expected} for #{key}, got #{value.class}" if will_raise end end end