Permalink
Browse files

Merge pull request #651 from nashby/fix-issue-643

add ability to specify checked and uncked values for boolean input
  • Loading branch information...
2 parents 4e6508e + 95141eb commit ece4dcaa4396cee8e935ecfb1df63ad6602f68fa @nashby nashby committed Sep 5, 2012
Showing with 32 additions and 3 deletions.
  1. +3 −0 CHANGELOG.md
  2. +11 −3 lib/simple_form/inputs/boolean_input.rb
  3. +18 −0 test/inputs/boolean_input_test.rb
View
@@ -6,6 +6,9 @@
([@nashby](https://github.com/nashby))
### bug fix
+ * Allow to specify checked and uncked values for boolean input
+ ([@nashby](https://github.com/nashby)).
+ Closes [#643](https://github.com/plataformatec/simple_form/issues/629)
* Allow to add additional classes only for wrapper.
([@nashby](https://github.com/nashby)).
Closes [#629](https://github.com/plataformatec/simple_form/issues/629)
@@ -34,8 +34,8 @@ def label_input
# reuse the method for nested boolean style, but with no unchecked value,
# which won't generate the hidden checkbox. This is the default functionality
# in Rails > 3.2.1, and is backported in SimpleForm AV helpers.
- def build_check_box(unchecked_value='0')
- @builder.check_box(attribute_name, input_html_options, '1', unchecked_value)
+ def build_check_box(unchecked_value = unchecked_value)
+ @builder.check_box(attribute_name, input_html_options, checked_value, unchecked_value)
end
# Build a checkbox without generating the hidden field. See
@@ -49,7 +49,7 @@ def build_check_box_without_hidden_field
# we need the hidden field to be *outside* the label (otherwise it
# generates invalid html - html5 only).
def build_hidden_field_for_checkbox
- @builder.hidden_field(attribute_name, :value => '0', :id => nil,
+ @builder.hidden_field(attribute_name, :value => unchecked_value, :id => nil,
:disabled => input_html_options[:disabled],
:name => input_html_options[:name])
end
@@ -65,6 +65,14 @@ def inline_label
def required_by_default?
false
end
+
+ def checked_value
+ options.fetch(:checked_value, '1')
+ end
+
+ def unchecked_value
+ options.fetch(:unchecked_value, '0')
+ end
end
end
end
@@ -14,6 +14,24 @@ class BooleanInputTest < ActionView::TestCase
assert_no_select 'label'
end
+ test 'input uses custom checked value' do
+ @user.action = 'on'
+ with_input_for @user, :action, :boolean, :checked_value => 'on', :unchecked_value => 'off'
+ assert_select 'input[type=checkbox][value=on][checked=checked]'
+ end
+
+ test 'input uses custom unchecked value' do
+ @user.action = 'off'
+ with_input_for @user, :action, :boolean, :checked_value => 'on', :unchecked_value => 'off'
+ assert_select 'input[type=checkbox][value=on]'
+ assert_no_select 'input[checked=checked][value=on]'
+ end
+
+ test 'input generates hidden input with custom unchecked value' do
+ with_input_for @user, :action, :boolean, :checked_value => 'on', :unchecked_value => 'off'
+ assert_select 'input[type=hidden][value=off]'
+ end
+
test 'input uses inline boolean style by default' do
with_input_for @user, :active, :boolean
assert_select 'input.boolean + label.boolean.optional'

0 comments on commit ece4dca

Please sign in to comment.