class Primer::Forms::ToggleSwitchForm
) %>
)
name: :foo, label: “Foo”, src: “/foo”
Primer::Forms::ToggleSwitchForm.new(
<%= render(
# app/views/some_view.html.erb
Directly:
<%= render(MyToggleForm.new) %>
# app/views/some_view.html.erb
end
end
super(name: :foo, label: “Foo”, src: “/foo”, **system_arguments)
def initialize(**system_arguments)
class MyToggleForm < Primer::Forms::ToggleSwitchForm
# app/forms/my_toggle_form.rb
Via inheritance:
ToggleSwitchForm can be used directly or via inheritance.
class.
that have other fields, etc. Instead they should be used independently via this
upon click. For that reason they are not designed to be used in “regular” forms
Toggle switches are designed to submit an on/off value to the server immediately
def self.define_form_on(klass)
(this is called directly on this class, but also on classes
Define the form on subclasses so render(Subclass.new) works as expected.
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
def self.inherited(base)
def self.inherited(base) super define_form_on(base) end
def self.new(**options)
Override to avoid accepting a builder argument. We create our own builder
def self.new(**options) allocate.tap { |obj| obj.send(:initialize, **options) } end
def initialize(**system_arguments)
def initialize(**system_arguments) @system_arguments = system_arguments end
def render_in(view_context, &block)
cannot be constructed without a corresponding view context, which is why
assumes the presence of a builder so we create our own here. A builder
call form_with/form_for, etc. That said, the rest of the forms framework
ergonomic reasons; it's much less verbose than if you were required to
is not given a Rails form builder on instantiation. We do this mostly for
Unlike other instances of Base, ToggleSwitchForm defines its own form and
def render_in(view_context, &block) @builder = Primer::Forms::Builder.new( nil, nil, view_context, {} ) super end