Class: Operations::Form

Inherits:
Object
  • Object
show all
Includes:
Dry::Core::Constants
Defined in:
lib/operations/form.rb

Overview

Configures and defines a form object factory. Forms can be defined on top of commants and used in the user-facing controllers. Form objects are Rails-specific and support everything that is needed for Rails’ form rendering helpers. They are designed to replace direct usage of ActiveRecord models in controllers and views and act as an integration bridge from Rails application to the Operations framework.

Examples:


command = Operations::Command.new(...)
form = Operations::Form.new(command)

@form_object = form.build(params)

form_for @form_object, url: ...

@form_object = form.persist(params)
respond_with @form_object

Defined Under Namespace

Classes: Attribute, Base, Builder, DeprecatedLegacyModelMapImplementation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(subclass) ⇒ Object

We need to make deprecated inheritance from Operations::Form act exactly the same way as from Operations::Form::Base. In order to do this, we are encapsulating all the inheritable functionality in 2 modules and removing methods defined in Operations::Form from the result class.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/operations/form.rb', line 31

def self.inherited(subclass)
  super

  return unless self == Operations::Form

  ActiveSupport::Deprecation.new.warn("Inheritance from Operations::Form is deprecated and will be " \
    "removed in 1.0.0. Please inherit from Operations::Form::Base instead")

  (Operations::Form.instance_methods - Object.instance_methods).each do |method|
    subclass.undef_method(method)
  end

  subclass.extend Operations::Form::Base::ClassMethods
  subclass.prepend Operations::Form::Base::InstanceMethods
end

Instance Method Details

#build(params = EMPTY_HASH, **context) ⇒ Object



58
59
60
# File 'lib/operations/form.rb', line 58

def build(params = EMPTY_HASH, **context)
  instantiate_form(command.callable(transform_params(params, **context), **context))
end

#form_classObject



66
67
68
69
# File 'lib/operations/form.rb', line 66

def form_class
  @form_class ||= Operations::Form::Builder.new(base_class: base_class)
    .build(key_map: key_map, model_map: model_map, model_name: model_name, persisted: persisted)
end

#persist(params = EMPTY_HASH, **context) ⇒ Object



62
63
64
# File 'lib/operations/form.rb', line 62

def persist(params = EMPTY_HASH, **context)
  instantiate_form(command.call(transform_params(params, **context), **context))
end