65 lines
2.2 KiB
Ruby
65 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
|
|
}
|
|
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
|