Class: HotwireClub::Toolbox::OptimisticFormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- HotwireClub::Toolbox::OptimisticFormBuilder
- Defined in:
- app/helpers/hotwire_club/toolbox/optimistic_form_builder.rb
Overview
Form builder that renders the optimistic-UI scaffolding: a hidden holding the turbo-stream(s) the Stimulus controller paints on submit, and (optionally) a hidden field carrying the value being toggled.
Constant Summary collapse
- UNSET =
Sentinel used by OptimisticFormHelper's form defaults to distinguish "no value given" from a real nil/false value.
Object.new
Instance Method Summary collapse
-
#optimistic_hidden_field(attribute_name, value:) ⇒ Object
Explicit hidden field for the value being submitted.
- #optimistic_hidden_field_rendered? ⇒ Boolean
-
#optimistic_template(target = nil, template = nil, &block) ⇒ Object
Wraps the optimistic markup in a the Stimulus controller clones into the DOM on
turbo:submit-start.
Instance Method Details
#optimistic_hidden_field(attribute_name, value:) ⇒ Object
Explicit hidden field for the value being submitted. Calling this marks the field as rendered so the helper skips its automatic injection.
form.optimistic_hidden_field :favorite, value: !photo.favorite
value: is required (a value-less field would be meaningless); pass
false freely, only nil renders an empty value.
38 39 40 41 |
# File 'app/helpers/hotwire_club/toolbox/optimistic_form_builder.rb', line 38 def optimistic_hidden_field(attribute_name, value:) @optimistic_hidden_field_rendered = true hidden_field(attribute_name, value: value) end |
#optimistic_hidden_field_rendered? ⇒ Boolean
43 44 45 |
# File 'app/helpers/hotwire_club/toolbox/optimistic_form_builder.rb', line 43 def optimistic_hidden_field_rendered? !!@optimistic_hidden_field_rendered end |
#optimistic_template(target = nil, template = nil, &block) ⇒ Object
Wraps the optimistic markup in a the Stimulus controller
clones into the DOM on turbo:submit-start.
form.optimistic_template "cart-count", @count + 1
form.optimistic_template { turbo_stream.update("cart-count") { ... } }
Positional form builds a turbo_stream.update for you; block form lets
you author the stream(s) yourself and ignores +target+/+template+.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/helpers/hotwire_club/toolbox/optimistic_form_builder.rb', line 19 def optimistic_template(target = nil, template = nil, &block) content = if block @template.capture(&block) else @template.turbo_stream.turbo_stream_action_tag(:update, target: target, template: template) end @template.content_tag("template", data: { optimistic_form_target: "template" }) do content end end |