From 5b1471fdeee6bd8bf73a9382e60116c84712fc6c Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sat, 3 Sep 2011 11:25:53 -0300 Subject: [PATCH] Add input type range. Closes #308 and #290 --- lib/simple_form/form_builder.rb | 1 + lib/simple_form/inputs.rb | 6 +- lib/simple_form/inputs/range_input.rb | 16 ++++ test/inputs_test.rb | 128 +++++++++++++++++++------- 4 files changed, 115 insertions(+), 36 deletions(-) create mode 100644 lib/simple_form/inputs/range_input.rb diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index a7a96f57..90c3453e 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -10,6 +10,7 @@ module SimpleForm 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 diff --git a/lib/simple_form/inputs.rb b/lib/simple_form/inputs.rb index af874051..2af2755c 100644 --- a/lib/simple_form/inputs.rb +++ b/lib/simple_form/inputs.rb @@ -10,7 +10,9 @@ module SimpleForm 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 \ No newline at end of file +end diff --git a/lib/simple_form/inputs/range_input.rb b/lib/simple_form/inputs/range_input.rb new file mode 100644 index 00000000..8304f6ed --- /dev/null +++ b/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 diff --git a/test/inputs_test.rb b/test/inputs_test.rb index bf50b800..823df4bc 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -229,6 +229,22 @@ class InputTest < ActionView::TestCase 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 @@ class InputTest < ActionView::TestCase 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,24 +378,6 @@ class InputTest < ActionView::TestCase 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 - swap SimpleForm, :html5 => false do - with_input_for @user, :age, :integer - assert_no_select "input[type=number]" - assert_no_select "input#user_age[text]" - end - end - - test 'when not using HTML5 input should not use min or max or step attributes' do - swap SimpleForm, :html5 => false do - with_input_for @validating_user, :age, :integer - assert_no_select "input[min]" - assert_no_select "input[max]" - assert_no_select "input[step]" - 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 @@ -408,6 +390,84 @@ class InputTest < ActionView::TestCase 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 + swap SimpleForm, :html5 => false do + with_input_for @user, :age, :integer + assert_no_select "input[type=number]" + assert_select "input#user_age[type=text]" + end + end + + 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]" + assert_no_select "input[max]" + assert_no_select "input[step]" + end + end + + # 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 '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 + # BooleanInput test 'input should generate a checkbox by default for boolean attributes' do with_input_for @user, :active, :boolean