Module: NuecaRailsInterfaces::V1::FormInterface

Defined in:
lib/nueca_rails_interfaces/v1/form_interface.rb

Overview

Form Interface. Include this module to create a new form. In this version, a form is defined as a data verifier, mainly coming from parameters to shoulder off validation from processors such as services and interactors, or action controllers. A form should be treated like a custom model without the need for a database. Forms are responsible for validating data and returning the data in a format that is ready for processing. Forms are responsible for handling bad data, returning errors provided by ActiveModel. Forms should implement the ‘attributes` method to define the attributes of the form. Forms should not override methods from ActiveModel for customization. The `attributes` method should return a hash of the attributes of the form (strictly not an array). It is up to the developer what `attributes` method will contain if there is an error. Treat as such like an API.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Allows the form mixin to include ActiveModel::Model powers.

Parameters:

  • base (self)

    Instance of the base form that would include this module.



19
20
21
# File 'lib/nueca_rails_interfaces/v1/form_interface.rb', line 19

def self.included(base)
  base.include(ActiveModel::Model)
end

Instance Method Details

#attributesObject

Final attributes to be returned by the form after validation. This is the data that is expected of the form to produce for processing.

Raises:

  • (NotImplementedError)

    If the method is not overridden.



26
27
28
# File 'lib/nueca_rails_interfaces/v1/form_interface.rb', line 26

def attributes
  raise NotImplementedError, 'Requires implementation of attributes.'
end