class SimpleForm::Inputs::BooleanInput

Public Instance Methods

input() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 4
def input
  if nested_boolean_style?
    build_hidden_field_for_checkbox +
      template.label_tag(nil, :class => "checkbox") {
        build_check_box_without_hidden_field + inline_label
      }
  else
    build_check_box
  end
end
label_input() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 15
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 Instance Methods

build_check_box(unchecked_value = unchecked_value) 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 37
def build_check_box(unchecked_value = unchecked_value)
  @builder.check_box(attribute_name, input_html_options, checked_value, unchecked_value)
end
build_check_box_without_hidden_field() 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 43
def build_check_box_without_hidden_field
  build_check_box(nil)
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 51
def build_hidden_field_for_checkbox
  @builder.hidden_field(attribute_name, :value => unchecked_value, :id => nil,
                        :disabled => input_html_options[:disabled],
                        :name => input_html_options[:name])
end
checked_value() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 69
def checked_value
  options.fetch(:checked_value, '1')
end
inline_label() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 57
def inline_label
  inline_option = options[:inline_label]
  inline_option == true ? label_text : inline_option
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 65
def required_by_default?
  false
end
unchecked_value() click to toggle source
# File lib/simple_form/inputs/boolean_input.rb, line 73
def unchecked_value
  options.fetch(:unchecked_value, '0')
end