Skip to content
Browse files

Add input type range. Closes #308 and #290

  • Loading branch information...
1 parent 6b02498 commit 5b1471fdeee6bd8bf73a9382e60116c84712fc6c @carlosantoniodasilva carlosantoniodasilva committed Sep 3, 2011
Showing with 107 additions and 28 deletions.
  1. +1 −0 lib/simple_form/form_builder.rb
  2. +4 −2 lib/simple_form/inputs.rb
  3. +16 −0 lib/simple_form/inputs/range_input.rb
  4. +86 −26 test/inputs_test.rb
View
1 lib/simple_form/form_builder.rb
@@ -10,6 +10,7 @@ class FormBuilder < ActionView::Helpers::FormBuilder
map_type :string, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
map_type :password, :to => SimpleForm::Inputs::PasswordInput
map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
+ map_type :range, :to => SimpleForm::Inputs::RangeInput
map_type :select, :radio, :check_boxes, :to => SimpleForm::Inputs::CollectionInput
map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
View
6 lib/simple_form/inputs.rb
@@ -10,7 +10,9 @@ module Inputs
autoload :NumericInput, 'simple_form/inputs/numeric_input'
autoload :PasswordInput, 'simple_form/inputs/password_input'
autoload :PriorityInput, 'simple_form/inputs/priority_input'
- autoload :TextInput, 'simple_form/inputs/text_input'
+ autoload :PasswordInput, 'simple_form/inputs/password_input'
+ autoload :RangeInput, 'simple_form/inputs/range_input'
autoload :StringInput, 'simple_form/inputs/string_input'
+ autoload :TextInput, 'simple_form/inputs/text_input'
end
-end
+end
View
16 lib/simple_form/inputs/range_input.rb
@@ -0,0 +1,16 @@
+module SimpleForm
+ module Inputs
+ class RangeInput < NumericInput
+ disable :placeholder
+
+ def input
+ if SimpleForm.html5
+ input_html_options[:type] ||= "range"
+ input_html_options[:step] ||= 1
+ end
+
+ super
+ end
+ end
+ end
+end
View
112 test/inputs_test.rb
@@ -229,6 +229,22 @@ def with_input_for(object, attribute_name, type, options={})
end
end
+ test 'input should infer pattern from attributes when pattern is true' do
+ with_input_for @other_validating_user, :country, :string, :pattern => true
+ assert_select 'input[pattern="\w+"]'
+ end
+
+ test 'input should use given pattern from attributes' do
+ with_input_for @other_validating_user, :country, :string, :pattern => "\\d+"
+ assert_select 'input[pattern="\d+"]'
+ end
+
+ test 'input should fail if pattern is true but no pattern exists' do
+ assert_raise RuntimeError do
+ with_input_for @other_validating_user, :name, :string, :pattern => true
+ end
+ end
+
# NumericInput
test 'input should generate an integer text field for integer attributes ' do
with_input_for @user, :age, :integer
@@ -335,22 +351,6 @@ def with_input_for(object, attribute_name, type, options={})
assert_select 'input[max=119]'
end
- test 'input should infer pattern from attributes when pattern is true' do
- with_input_for @other_validating_user, :country, :string, :pattern => true
- assert_select 'input[pattern="\w+"]'
- end
-
- test 'input should use given pattern from attributes' do
- with_input_for @other_validating_user, :country, :string, :pattern => "\\d+"
- assert_select 'input[pattern="\d+"]'
- end
-
- test 'input should fail if pattern is true but no pattern exists' do
- assert_raise RuntimeError do
- with_input_for @other_validating_user, :name, :string, :pattern => true
- end
- end
-
test 'input should have step value of any except for integer attribute' do
with_input_for @validating_user, :age, :float
assert_select 'input[step="any"]'
@@ -378,16 +378,28 @@ def with_input_for(object, attribute_name, type, options={})
end
end
+ [:integer, :float, :decimal].each do |type|
+ test "#{type} input should infer min value from attributes with greater than or equal validation" do
+ with_input_for @validating_user, :age, type
+ assert_select 'input[min=18]'
+ end
+
+ test "#{type} input should infer the max value from attributes with less than or equal to validation" do
+ with_input_for @validating_user, :age, type
+ assert_select 'input[max=99]'
+ end
+ end
+
# Numeric input but HTML5 disabled
- test ' when not using HTML5 input should not generate field with type number and use text instead' do
+ test 'when not using HTML5 input should not generate field with type number and use text instead' do
swap SimpleForm, :html5 => false do
with_input_for @user, :age, :integer
assert_no_select "input[type=number]"
- assert_no_select "input#user_age[text]"
+ assert_select "input#user_age[type=text]"
end
end
- test 'when not using HTML5 input should not use min or max or step attributes' do
+ test 'when not using HTML5 input should not use min or max or step attributes for numeric type' do
swap SimpleForm, :html5 => false do
with_input_for @validating_user, :age, :integer
assert_no_select "input[min]"
@@ -396,15 +408,63 @@ def with_input_for(object, attribute_name, type, options={})
end
end
- [:integer, :float, :decimal].each do |type|
- test "#{type} input should infer min value from attributes with greater than or equal validation" do
- with_input_for @validating_user, :age, type
- assert_select 'input[min=18]'
+ # RangeInput
+ test 'range input generates a input type range, based on numeric input' do
+ with_input_for @user, :age, :range
+ assert_select "input#user_age.range[type=range]"
+ end
+
+ test 'range input does not generate placeholder' do
+ with_input_for @user, :age, :range, :placeholder => "Select your age"
+ assert_select "input[type=range]"
+ assert_no_select "input[placeholder]"
+ end
+
+ test 'range input allows givin min and max attributes' do
+ with_input_for @user, :age, :range, :input_html => { :min => 10, :max => 50 }
+ assert_select "input[type=range][min=10][max=50]"
+ end
+
+ test 'range input infers min and max attributes from validations' do
+ with_input_for @validating_user, :age, :range
+ assert_select "input[type=range][min=18][max=99]"
+ end
+
+ test 'range input add default step attribute' do
+ with_input_for @validating_user, :age, :range
+ assert_select "input[type=range][step=1]"
+ end
+
+ test 'range input allows givin a step through input html options' do
+ with_input_for @validating_user, :age, :range, :input_html => { :step => 2 }
+ assert_select "input[type=range][step=2]"
+ end
+
+ test 'range input should not generate min attribute by default' do
+ with_input_for @user, :age, :range
+ assert_no_select 'input[min]'
+ end
+
+ test 'range input should not generate max attribute by default' do
+ with_input_for @user, :age, :range
+ assert_no_select 'input[max]'
+ end
+
+ # RangeInput iwth HTML5 disabled
+ test 'when not using HTML5, range input does not generate field with range type, and use text instead' do
+ swap SimpleForm, :html5 => false do
+ with_input_for @user, :age, :range
+ assert_no_select "input[type=number]"
+ assert_select "input[type=text]"
end
+ end
- test "#{type} input should infer the max value from attributes with less than or equal to validation" do
- with_input_for @validating_user, :age, type
- assert_select 'input[max=99]'
+ test 'when not using HTML5, range input should not use min or max or step attributes' do
+ swap SimpleForm, :html5 => false do
+ with_input_for @validating_user, :age, :range
+ assert_no_select "input[min]"
+ assert_no_select "input[max]"
+ assert_no_select "input[step]"
end
end

0 comments on commit 5b1471f

Please sign in to comment.
Something went wrong with that request. Please try again.