Skip to content

Commit

Permalink
[SQUASH LATER] Validate default_form_attributes config argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sharshenov committed Apr 19, 2020
1 parent ce7bc3d commit 5d2d6a7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions bootstrap_form.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions lib/bootstrap_form/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 17 additions & 1 deletion test/bootstrap_configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5d2d6a7

Please # to comment.