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

Added possibility to manually decide whether a check box is checked #909

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion lib/formtastic/inputs/boolean_input.rb
Expand Up @@ -101,7 +101,9 @@ def input_html_options_name
end end


def checked? def checked?
if defined? ActionView::Helpers::InstanceTag if input_html_options.key?(:checked)
input_html_options[:checked]
elsif defined? ActionView::Helpers::InstanceTag
object && ActionView::Helpers::InstanceTag.check_box_checked?(object.send(method), checked_value) object && ActionView::Helpers::InstanceTag.check_box_checked?(object.send(method), checked_value)
else else
object && boolean_checked?(object.send(method), checked_value) object && boolean_checked?(object.send(method), checked_value)
Expand Down
16 changes: 16 additions & 0 deletions spec/inputs/boolean_input_spec.rb
Expand Up @@ -41,6 +41,22 @@
output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]') output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]')
end end


it 'should generate a checked input if :input_html => { :checked => true } is passed' do
@output_buffer = ''
concat(semantic_form_for(@new_post) do |builder|
concat(builder.input(:answer_comments, :as => :boolean, :input_html => {:checked => true}))
end)
output_buffer.should have_tag('form li label input[@checked="checked"]')
end

it 'should not generate a checked input if :input_html => { :checked => false } is passed' do
@output_buffer = ''
concat(semantic_form_for(@new_post) do |builder|
concat(builder.input(:answer_comments, :as => :boolean, :input_html => {:checked => false}))
end)
output_buffer.should_not have_tag('form li label input[@checked="checked"]')
end

it 'should generate a checked input if object.method returns true' do it 'should generate a checked input if object.method returns true' do
output_buffer.should have_tag('form li label input[@checked="checked"]') output_buffer.should have_tag('form li label input[@checked="checked"]')
output_buffer.should have_tag('form li input[@name="post[allow_comments]"]', :count => 2) output_buffer.should have_tag('form li input[@name="post[allow_comments]"]', :count => 2)
Expand Down