Module: Granite::Action::Performing

Extended by:
ActiveSupport::Concern
Includes:
Transaction
Included in:
Granite::Action
Defined in:
lib/granite/action/performing.rb

Overview

Performing module used for defining perform procedure and error handling. Perform procedure is defined as block, which is executed in action instance context so all attributes are available there. Actions by default are performed in silent way (no validation exception raised), to raise exceptions, call bang method #perform!

Defined exceptions handlers are also executed in action instance context, but additionally get raised exception as parameter.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#perform(context: nil, **options) ⇒ Object

Check preconditions and validations for action and associated objects, then in case of valid action run defined procedure. Procedure is wrapped with database transaction. Returns the result of execute_perform! method execution or true if method execution returned false or nil

Parameters:

  • context (Symbol) (defaults to: nil)

    can be optionally provided to define which validations to test against (the context is defined on validations using ‘:on`)

Returns:

  • (Object)

    result of execute_perform! method execution or false in case of errors



56
57
58
59
60
# File 'lib/granite/action/performing.rb', line 56

def perform(context: nil, **options)
  transactional do
    valid?(context) && perform_action(options)
  end
end

#perform!(context: nil, **options) ⇒ Object

Check precondition and validations for action and associated objects, then raise exception in case of validation errors. In other case run defined procedure. Procedure is wraped with database transaction. After procedure execution check for errors, and raise exception if any. Returns the result of execute_perform! method execution or true if block execution returned false or nil

Parameters:

  • context (Symbol) (defaults to: nil)

    can be optionally provided to define which validations to test against (the context is defined on validations using ‘:on`)

Returns:

  • (Object)

    result of execute_perform! method execution

Raises:



74
75
76
77
78
79
# File 'lib/granite/action/performing.rb', line 74

def perform!(context: nil, **options)
  transactional do
    validate!(context)
    perform_action!(**options)
  end
end

#performed?Boolean

Checks if action was successfully performed or not

Returns:

  • (Boolean)

    whether action was successfully performed or not



99
100
101
# File 'lib/granite/action/performing.rb', line 99

def performed?
  @_action_performed.present?
end

#try_perform!(context: nil, **options) ⇒ Object

Performs action if preconditions are satisfied.

Parameters:

  • context (Symbol) (defaults to: nil)

    can be optionally provided to define which validations to test against (the context is defined on validations using ‘:on`)

Returns:

  • (Object)

    result of execute_perform! method execution

Raises:



89
90
91
92
93
94
# File 'lib/granite/action/performing.rb', line 89

def try_perform!(context: nil, **options)
  return unless satisfy_preconditions?
  transactional do
    perform!(context: context, **options)
  end
end