Class: Superform::Rails::Form

Inherits:
Component
  • Object
show all
Defined in:
lib/superform/rails.rb

Overview

A Phlex::HTML view module that accepts a model and sets a ‘Superform::Namespace` with the `Object#model_name` as the key and maps the object to form fields and namespaces.

The ‘Form::Field` is a class that’s meant to be extended so you can customize the ‘Form` inputs to your applications needs. Defaults for the `input`, `button`, `label`, and `textarea` tags are provided.

The ‘Form` component also handles Rails authenticity tokens via the `authenticity_toklen_field` method and the HTTP verb via the `_method_field`.

Direct Known Subclasses

ApplicationForm

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, action: nil, method: nil, **attributes) ⇒ Form

Returns a new instance of Form.



77
78
79
80
81
82
83
# File 'lib/superform/rails.rb', line 77

def initialize(model, action: nil, method: nil, **attributes)
  @model = model
  @action = action
  @method = method
  @attributes = attributes
  @namespace = Namespace.root(key, object: model, field_class: self.class::Field)
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

Instance Method Details

#around_templateObject



85
86
87
88
89
90
91
# File 'lib/superform/rails.rb', line 85

def around_template(&)
  form_tag do
    authenticity_token_field
    _method_field
    super
  end
end

#form_tagObject



93
94
95
# File 'lib/superform/rails.rb', line 93

def form_tag(&)
  form action: form_action, method: form_method, **@attributes, &
end

#keyObject



109
110
111
# File 'lib/superform/rails.rb', line 109

def key
  @model.model_name.param_key
end

#submit(value = submit_value, **attributes) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/superform/rails.rb', line 101

def submit(value = submit_value, **attributes)
  input **attributes.merge(
    name: "commit",
    type: "submit",
    value: value
  )
end

#template(&block) ⇒ Object



97
98
99
# File 'lib/superform/rails.rb', line 97

def template(&block)
  yield_content(&block)
end