Class: Upgrow::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/upgrow/action.rb

Overview

Actions represent the entry points to the app’s core logic. These objects coordinate workflows in order to get operations and activities done. Ultimately, Actions are the public interface of the app’s business layers.

Rails controllers talk to the app’s internals by sending messages to specific Actions, optionally with the required inputs. Actions have a one-to-one relationship with incoming requests: they are paired symmetrically with end-user intents and demands. This is quite a special requirement from this layer: any given HTTP request handled by the app should be handled by a single Action.

The fact that each Action represents a meaningful and complete request-response cycle forces modularization for the app’s business logic, exposing immediately complex relationships between objects at the same time that frees up the app from scenarios such as interdependent requests. In other words, Actions do not have knowledge or coupling between other Actions whatsoever.

Actions respond to a single public method perform. Each Action defines its own set of required arguments for perform, as well what can be expected as the result of that method.

Defined Under Namespace

Modules: Decorator

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.exposuresArray

Each Action class has its own list of exposed instance variables to be included in the returned Result when the Action is called.

Returns:

  • (Array)

    the list of exposed instance variables.



52
53
54
# File 'lib/upgrow/action.rb', line 52

def exposures
  @exposures ||= []
end

Class Method Details

.expose(*names) ⇒ Object

Sets the given instance variable names as exposed in the Result.

Parameters:

  • names (Array<Symbol>)

    the list of instance variable names to be exposed as part of the Result.



60
61
62
# File 'lib/upgrow/action.rb', line 60

def expose(*names)
  @exposures += names
end

Instance Method Details

#failure(*errors) ⇒ :failure, Result

Throws a Result populated with the given errors.

Parameters:

  • errors (Array<Error>, ActiveModel::Errors)

    the errors object to be set as the Result errors. If an ActiveModel::Errors object is provided, it will be converted to an Array of Errors.

Returns:

  • (:failure, Result)

    the Result instance populated with the given errors.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/upgrow/action.rb', line 81

def failure(*errors)
  errors = errors.first if errors.first.respond_to?(:each)

  if errors.respond_to?(:full_message)
    errors = errors.map do |error|
      Error.new(
        attribute: error.attribute,
        code: error.type,
        message: error.full_message
      )
    end
  end

  throw(:failure, result(errors: errors))
end