1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00
heartcombo--simple_form/lib/simple_form/inputs/boolean_input.rb
2012-02-29 16:36:25 -05:00

64 lines
2.2 KiB
Ruby

module SimpleForm
module Inputs
class BooleanInput < Base
def input
if nested_boolean_style?
build_hidden_field_for_checkbox +
template.label_tag(nil, :class => "checkbox") {
build_check_box_without_hidden_field + options[:inline_label]
}
else
build_check_box
end
end
def label_input
if options[:label] == false
input
elsif nested_boolean_style?
html_options = label_html_options.dup
html_options[:class].push(:checkbox)
build_hidden_field_for_checkbox +
@builder.label(label_target, html_options) {
build_check_box_without_hidden_field + label_text
}
else
input + label
end
end
private
# Build a checkbox tag using default unchecked value. This allows us to
# 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)
end
# Build a checkbox without generating the hidden field. See
# #build_hidden_field_for_checkbox for more info.
def build_check_box_without_hidden_field
build_check_box(nil)
end
# Create a hidden field for the current checkbox, so we can simulate Rails
# functionality with hidden + checkbox, but under a nested context, where
# 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,
:disabled => input_html_options[:disabled])
end
# Booleans are not required by default because in most of the cases
# it makes no sense marking them as required. The only exception is
# Terms of Use usually presented at most sites sign up screen.
def required_by_default?
false
end
end
end
end