Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions app/helpers/trestle/form_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
module Trestle
module FormHelper
DEFAULT_FORM_CONTROLLERS = %w(keyboard-submit form-loading form-error)

# Generates a form for a resource using Rails' #form_for helper.
#
# In addition to delegating to #form_for, this helper method:
#
# 1) Sets the default form builder to `Trestle::Form::Builder`.
# 2) Sets the default :as option to match the parameter name
# expected by the admin.
# 3) Sets default Stimulus controllers on the <form> element:
# "keyboard-submit form-loading form-error"
# 3) Sets default Stimulus controllers on the <form> element
# from `Trestle.config.default_form_controllers`.
# (defaults to: "keyboard-submit form-loading form-error")
# 4) Sets a null/identity ActionView::Base.field_error_proc as
# errors are handled by Trestle::Form::Fields::FormGroup.
# 5) Exposes the yielded form builder instance via the `form` helper.
Expand All @@ -26,7 +25,7 @@ def trestle_form_for(instance, **options, &block)
options[:as] ||= admin.parameter_name

options[:data] ||= {}
options[:data][:controller] = (DEFAULT_FORM_CONTROLLERS + Array(options[:data][:controller])).join(" ")
options[:data][:controller] = (Trestle.config.default_form_controllers + Array(options[:data][:controller])).join(" ")

form_for(instance, **options) do |f|
with_identity_field_error_proc do
Expand Down
3 changes: 3 additions & 0 deletions lib/trestle/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def helper(*helpers, &block)
# Default adapter class used by all admin resources
option :default_adapter, Adapters.compose(Adapters::ActiveRecordAdapter, Adapters::DraperAdapter)

# List of Stimulus controllers to add to forms by default
option :default_form_controllers, %w(keyboard-submit form-loading form-error)

# Register a custom form field class
def form_field(name, field)
Form::Builder.register(name, field)
Expand Down
4 changes: 4 additions & 0 deletions spec/trestle/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
expect(config.default_adapter.ancestors).to include(Trestle::Adapters::ActiveRecordAdapter, Trestle::Adapters::DraperAdapter)
end

it "has a default form controllers configuration option" do
expect(config).to have_accessor(:default_form_controllers).with_default(["keyboard-submit", "form-loading", "form-error"])
end

it "has a root breadcrumbs configuration option" do
expect(config).to have_accessor(:root_breadcrumbs).with_default([Trestle::Breadcrumb.new("Home", "/admin")])
end
Expand Down