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



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

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

#initializeObject



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

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

#satisfy_preconditions?Boolean

Check if all preconditions are satisfied

Returns:

  • (Boolean)

    wheter all preconditions are satisfied



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

def satisfy_preconditions?
  errors.clear
  failed_preconditions.clear
  run_preconditions!
end