Class: HyperActiveForm::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/hyper_active_form/base.rb

Overview

Base class for HyperActiveForm objects

HyperActiveForm objects are simple ActiveModel objects that encapsulate form logic and validations. They are designed to be subclassed and customized to fit the needs of your application.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



36
37
38
39
# File 'lib/hyper_active_form/base.rb', line 36

def initialize(*, **)
  super()
  setup(*, **)
end

Instance Attribute Details

#assigned_attribute_namesObject (readonly)

The list of attribute names that have been passed to the form during the last call to assign_form_attributes or submit



24
25
26
# File 'lib/hyper_active_form/base.rb', line 24

def assigned_attribute_names
  @assigned_attribute_names
end

Class Method Details

.proxy_for(klass, object) ⇒ Object

Defines to which object the form should delegate the active model methods This is useful so form_for/form_with can automatically deduce the url and method to use

Parameters:

  • klass (Class)

    the class of the object to proxy

  • object (Object)

    where to delegate the object to, for example: :@user



31
32
33
34
# File 'lib/hyper_active_form/base.rb', line 31

def self.proxy_for(klass, object)
  delegate :new_record?, :persisted?, :id, to: object
  singleton_class.delegate :model_name, to: klass
end

Instance Method Details

#add_errors_from(model) ⇒ Object

Adds the errors from a model to the form

Parameters:

  • model (ActiveModel::Model)

    the model to add the errors from



105
106
107
108
109
110
111
# File 'lib/hyper_active_form/base.rb', line 105

def add_errors_from(model)
  model.errors.each do |error|
    Array.wrap(error.message).each { |e| errors.add(error.attribute, e) }
  end

  false
end

#assign_form_attributes(params = nil, **options) ⇒ Object

Assigns the attributes of the form from the params This method is called by the submit method, but can also be called directly if you need to assign the attributes without submitting the form for example if you want to refresh the form with new data

Parameters:

  • params (Hash) (defaults to: nil)

    the params to assign the attributes from

  • options (Hash)

    optional keyword arguments

Options Hash (**options):

  • :overwrite_missing (Boolean)

    whether to overwrite missing attributes with their default value or nil



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hyper_active_form/base.rb', line 55

def assign_form_attributes(params = nil, **options)
  overwrite_missing = if params.nil?
    params = options
  else
    options.fetch(:overwrite_missing, true)
  end

  run_callbacks :assign_form_attributes do
    params = ActionController::Parameters.new(params) unless params.is_a?(ActionController::Parameters)
    attribute_names.each do |attribute|
      default_value = self.class._default_attributes[attribute]&.value_before_type_cast
      if params&.key?(attribute)
        public_send(:"#{attribute}=", params&.dig(attribute))
      elsif overwrite_missing
        public_send(:"#{attribute}=", default_value)
      end
    end
    @assigned_attribute_names = params.slice(*attribute_names).keys
  end
end

#performObject

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/hyper_active_form/base.rb', line 43

def perform
  raise NotImplementedError
end

#setupObject



41
# File 'lib/hyper_active_form/base.rb', line 41

def setup; end

#submit(params = nil, **options) ⇒ Boolean

Submits the form, assigning the attributes from the params, running validations and calling the perform method if the form is valid

Parameters:

  • params (Hash) (defaults to: nil)

    the params to assign the attributes from

  • options (Hash)

    optional keyword arguments

Options Hash (**options):

  • :overwrite_missing (Boolean)

    whether to overwrite missing attributes with their default value or nil

Returns:

  • (Boolean)

    true if the form is valid and the perform method returned something truthy



83
84
85
86
87
88
89
90
# File 'lib/hyper_active_form/base.rb', line 83

def submit(params = nil, **options)
  run_callbacks :submit do
    assign_form_attributes(params, **options)
    !!(valid? && perform)
  end
rescue HyperActiveForm::CancelFormSubmit
  false
end

#submit!(params = nil, **options) ⇒ Boolean

Same as submit but raises a FormDidNotSubmitError if the form is not valid

Parameters:

  • params (Hash) (defaults to: nil)

    the params to assign the attributes from

  • options (Hash)

    optional keyword arguments

Options Hash (**options):

  • :overwrite_missing (Boolean)

    whether to overwrite missing attributes with their default value or nil

Returns:

  • (Boolean)

    true if the form is valid and the perform method returned something truthy



98
99
100
# File 'lib/hyper_active_form/base.rb', line 98

def submit!(params = nil, **options)
  submit(params, **options) || raise(HyperActiveForm::FormDidNotSubmitError)
end