2010-07-06 09:24:12 +00:00
|
|
|
module SimpleForm
|
|
|
|
module Inputs
|
|
|
|
class BooleanInput < Base
|
|
|
|
def input
|
2012-01-30 13:15:05 +00:00
|
|
|
if nested_boolean_style?
|
2012-01-31 17:57:45 +00:00
|
|
|
build_hidden_field_for_checkbox +
|
2013-01-28 21:02:59 +00:00
|
|
|
template.label_tag(nil, class: "checkbox") {
|
2012-04-30 05:34:40 +00:00
|
|
|
build_check_box_without_hidden_field + inline_label
|
2012-01-30 18:38:28 +00:00
|
|
|
}
|
2012-01-30 13:15:05 +00:00
|
|
|
else
|
|
|
|
build_check_box
|
|
|
|
end
|
2010-07-06 09:24:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def label_input
|
2012-01-26 13:30:08 +00:00
|
|
|
if options[:label] == false
|
|
|
|
input
|
2012-01-26 18:17:12 +00:00
|
|
|
elsif nested_boolean_style?
|
2012-02-03 21:58:06 +00:00
|
|
|
html_options = label_html_options.dup
|
2013-05-28 20:12:18 +00:00
|
|
|
html_options[:class] ||= []
|
2012-02-03 21:58:06 +00:00
|
|
|
html_options[:class].push(:checkbox)
|
|
|
|
|
2012-01-31 17:57:45 +00:00
|
|
|
build_hidden_field_for_checkbox +
|
2012-02-03 21:58:06 +00:00
|
|
|
@builder.label(label_target, html_options) {
|
2012-01-30 18:38:28 +00:00
|
|
|
build_check_box_without_hidden_field + label_text
|
|
|
|
}
|
2012-01-26 13:30:08 +00:00
|
|
|
else
|
|
|
|
input + label
|
|
|
|
end
|
2010-07-06 09:24:12 +00:00
|
|
|
end
|
|
|
|
|
2011-09-03 09:13:29 +00:00
|
|
|
private
|
2010-07-06 09:24:12 +00:00
|
|
|
|
2012-01-27 12:45:36 +00:00
|
|
|
# Build a checkbox tag using default unchecked value. This allows us to
|
2012-01-30 16:31:07 +00:00
|
|
|
# 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.
|
2012-08-26 12:06:19 +00:00
|
|
|
def build_check_box(unchecked_value = unchecked_value)
|
|
|
|
@builder.check_box(attribute_name, input_html_options, checked_value, unchecked_value)
|
2012-01-27 12:45:36 +00:00
|
|
|
end
|
|
|
|
|
2012-01-30 16:31:07 +00:00
|
|
|
# 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
|
2012-12-27 16:03:46 +00:00
|
|
|
options = { value: unchecked_value, id: nil, disabled: input_html_options[:disabled] }
|
|
|
|
options[:name] = input_html_options[:name] if input_html_options.has_key?(:name)
|
|
|
|
|
|
|
|
@builder.hidden_field(attribute_name, options)
|
2012-01-30 16:31:07 +00:00
|
|
|
end
|
|
|
|
|
2012-04-30 14:24:01 +00:00
|
|
|
def inline_label
|
2012-04-30 14:27:19 +00:00
|
|
|
inline_option = options[:inline_label]
|
|
|
|
inline_option == true ? label_text : inline_option
|
2012-04-30 14:24:01 +00:00
|
|
|
end
|
|
|
|
|
2010-07-06 09:24:12 +00:00
|
|
|
# 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.
|
2011-12-04 12:00:39 +00:00
|
|
|
def required_by_default?
|
2010-07-06 09:24:12 +00:00
|
|
|
false
|
|
|
|
end
|
2012-08-26 12:06:19 +00:00
|
|
|
|
|
|
|
def checked_value
|
|
|
|
options.fetch(:checked_value, '1')
|
|
|
|
end
|
|
|
|
|
|
|
|
def unchecked_value
|
|
|
|
options.fetch(:unchecked_value, '0')
|
|
|
|
end
|
2010-07-06 09:24:12 +00:00
|
|
|
end
|
|
|
|
end
|
2012-01-26 13:30:08 +00:00
|
|
|
end
|