From 8a856180221d781ceb30516b0ad2a6ceefd0c075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 7 Oct 2010 02:55:52 +0800 Subject: [PATCH] Adding placeholder option to the numeric input --- lib/simple_form/inputs/base.rb | 8 ++++++++ lib/simple_form/inputs/numeric_input.rb | 7 ++++--- lib/simple_form/inputs/string_input.rb | 8 -------- test/inputs_test.rb | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index ba230601..2115116a 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -76,6 +76,14 @@ module SimpleForm reflection ? object.class.validators_on(reflection.name) : [] end + def has_placeholder? + options[:placeholder] != false && placeholder.present? + end + + def placeholder + @placeholder ||= options[:placeholder] || translate(:placeholders) + end + def attribute_required_by_default? SimpleForm.required_by_default end diff --git a/lib/simple_form/inputs/numeric_input.rb b/lib/simple_form/inputs/numeric_input.rb index 981a522d..eb1462d5 100644 --- a/lib/simple_form/inputs/numeric_input.rb +++ b/lib/simple_form/inputs/numeric_input.rb @@ -7,9 +7,10 @@ module SimpleForm def input_html_options input_options = super - input_options[:type] ||= "number" - input_options[:size] ||= SimpleForm.default_input_size - input_options[:step] ||= 1 if integer? + input_options[:type] ||= "number" + input_options[:size] ||= SimpleForm.default_input_size + input_options[:step] ||= 1 if integer? + input_options[:placeholder] ||= placeholder if has_placeholder? infer_attributes_from_validations(input_options) diff --git a/lib/simple_form/inputs/string_input.rb b/lib/simple_form/inputs/string_input.rb index b471b558..e7422915 100644 --- a/lib/simple_form/inputs/string_input.rb +++ b/lib/simple_form/inputs/string_input.rb @@ -23,14 +23,6 @@ module SimpleForm def limit column && column.limit end - - def has_placeholder? - options[:placeholder] != false && placeholder.present? - end - - def placeholder - @placeholder ||= options[:placeholder] || translate(:placeholders) - end end end end diff --git a/test/inputs_test.rb b/test/inputs_test.rb index 7581ed84..649278a2 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -150,6 +150,25 @@ class InputTest < ActionView::TestCase assert_select 'input[step=1]' end + test 'numeric input should not generate placeholder by default' do + with_input_for @user, :age, :integer + assert_no_select 'input[placeholder]' + end + + test 'numeric input should accept the placeholder option' do + with_input_for @user, :age, :integer, :placeholder => 'Put in some text' + assert_select 'input.integer[placeholder=Put in some text]' + end + + test 'numeric input should use i18n to translate placeholder text' do + store_translations(:en, :simple_form => { :placeholders => { :user => { + :age => 'Age goes here' + } } }) do + with_input_for @user, :age, :integer + assert_select 'input.integer[placeholder=Age goes here]' + 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