class Formtastic::Inputs::BooleanInput

Boolean inputs are used to render an input for a single checkbox, typically for attributes with a simple yes/no or true/false value. Boolean inputs are used by default for boolean database columns.

@example Full form context and markup

<%= semantic_form_for @post %>
  <%= f.inputs do %>
    <%= f.input :published, :as => :boolean %>
  <% end %>
<% end %>

<form...>
  <fieldset>
    <ol>
      <li class="boolean" id="post_published_input">
        <input type="hidden" name="post[published]" id="post_published" value="0">
        <label for="post_published">
          <input type="checkbox" name="post[published]" id="post_published" value="1">
          Published?
        </label>
      </li>
    </ol>
  </fieldset>
</form>

@example Set the values for the checked and unchecked states

<%= f.input :published, :checked_value => "yes", :unchecked_value => "no" %>

@see Formtastic::Helpers::InputsHelper#input InputsHelper#input for full documentation of all possible options.

Public Instance Methods

check_box_html() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 65
def check_box_html
  template.check_box_tag("#{object_name}[#{method}]", checked_value, checked?, input_html_options)
end
checked?() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 93
def checked?
  object && boolean_checked?(object.send(method), checked_value) 
end
checked_value() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 73
def checked_value
  options[:checked_value] || '1'
end
hidden_field_html() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 42
def hidden_field_html
  template.hidden_field_tag(input_html_options[:name], unchecked_value, :id => nil, :disabled => input_html_options[:disabled] )
end
input_html_options() click to toggle source
Calls superclass method
# File lib/formtastic/inputs/boolean_input.rb, line 81
def input_html_options
  {:name => input_html_options_name}.merge(super)
end
input_html_options_name() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 85
def input_html_options_name
  if builder.options.key?(:index)
    "#{object_name}[#{builder.options[:index]}][#{method}]"
  else
    "#{object_name}[#{method}]"
  end
end
label_html_options() click to toggle source
Calls superclass method
# File lib/formtastic/inputs/boolean_input.rb, line 54
def label_html_options
  {
    :for => input_html_options[:id],
    :class => super[:class] - ['label'] # remove 'label' class
  }
end
label_text_with_embedded_checkbox() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 61
def label_text_with_embedded_checkbox
  check_box_html << "" << label_text
end
label_with_nested_checkbox() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 46
def label_with_nested_checkbox
  builder.label(
    method,
    label_text_with_embedded_checkbox,
    label_html_options
  )
end
responds_to_global_required?() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 77
def responds_to_global_required?
  false
end
to_html() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 35
def to_html
  input_wrapping do
    hidden_field_html <<
    label_with_nested_checkbox
  end
end
unchecked_value() click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 69
def unchecked_value
  options[:unchecked_value] || '0'
end

Private Instance Methods

boolean_checked?(value, checked_value) click to toggle source
# File lib/formtastic/inputs/boolean_input.rb, line 99
def boolean_checked?(value, checked_value)
  case value
  when TrueClass, FalseClass
    value
  when NilClass
    false
  when Integer
    value == checked_value.to_i
  when String
    value == checked_value
  when Array
    value.include?(checked_value)
  else
    value.to_i != 0
  end
end