Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to specify checked and uncked values for boolean input #651

Merged
merged 2 commits into from Sep 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions lib/simple_form/inputs/boolean_input.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
18 changes: 18 additions & 0 deletions test/inputs/boolean_input_test.rb
Expand Up @@ -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]'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't ensure it's not checked, right? Perhaps it could have another line with assert_no_select 'input[checked]' or something like that? And may need to check if the hidden field is being generated with the correct unchecked_value (same in the other test)?

What do you guys think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need to ensure this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca @carlosantoniodasilva right, I've just pushed these tests. Please take a look.

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'
Expand Down