Class: Superform::Rails::Form

Inherits:
Component
  • Object
show all
Includes:
Phlex::Rails::Helpers::FormAuthenticityToken, Phlex::Rails::Helpers::URLFor
Defined in:
lib/superform/rails/form.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

Components::Form

Constant Summary collapse

Field =

The ‘Field` class is nested inside the `Form` class so it can be easily extended to customize the form inputs for your application. For example, if you wanted to add some default classes to all your inputs and labels you could do something like:

“‘ruby class MyForm < Superform::Rails::Form

class Field < self::Field
  def input(**attributes)
    super(class: "input input-bordered", **attributes)
  end

  def label(**attributes, &block)
    super(class: "label", **attributes, &block)
  end
end

end “‘

Superform::Rails::Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



51
52
53
54
55
56
57
# File 'lib/superform/rails/form.rb', line 51

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

Instance Attribute Details

#modelObject

Returns the value of attribute model.



36
37
38
# File 'lib/superform/rails/form.rb', line 36

def model
  @model
end

Instance Method Details

#around_templateObject



59
60
61
62
63
64
65
# File 'lib/superform/rails/form.rb', line 59

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

#build_fieldObject



47
48
49
# File 'lib/superform/rails/form.rb', line 47

def build_field(...)
  self.class::Field.new(...)
end

#form_tagObject



67
68
69
# File 'lib/superform/rails/form.rb', line 67

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

#keyObject



83
84
85
# File 'lib/superform/rails/form.rb', line 83

def key
  @model.model_name.param_key
end

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



75
76
77
78
79
80
81
# File 'lib/superform/rails/form.rb', line 75

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

#view_template {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



71
72
73
# File 'lib/superform/rails/form.rb', line 71

def view_template(&block)
  yield self if block_given?
end