Class: Yattho::Forms::ToggleSwitchForm

Inherits:
Base
  • Object
show all
Defined in:
lib/yattho/forms/toggle_switch_form.rb

Overview

Toggle switches are designed to submit an on/off value to the server immediately upon click. For that reason they are not designed to be used in “regular” forms that have other fields, etc. Instead they should be used independently via this class.

ToggleSwitchForm can be used directly or via inheritance.

Via inheritance:

# app/forms/my_toggle_form.rb class MyToggleForm < Yattho::Forms::ToggleSwitchForm

def initialize(**system_arguments)
  super(name: :foo, label: "Foo", src: "/foo", **system_arguments)
end

end

# app/views/some_view.html.erb <%= render(MyToggleForm.new) %>

Directly:

# app/views/some_view.html.erb <%= render(

Yattho::Forms::ToggleSwitchForm.new(
  name: :foo, label: "Foo", src: "/foo"
)

) %>

Direct Known Subclasses

ExampleToggleSwitchForm

Instance Attribute Summary

Attributes included from ActsAsComponent

#template_root_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after_content?, #before_render, caption_template?, #caption_template?, #each_input_in, fields_with_caption_templates, form, #inputs, #perform_render, #render?, #render_caption_template, sanitize_field_name_for_template_path

Methods included from ActsAsComponent

#compile!, extended, #renders_templates

Constructor Details

#initialize(**system_arguments) ⇒ ToggleSwitchForm

Returns a new instance of ToggleSwitchForm.



54
55
56
# File 'lib/yattho/forms/toggle_switch_form.rb', line 54

def initialize(**system_arguments)
  @system_arguments = system_arguments
end

Class Method Details

.inherited(base) ⇒ Object

Define the form on subclasses so render(Subclass.new) works as expected.



35
36
37
38
39
40
41
42
43
# File 'lib/yattho/forms/toggle_switch_form.rb', line 35

def self.inherited(base)
  base.form do |toggle_switch_form|
    input = Dsl::ToggleSwitchInput.new(
      builder: toggle_switch_form.builder, form: self, **@system_arguments
    )

    toggle_switch_form.send(:add_input, input)
  end
end

.new(**options) ⇒ Object

Override to avoid accepting a builder argument. We create our own builder on render. See the implementation of render_in below.



50
51
52
# File 'lib/yattho/forms/toggle_switch_form.rb', line 50

def self.new(**options)
  allocate.tap { |obj| obj.send(:initialize, **options) }
end

Instance Method Details

#render_in(view_context, &block) ⇒ Object

Unlike other instances of Base, ToggleSwitchForm defines its own form and is not given a Rails form builder on instantiation. We do this mostly for ergonomic reasons; it’s much less verbose than if you were required to call form_with/form_for, etc. That said, the rest of the forms framework assumes the presence of a builder so we create our own here. A builder cannot be constructed without a corresponding view context, which is why we have to override render_in and can’t create it in the initializer.



65
66
67
68
69
70
71
# File 'lib/yattho/forms/toggle_switch_form.rb', line 65

def render_in(view_context, &block)
  @builder = Yattho::Forms::Builder.new(
    nil, nil, view_context, {}
  )

  super
end