Class: ActionForm::Rails::Base

Inherits:
Base
  • Object
show all
Includes:
Rendering
Defined in:
lib/action_form/rails/base.rb

Overview

RailsForm class for ActionForm that handles Rails-specific form rendering. It integrates with Rails form helpers and provides a Rails-friendly interface for building forms.

Instance Attribute Summary collapse

Attributes inherited from Base

#elements_instances, #errors, #html_options, #object, #scope

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rendering

#render_authenticity_token, #render_form, #render_method_input, #render_submit, #render_utf8_input

Methods inherited from Base

#build_from_object, #helpers, inherited, #render_in

Methods included from ActionForm::Rendering

#render_element, #render_elements, #render_form, #render_inline_errors, #render_input, #render_label, #render_many_subforms, #render_new_subform_button, #render_remove_subform_button, #render_subform, #render_submit

Methods included from ElementsDSL

included

Methods included from SchemaDSL

included

Constructor Details

#initialize(model: nil, scope: self.class.scope, params: nil, **html_options) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
24
# File 'lib/action_form/rails/base.rb', line 19

def initialize(model: nil, scope: self.class.scope, params: nil, **html_options)
  @namespaced_model = model
  @object = model.is_a?(Array) ? Array(model).last : model
  @scope = scope.nil? && @object.nil? ? nil : (scope || param_key)
  super(object: @object, scope: @scope, params: params, **html_options)
end

Instance Attribute Details

#namespaced_modelObject (readonly)

Returns the value of attribute namespaced_model.



17
18
19
# File 'lib/action_form/rails/base.rb', line 17

def namespaced_model
  @namespaced_model
end

Class Method Details

.many(name, default: nil, &block) ⇒ Object



46
47
48
49
50
# File 'lib/action_form/rails/base.rb', line 46

def many(name, default: nil, &block)
  super
  elements[name].subform_definition.add_primary_key_element
  elements[name].subform_definition.add_delete_element
end

.params_definition(scope: self.scope) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/action_form/rails/base.rb', line 34

def params_definition(scope: self.scope)
  return super unless scope

  @params_definitions ||= Hash.new do |h, key|
    h[key] = begin
      klass = super
      Class.new(params_class) { has scope, klass }
    end
  end
  @params_definitions[scope]
end

.resource_model(model = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/action_form/rails/base.rb', line 27

def resource_model(model = nil)
  return @resource_model unless model

  @resource_model = model
  @scope = model.model_name.param_key.to_sym
end

.subform(name, default: nil, &block) ⇒ Object



52
53
54
55
# File 'lib/action_form/rails/base.rb', line 52

def subform(name, default: nil, &block)
  super
  elements[name].add_primary_key_element
end

.subform_classObject



13
14
15
# File 'lib/action_form/rails/base.rb', line 13

def self.subform_class
  ActionForm::Rails::Subform
end

Instance Method Details

#view_templateObject



58
59
60
61
62
63
# File 'lib/action_form/rails/base.rb', line 58

def view_template
  render_form do
    render_elements
    render_submit
  end
end

#with_params(form_params) ⇒ Object



65
66
67
# File 'lib/action_form/rails/base.rb', line 65

def with_params(form_params)
  self.class.new(model: @namespaced_model, scope: @scope, params: form_params, **html_options)
end