Class: Primer::Forms::ToggleSwitchForm
- Defined in:
- app/lib/primer/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 < Primer::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(
Primer::Forms::ToggleSwitchForm.new(
name: :foo, label: "Foo", src: "/foo"
)
) %>
Direct Known Subclasses
Class Method Summary collapse
-
.define_form_on(klass) ⇒ Object
Define the form on subclasses so render(Subclass.new) works as expected.
- .inherited(base) ⇒ Object
-
.new(**options) ⇒ Object
Override to avoid accepting a builder argument.
Instance Method Summary collapse
-
#initialize(**system_arguments) ⇒ ToggleSwitchForm
constructor
A new instance of ToggleSwitchForm.
-
#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.
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.
61 62 63 |
# File 'app/lib/primer/forms/toggle_switch_form.rb', line 61 def initialize(**system_arguments) @system_arguments = system_arguments end |
Class Method Details
.define_form_on(klass) ⇒ Object
Define the form on subclasses so render(Subclass.new) works as expected. (this is called directly on this class, but also on classes that inherit from this class)
37 38 39 40 41 42 43 44 45 |
# File 'app/lib/primer/forms/toggle_switch_form.rb', line 37 def self.define_form_on(klass) klass.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 |
.inherited(base) ⇒ Object
47 48 49 50 |
# File 'app/lib/primer/forms/toggle_switch_form.rb', line 47 def self.inherited(base) super define_form_on(base) 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.
57 58 59 |
# File 'app/lib/primer/forms/toggle_switch_form.rb', line 57 def self.new(**) allocate.tap { |obj| obj.send(:initialize, **) } 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.
72 73 74 75 76 77 78 |
# File 'app/lib/primer/forms/toggle_switch_form.rb', line 72 def render_in(view_context, &block) @builder = Primer::Forms::Builder.new( nil, nil, view_context, {} ) super end |