diff --git a/app/helpers/trestle/form_helper.rb b/app/helpers/trestle/form_helper.rb index 52161fef..bfa76b5d 100644 --- a/app/helpers/trestle/form_helper.rb +++ b/app/helpers/trestle/form_helper.rb @@ -1,7 +1,5 @@ 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: @@ -9,8 +7,9 @@ module FormHelper # 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
element: - # "keyboard-submit form-loading form-error" + # 3) Sets default Stimulus controllers on the 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. @@ -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 diff --git a/lib/trestle/configuration.rb b/lib/trestle/configuration.rb index a8add562..f4dff6e8 100644 --- a/lib/trestle/configuration.rb +++ b/lib/trestle/configuration.rb @@ -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) diff --git a/spec/trestle/configuration_spec.rb b/spec/trestle/configuration_spec.rb index 7f06ea71..cb81789a 100644 --- a/spec/trestle/configuration_spec.rb +++ b/spec/trestle/configuration_spec.rb @@ -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