Class: HyperActiveForm::Base
- Inherits:
-
Object
- Object
- HyperActiveForm::Base
- 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
-
#assigned_attribute_names ⇒ Object
readonly
The list of attribute names that have been passed to the form during the last call to ‘assign_form_attributes` or `submit`.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#add_errors_from(model) ⇒ Object
Adds the errors from a model to the form.
-
#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.
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #perform ⇒ Object
- #setup ⇒ Object
-
#submit(params) ⇒ Boolean
Submits the form, assigning the attributes from the params, running validations and calling the ‘perform` method if the form is valid.
-
#submit!(params) ⇒ Boolean
Same as ‘submit` but raises a `FormDidNotSubmitError` if the form is not valid.
Constructor Details
#initialize ⇒ Base
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_names ⇒ Object (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
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
89 90 91 92 93 94 95 |
# File 'lib/hyper_active_form/base.rb', line 89 def add_errors_from(model) model.errors.each do |error| Array.wrap(error.).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
53 54 55 56 57 58 59 60 61 62 |
# 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 public_send(:"#{attribute}=", params&.dig(attribute) || default_value) end @assigned_attribute_names = params.slice(*attribute_names).keys end end |
#perform ⇒ Object
43 44 45 |
# File 'lib/hyper_active_form/base.rb', line 43 def perform raise NotImplementedError end |
#setup ⇒ Object
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
69 70 71 72 73 74 75 76 |
# File 'lib/hyper_active_form/base.rb', line 69 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
82 83 84 |
# File 'lib/hyper_active_form/base.rb', line 82 def submit!(params) submit(params) || raise(HyperActiveForm::FormDidNotSubmitError) end |