Class: Formtastic::Inputs::RangeInput

Inherits:
Object
  • Object
show all
Includes:
Base, Base::Stringish
Defined in:
lib/formtastic/inputs/range_input.rb

Overview

TODO:

Is it still correct for this to be Stringish?

Outputs a simple <label> with a HTML5 <input type="range"> wrapped in the standard <li> wrapper. This is an alternative input choice to a number input.

Sensible default for the min, max and step attributes are found by reflecting on the model's validations. When validations are not provided, the min and step default to 1 and the max default to 100. An IndeterminableMinimumAttributeError exception will be raised when the following conditions are all true:

  • you haven't specified a :min or :max for the input
  • the model's database column type is a :float or :decimal
  • the validation uses :less_than or :greater_than

The solution is to either:

  • manually specify the :min or :max for the input
  • change the database column type to an :integer (if appropriate)
  • change the validations to use :less_than_or_equal_to or :greater_than_or_equal_to

Examples:

Full form context and output


<%= semantic_form_for(@user) do |f| %>
  <%= f.inputs do %>
    <%= f.input :shoe_size, :as => :range %>
  <% end %>
<% end %>

<form...>
  <fieldset>
    <ol>
      <li class="numeric">
        <label for="user_shoe_size">Shoe size</label>
        <input type="range" id="user_shoe_size" name="user[shoe_size]" min="1" max="100" step="1">
      </li>
    </ol>
  </fieldset>
</form>

Default HTML5 min/max/step attributes are detected from the numericality validations


class Person < ActiveRecord::Base
  validates_numericality_of :age, 
    :less_than_or_equal_to => 100, 
    :greater_than_or_equal_to => 18, 
    :only_integer => true
end

<%= f.input :age, :as => :number %>

<li class="numeric">
  <label for="persom_age">Age</label>
  <input type="range" id="person_age" name="person[age]" min="18" max="100" step="1">
</li>

Pass attributes down to the <input> tag with :input_html

<%= f.input :shoe_size, :as => :range, :input_html => { :min => 3, :max => 15, :step => 1, :class => "special" } %>

Min/max/step also work as options

<%= f.input :shoe_size, :as => :range, :min => 3, :max => 15, :step => 1, :input_html => { :class => "special" } %>

Use :in with a Range as a shortcut for :min/:max

<%= f.input :shoe_size, :as => :range, :in => 3..15, :step => 1 %>
<%= f.input :shoe_size, :as => :range, :input_html => { :in => 3..15, :step => 1 } %>

See Also:

Instance Attribute Summary

Attributes included from Base

#builder, #method, #object, #object_name, #options, #template

Instance Method Summary collapse

Methods included from Base::Stringish

#placeholder_text, #wrapper_html_options

Methods included from Base

#initialize, #warn_and_correct_option!

Methods included from Base::Wrapping

#input_wrapping, #wrapper_dom_id, #wrapper_html_options

Methods included from Base::Labelling

#label_from_options, #label_html, #label_html_options, #label_text, #localized_label, #render_label?, #requirement_text, #requirement_text_or_proc

Methods included from Base::Associations

#association, #association_primary_key, #belongs_to?, #has_many?, #reflection

Methods included from Base::Fileish

#file?

Methods included from Base::Validations

#autofocus?, #column_limit, #limit, #not_required_through_negated_validation!, #not_required_through_negated_validation?, #optional?, #required?, #required_attribute?, #responds_to_global_required?, #validation_integer_only?, #validation_limit, #validation_max, #validation_min, #validation_step, #validations, #validations?, #validator_relevant?

Methods included from Base::Naming

#as, #attributized_method_name, #humanized_method_name, #input_name, #sanitized_method_name, #sanitized_object_name

Methods included from Base::Hints

#hint?, #hint_html, #hint_text, #hint_text_from_options

Methods included from Base::Errors

#error_first_html, #error_html, #error_keys, #error_list_html, #error_none_html, #error_sentence_html, #errors, #errors?

Methods included from Base::Database

#column, #column?

Methods included from Base::Options

#formtastic_options, #input_options

Methods included from Base::Html

#dom_id, #dom_index

Instance Method Details

#in_optionObject



113
114
115
# File 'lib/formtastic/inputs/range_input.rb', line 113

def in_option
  options[:in]
end

#input_html_optionsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/formtastic/inputs/range_input.rb', line 82

def input_html_options
  defaults = super
  
  if in_option
    defaults[:min] = in_option.to_a.min
    defaults[:max] = in_option.to_a.max
  else
    defaults[:min]  ||= min_option
    defaults[:max]  ||= max_option
  end
  defaults[:step] ||= step_option
  defaults
end

#max_optionObject



108
109
110
111
# File 'lib/formtastic/inputs/range_input.rb', line 108

def max_option
  return options[:max] if options.key?(:max)
  validation_max || 100
end

#min_optionObject



103
104
105
106
# File 'lib/formtastic/inputs/range_input.rb', line 103

def min_option
  return options[:min] if options.key?(:min)
  validation_min || 1
end

#step_optionObject



96
97
98
99
100
101
# File 'lib/formtastic/inputs/range_input.rb', line 96

def step_option
  return options[:step] if options.key?(:step)
  return validation_step if validation_step
  return 1 if validation_integer_only?
  1
end

#to_htmlObject



75
76
77
78
79
80
# File 'lib/formtastic/inputs/range_input.rb', line 75

def to_html
  input_wrapping do
    label_html <<
    builder.range_field(method, input_html_options)
  end
end