Module: Granite::Action::Preconditions

Extended by:
ActiveSupport::Concern
Included in:
Granite::Action
Defined in:
lib/granite/action/preconditions.rb,
lib/granite/action/preconditions/base_precondition.rb,
lib/granite/action/preconditions/object_precondition.rb,
lib/granite/action/preconditions/embedded_precondition.rb

Overview

Conditions module is used to define preconditions for actions. Each precondition is also defined as validation, so it always run before action execution. Precondition name is by default I18n key for :base error, if precondition fails. Along with preconditions question methods with the same names are created.

Defined Under Namespace

Modules: ClassMethods Classes: BasePrecondition, EmbeddedPrecondition, ObjectPrecondition, PreconditionsCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#failed_preconditionsObject (readonly)

Returns the value of attribute failed_preconditions.



84
85
86
# File 'lib/granite/action/preconditions.rb', line 84

def failed_preconditions
  @failed_preconditions
end

Instance Method Details

#decline_with(*args, **kwargs) ⇒ Object

Adds passed error message and options to ‘errors` object



102
103
104
105
# File 'lib/granite/action/preconditions.rb', line 102

def decline_with(*args, **kwargs)
  errors.add(:base, *args, **kwargs)
  failed_preconditions << args.first
end

#initializeObject



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

def initialize(*)
  @failed_preconditions = []
  @preconditions_run = nil
  super
end

#satisfy_preconditions?(cache_result: false) ⇒ Boolean

Check if all preconditions are satisfied

Returns:

  • (Boolean)

    wheter all preconditions are satisfied



95
96
97
98
99
# File 'lib/granite/action/preconditions.rb', line 95

def satisfy_preconditions?(cache_result: false)
  errors.clear
  failed_preconditions.clear
  run_preconditions!(cache_result: cache_result)
end