diff --git a/bootstrap_form.gemspec b/bootstrap_form.gemspec index 1af225852..00f6e6171 100644 --- a/bootstrap_form.gemspec +++ b/bootstrap_form.gemspec @@ -15,6 +15,8 @@ Gem::Specification.new do |s| "easy to create beautiful-looking forms using Bootstrap 4" s.license = "MIT" + s.post_install_message = "Default form attribute role=\"form\" will be dropped in 5.0.0" + s.files = `git ls-files -z`.split("\x0").reject do |f| f.match(%r{^(test)/}) end diff --git a/lib/bootstrap_form/configuration.rb b/lib/bootstrap_form/configuration.rb index ee447e9bd..9b010b081 100644 --- a/lib/bootstrap_form/configuration.rb +++ b/lib/bootstrap_form/configuration.rb @@ -2,16 +2,22 @@ module BootstrapForm class Configuration - DEFAULT = { - default_form_attributes: { - role: "form" - } - }.freeze + def default_form_attributes=(attributes) + case attributes + when nil + @default_form_attributes = {} + when Hash + @default_form_attributes = attributes + else + raise ArgumentError, "Unsupported default_form_attributes #{attributes.inspect}" + end + end - DEFAULT.keys.each { |key| attr_accessor key } + def default_form_attributes + return @default_form_attributes if defined? @default_form_attributes - def initialize - DEFAULT.each { |key, value| send("#{key}=", value) } + # TODO: Return blank hash ({}) in 5.0.0. Role "form" for form tags is redundant and makes W3C to raise a warning. + { role: "form" } end end end diff --git a/test/bootstrap_configuration_test.rb b/test/bootstrap_configuration_test.rb index 6c0269f1b..cce4f3926 100644 --- a/test/bootstrap_configuration_test.rb +++ b/test/bootstrap_configuration_test.rb @@ -7,10 +7,26 @@ class BootstrapConfigurationTest < ActionView::TestCase assert_equal({ role: "form" }, config.default_form_attributes) end - test "allows to set default_form_attributes" do + test "allows to set default_form_attributes with custom value" do config = BootstrapForm::Configuration.new config.default_form_attributes = { foo: "bar" } assert_equal({ foo: "bar" }, config.default_form_attributes) end + + test "allows to set default_form_attributes with nil" do + config = BootstrapForm::Configuration.new + config.default_form_attributes = nil + + assert_equal({ }, config.default_form_attributes) + end + + test "does not allow to set default_form_attributes with unsupported value" do + config = BootstrapForm::Configuration.new + + exception = assert_raises ArgumentError do + config.default_form_attributes = [1,2,3] + end + assert_equal('Unsupported default_form_attributes [1, 2, 3]', exception.message) + end end