class Upgrow::Action

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.

Attributes

exposures[W]

Public Class Methods

expose(*names) click to toggle source

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

@param names [Array<Symbol>] the list of instance variable names to be

exposed as part of the Result.
# File lib/upgrow/action.rb, line 60
def expose(*names)
  @exposures += names
end
exposures() click to toggle source

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

@return [Array] the list of exposed instance variables.

# File lib/upgrow/action.rb, line 52
def exposures
  @exposures ||= []
end

Private Class Methods

inherited(subclass) click to toggle source
Calls superclass method
# File lib/upgrow/action.rb, line 66
def inherited(subclass)
  super
  subclass.exposures = exposures.dup
  subclass.prepend(Decorator)
end

Public Instance Methods

failure(*errors) click to toggle source

Throws a Result populated with the given errors.

@param 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.

@return [:failure, Result] the Result instance populated with the given

errors.
# 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

Private Instance Methods

result(members = {}) click to toggle source
# File lib/upgrow/action.rb, line 99
def result(members = {})
  result_class = Result.new(*self.class.exposures)

  values = self.class.exposures.to_h do |name|
    [name, instance_variable_get("@#{name}")]
  end

  result_class.new(**values.merge(members))
end