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

Constructor Details

#initialize(command, hydrator: nil, hydrators: [], **options) ⇒ Form

Returns a new instance of Form.



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

def initialize(command, hydrator: nil, hydrators: [], **options)
  hydrators.push(hydrator) if hydrator.present?

  super(command, hydrators: hydrators, **options)
end

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.



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

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



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

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

#form_classObject



75
76
77
78
# File 'lib/operations/form.rb', line 75

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



71
72
73
# File 'lib/operations/form.rb', line 71

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