Class: ActiveDryForm::Form

Inherits:
BaseForm show all
Defined in:
lib/active_dry_form/form.rb

Constant Summary collapse

ResultError =
Class.new(StandardError)

Instance Attribute Summary

Attributes inherited from BaseForm

#attributes, #base_errors, #data, #errors, #parent_form, #record, #validator

Class Method Summary collapse

Methods inherited from BaseForm

#[], #[]=, define_methods, #errors_full_messages, human_attribute_name, #id, #info, #initialize, #model_name, #params=, #persisted?, #t, #to_key, #to_model, #to_param, #valid?, #validate, wrap

Constructor Details

This class inherits a constructor from ActiveDryForm::BaseForm

Class Method Details

.action(method) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_dry_form/form.rb', line 22

def self.action(method)
  alias_method :"__#{method}", method

  class_eval <<~RUBY, __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition
    def #{method}(...)
      validate
      return Failure(:invalid_form) unless valid?

      result = __#{method}(...)

      case result
      when ::Dry::Monads::Result::Failure
        @base_errors = Array.wrap(result.failure)
      when ::Dry::Monads::Result::Success
      else
        raise ResultError, 'method `#{method}` must return `monad`'
      end

      result
    end
  RUBY
end

.fields(namespace, i18n_key: nil, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/active_dry_form/form.rb', line 12

def self.fields(namespace, i18n_key: nil, &block)
  const_set :CURRENT_CONTRACT, Class.new(contract_klass, &block).new
  const_set :FIELDS_INFO, self::CURRENT_CONTRACT.schema.json_schema(loose: true)

  const_set :NAMESPACE, ActiveModel::Name.new(nil, nil, namespace.name.capitalize)
  const_get(:NAMESPACE).i18n_key = i18n_key if i18n_key

  define_methods
end