class SimpleForm::Inputs::BooleanInput

Public Instance Methods

input(wrapper_options = nil) click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 5
def input(wrapper_options = nil)
  merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

  if nested_boolean_style?
    build_hidden_field_for_checkbox +
      template.label_tag(nil, class: boolean_label_class) {
        build_check_box_without_hidden_field(merged_input_options) +
          inline_label
      }
  else
    if include_hidden?
      build_check_box(unchecked_value, merged_input_options)
    else
      build_check_box_without_hidden_field(merged_input_options)
    end
  end
end
label_input(wrapper_options = nil) click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 23
def label_input(wrapper_options = nil)
  if options[:label] == false || inline_label?
    input(wrapper_options)
  elsif nested_boolean_style?
    html_options = label_html_options.dup
    html_options[:class] ||= []
    html_options[:class].push(boolean_label_class) if boolean_label_class

    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

    build_hidden_field_for_checkbox +
      @builder.label(label_target, html_options) {
        build_check_box_without_hidden_field(merged_input_options) + label_text
      }
  else
    input(wrapper_options) + label(wrapper_options)
  end
end

Private Instance Methods

boolean_label_class() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 44
def boolean_label_class
  options[:boolean_label_class] || SimpleForm.boolean_label_class
end
build_check_box(unchecked_value, options) click to toggle source

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.

# File lib/simple_form/inputs/boolean_input.rb, line 52
def build_check_box(unchecked_value, options)
  @builder.check_box(attribute_name, options, checked_value, unchecked_value)
end
build_check_box_without_hidden_field(options) click to toggle source

Build a checkbox without generating the hidden field. See build_hidden_field_for_checkbox for more info.

# File lib/simple_form/inputs/boolean_input.rb, line 58
def build_check_box_without_hidden_field(options)
  build_check_box(nil, options)
end
build_hidden_field_for_checkbox() click to toggle source

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).

# File lib/simple_form/inputs/boolean_input.rb, line 66
def build_hidden_field_for_checkbox
  return "".html_safe if !include_hidden? || !unchecked_value
  options = { value: unchecked_value, id: nil, disabled: input_html_options[:disabled] }
  options[:name] = input_html_options[:name] if input_html_options.key?(:name)
  options[:form] = input_html_options[:form] if input_html_options.key?(:form)

  @builder.hidden_field(attribute_name, options)
end
checked_value() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 99
def checked_value
  options.fetch(:checked_value, '1')
end
include_hidden?() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 95
def include_hidden?
  options.fetch(:include_hidden, true)
end
inline_label() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 79
def inline_label
  inline_option = options[:inline_label]

  if inline_option
    label = inline_option == true ? label_text : html_escape(inline_option)
    " #{label}".html_safe
  end
end
inline_label?() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 75
def inline_label?
  nested_boolean_style? && options[:inline_label]
end
required_by_default?() click to toggle source

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.

# File lib/simple_form/inputs/boolean_input.rb, line 91
def required_by_default?
  false
end
unchecked_value() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 103
def unchecked_value
  options.fetch(:unchecked_value, '0')
end