Permalink
Browse files

Numberic type inputs now have type=number for HTML5.

  • Loading branch information...
josevalim committed Jun 1, 2010
1 parent 2f17381 commit 1ef232cf6dd5695defdef7398c4556d080c82155
View
@@ -1,5 +1,6 @@
* enhancements
- * Add error class on wrappers (by github.com/jduff)
+ * Added error class on wrappers (by github.com/jduff)
+ * Changed numeric types to have type=number for HTML5
== 1.2.0
View
@@ -236,9 +236,9 @@ SimpleForm comes with a lot of default mappings:
text text area text
file file field string, responding to file methods
hidden hidden field -
- integer text field integer
- float text field float
- decimal text field decimal
+ integer number field integer
+ float number field float
+ decimal number field decimal
datetime datetime select datetime/timestamp
date date select date
time time select time
@@ -53,4 +53,4 @@ def simple_#{helper}(record_or_name_or_array, *args, &block)
end
end
-ActionView::Base.send :include, SimpleForm::ActionViewExtensions::FormHelper
+ActionView::Helpers.send :include, SimpleForm::ActionViewExtensions::FormHelper
@@ -14,7 +14,7 @@ def error_text
end
def error_html_options
- html_options_for(:error, :error)
+ html_options_for(:error, [:error])
end
protected
@@ -14,7 +14,7 @@ def hint_text
end
def hint_html_options
- html_options_for(:hint, :hint)
+ html_options_for(:hint, [:hint])
end
end
end
@@ -36,7 +36,7 @@ def label_target
end
def label_html_options
- label_options = html_options_for(:label, input_type, required_class)
+ label_options = html_options_for(:label, [input_type, required_class])
label_options[:for] = options[:input_html][:id] if options.key?(:input_html)
label_options
end
@@ -17,13 +17,13 @@ def wrapper_class
options[:wrapper_class] || SimpleForm.wrapper_class
end
- def errors_class
+ def wrapper_error_class
options[:wrapper_error_class] || SimpleForm.wrapper_error_class
end
def wrapper_html_options
- css_classes = [wrapper_class, input_type, required_class]
- css_classes << errors_class if object && errors.present?
+ css_classes = input_html_classes.unshift(wrapper_class)
+ css_classes << wrapper_error_class if object && errors.present?
html_options_for(:wrapper, css_classes)
end
end
@@ -30,7 +30,11 @@ def input_options
end
def input_html_options
- html_options_for(:input, input_type, required_class)
+ html_options_for(:input, input_html_classes)
+ end
+
+ def input_html_classes
+ [input_type, required_class]
end
def render
@@ -68,7 +72,7 @@ def reflection_or_attribute_name
end
# Retrieve options for the given namespace from the options hash
- def html_options_for(namespace, *extra)
+ def html_options_for(namespace, extra)
html_options = options[:"#{namespace}_html"] || {}
html_options[:class] = (extra << html_options[:class]).join(' ').strip if extra.present?
html_options
@@ -42,12 +42,12 @@ def detect_collection_methods(collection, options)
sample = collection.first || collection.last
case sample
- when Array
- label, value = :first, :last
- when Integer
- label, value = :to_s, :to_i
- when String, NilClass
- label, value = :to_s, :to_s
+ when Array
+ label, value = :first, :last
+ when Integer
+ label, value = :to_s, :to_i
+ when String, NilClass
+ label, value = :to_s, :to_s
end
options[:label_method] ||= label || SimpleForm.collection_label_methods.find { |m| sample.respond_to?(m) }
@@ -7,10 +7,10 @@ def input
def label_target
case input_type
- when :date, :datetime
- "#{attribute_name}_1i"
- when :time
- "#{attribute_name}_4i"
+ when :date, :datetime
+ "#{attribute_name}_1i"
+ when :time
+ "#{attribute_name}_4i"
end
end
end
@@ -7,10 +7,14 @@ def input
def input_html_options
input_options = super
- input_options[:class] = "numeric #{input_options[:class]}"
+ input_options[:type] ||= "number"
input_options[:size] ||= SimpleForm.default_input_size
input_options
end
+
+ def input_html_classes
+ super.unshift("numeric")
+ end
end
end
end
@@ -9,15 +9,14 @@ def input_html_options
input_options = super
input_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
input_options[:maxlength] ||= limit if limit
-
- unless input_type == :string
- input_options[:type] ||= input_type
- input_options[:class] = "string #{input_options[:class]}"
- end
-
+ input_options[:type] ||= input_type unless input_type == :string
input_options
end
+ def input_html_classes
+ input_type == :string ? super : super.unshift("string")
+ end
+
protected
def limit
@@ -3,7 +3,7 @@
class WrapperTest < ActionView::TestCase
def with_error_for(object, attribute_name, options={}, &block)
concat(simple_form_for object do |f|
- f.options = options
+ f.options = options
f.input attribute_name
end)
end
View
@@ -34,17 +34,17 @@ def with_input_for(object, attribute_name, type, options={})
test 'input should generate an integer text field for integer attributes ' do
with_input_for @user, :age, :integer
- assert_select 'input[type=text].integer#user_age'
+ assert_select 'input[type=number].integer#user_age'
end
test 'input should generate a float text field for float attributes ' do
with_input_for @user, :age, :float
- assert_select 'input[type=text].float#user_age'
+ assert_select 'input[type=number].float#user_age'
end
test 'input should generate a decimal text field for decimal attributes ' do
with_input_for @user, :age, :decimal
- assert_select 'input[type=text].decimal#user_age'
+ assert_select 'input[type=number].decimal#user_age'
end
test 'input should use default text size for decimal attributes' do
@@ -1,14 +1,14 @@
class MockController
attr_accessor :action_name
- def action_name
- @action_name || "edit"
- end
-
def _router
self
end
+ def action_name
+ @action_name || "edit"
+ end
+
def url_for(*args)
"http://example.com"
end
View
@@ -1,20 +1,25 @@
require 'rubygems'
require 'test/unit'
+gem "actionpack", "3.0.0.beta3"
+gem "activemodel", "3.0.0.beta3"
+
require 'active_model'
require 'action_controller'
require 'action_view'
require 'action_view/template'
+
+$:.unshift File.join(File.dirname(__FILE__), '..', 'lib', 'simple_form')
+require 'simple_form'
+
+# Load tests specifics
require 'action_view/test_case'
begin
require 'ruby-debug'
rescue LoadError
end
-$:.unshift File.join(File.dirname(__FILE__), '..', 'lib', 'simple_form')
-require 'simple_form'
-
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
I18n.default_locale = :en
@@ -34,8 +39,6 @@ class SimpleForm::FormBuilder
class ActionView::TestCase
include MiscHelpers
- tests SimpleForm::ActionViewExtensions::FormHelper
-
setup :set_controller
setup :set_response
setup :setup_new_user

0 comments on commit 1ef232c

Please sign in to comment.