Module: Granite::Action::Policies

Extended by:
ActiveSupport::Concern
Included in:
Granite::Action
Defined in:
lib/granite/action/policies.rb,
lib/granite/action/policies/any_strategy.rb,
lib/granite/action/policies/always_allow_strategy.rb,
lib/granite/action/policies/required_performer_strategy.rb

Overview

Policies module used for abilities definition. Basically policies are defined as blocks which are executed in action instance context, so performer, object and all the attributes are available inside the block.

By default action is allowed to be performed only by default performer.

Defined Under Namespace

Modules: ClassMethods Classes: AlwaysAllowStrategy, AnyStrategy, RequiredPerformerStrategy

Instance Method Summary collapse

Instance Method Details

#allowed?Boolean

Returns true if any of defined policies returns true

Returns:

  • (Boolean)


77
78
79
80
81
82
# File 'lib/granite/action/policies.rb', line 77

def allowed?
  unless instance_variable_defined?(:@allowed)
    @allowed = _policies_strategy.allowed?(self)
  end
  @allowed
end

#authorize!Object

Raises Granite::Action::NotAllowedError if action is not allowed



86
87
88
89
# File 'lib/granite/action/policies.rb', line 86

def authorize!
  fail Granite::Action::NotAllowedError, self unless allowed?
  self
end

#performObject



65
66
67
68
# File 'lib/granite/action/policies.rb', line 65

def perform(*)
  authorize!
  super
end

#perform!Object



70
71
72
73
# File 'lib/granite/action/policies.rb', line 70

def perform!(*)
  authorize!
  super
end

#try_perform!Object



60
61
62
63
# File 'lib/granite/action/policies.rb', line 60

def try_perform!(*)
  authorize!
  super
end