Adding placeholder option to the numeric input

This commit is contained in:
Rafael Mendonça França 2010-10-07 02:55:52 +08:00 committed by Carlos Antonio da Silva
parent 1654d95a85
commit 8a85618022
4 changed files with 31 additions and 11 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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