Skip to content

Commit

Permalink
default step for number field. closes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
GBH committed Jul 15, 2019
1 parent 1b8b339 commit 304b6a9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/comfy_bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class FormBuilder < ActionView::Helpers::FormBuilder

FIELD_HELPERS = %w[
color_field date_field datetime_field email_field month_field
number_field password_field phone_field range_field search_field text_area
password_field phone_field range_field search_field text_area
text_field time_field url_field week_field
].freeze

Expand Down Expand Up @@ -67,6 +67,20 @@ def #{select_helper}(method, options = {}, html_options = {}, &block)
RUBY_EVAL
end

# Wrapper for the number field. It has default changed from `step: "1"` to `step: "any"`
# to prevent confusion when dealing with decimal numbers.
#
# number_field :amount, step: 5
#
def number_field(method, options = {})
bootstrap = form_bootstrap.scoped(options.delete(:bootstrap))
options.reverse_merge!(step: "any")
return super(method, options) if bootstrap.disabled
draw_form_group(bootstrap, method, options) do
super(method, options)
end
end

# Wrapper for select helper. Boostrap options are sent via options hash:
#
# select :choices, ["a", "b"], bootstrap: {label: {text: "Custom"}}
Expand Down
13 changes: 12 additions & 1 deletion test/comfy_bootstrap_form/fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,18 @@ def test_number_field
expected = <<-HTML
<div class="form-group">
<label for="user_test">Test</label>
<input class="form-control" type="number" name="user[test]" id="user_test"/>
<input class="form-control" type="number" name="user[test]" id="user_test" step="any"/>
</div>
HTML
assert_xml_equal expected, actual
end

def test_number_field_with_custom_step
actual = @builder.number_field(:test, step: "5")
expected = <<-HTML
<div class="form-group">
<label for="user_test">Test</label>
<input class="form-control" type="number" name="user[test]" id="user_test" step="5"/>
</div>
HTML
assert_xml_equal expected, actual
Expand Down

0 comments on commit 304b6a9

Please sign in to comment.