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



93
94
95
96
97
98
99
# File 'lib/hyper_active_form/base.rb', line 93

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) ⇒ 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)

    the params to assign the attributes from



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hyper_active_form/base.rb', line 53

def assign_form_attributes(params)
  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))
      else
        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) ⇒ 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)

    the params to assign the attributes from

Returns:

  • (Boolean)

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



73
74
75
76
77
78
79
80
# File 'lib/hyper_active_form/base.rb', line 73

def submit(params)
  run_callbacks :submit do
    assign_form_attributes(params)
    !!(valid? && perform)
  end
rescue HyperActiveForm::CancelFormSubmit
  false
end

#submit!(params) ⇒ Boolean

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

Parameters:

  • params (Hash)

    the params to assign the attributes from

Returns:

  • (Boolean)

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



86
87
88
# File 'lib/hyper_active_form/base.rb', line 86

def submit!(params)
  submit(params) || raise(HyperActiveForm::FormDidNotSubmitError)
end