Permalink
Browse files

Ensure proper delocalization for non-string :value

The behavior is similar to what is proposed in pull request/issue #31 while keeping the exissting
behavior for blank strings.
  • Loading branch information...
clemens committed Aug 20, 2012
1 parent eec3192 commit 0277c1ffb2bb4947442c370f289866c072553a18
Showing with 27 additions and 24 deletions.
  1. +20 −23 lib/delocalize/rails_ext/action_view.rb
  2. +7 −1 test/delocalize_test.rb
@@ -10,30 +10,27 @@
def to_input_field_tag(field_type, options = {})
options.symbolize_keys!
# numbers and dates/times should be localized unless value is already defined
- if object && options[:value].blank? && object.respond_to?(:column_for_attribute) && column = object.column_for_attribute(method_name)
- # a little verbose
- if column.number? || column.date? || column.time?
- value = object.send(method_name)
-
- if column.number?
- number_options = I18n.t(:'number.format')
- separator = options.delete(:separator) || number_options[:separator]
- delimiter = options.delete(:delimiter) || number_options[:delimiter]
- precision = options.delete(:precision) || number_options[:precision]
- opts = { :separator => separator, :delimiter => delimiter, :precision => precision }
- # integers don't need a precision
- opts.merge!(:precision => 0) if column.type == :integer
-
- hidden_for_integer = field_type == 'hidden' && column.type == :integer
-
- # the number will be formatted only if it has no numericality errors
- if object.respond_to?(:errors) && !Array(object.errors[method_name]).try(:include?, 'is not a number')
- # we don't format integer hidden fields because this breaks nested_attributes
- options[:value] = number_with_precision(value, opts) unless hidden_for_integer
- end
- elsif column.date? || column.time?
- options[:value] = value ? I18n.l(value, :format => options.delete(:format)) : nil
+ if object && (options[:value].blank? || !options[:value].is_a?(String)) && object.respond_to?(:column_for_attribute) && column = object.column_for_attribute(method_name)
+ value = options[:value] || object.send(method_name)
+
+ if column.number?
+ number_options = I18n.t(:'number.format')
+ separator = options.delete(:separator) || number_options[:separator]
+ delimiter = options.delete(:delimiter) || number_options[:delimiter]
+ precision = options.delete(:precision) || number_options[:precision]
+ opts = { :separator => separator, :delimiter => delimiter, :precision => precision }
+ # integers don't need a precision
+ opts.merge!(:precision => 0) if column.type == :integer
+
+ hidden_for_integer = field_type == 'hidden' && column.type == :integer
+
+ # the number will be formatted only if it has no numericality errors
+ if object.respond_to?(:errors) && !Array(object.errors[method_name]).try(:include?, 'is not a number')
+ # we don't format integer hidden fields because this breaks nested_attributes
+ options[:value] = number_with_precision(value, opts) unless hidden_for_integer
end
+ elsif column.date? || column.time?
+ options[:value] = value ? I18n.l(value, :format => options.delete(:format)) : nil
end
end
View
@@ -296,7 +296,13 @@ def setup
}
end
- test "doesn't override given :value" do
+ test "delocalizes a given non-string :value" do
+ @product.price = 1299.9
+ assert_dom_equal '<input id="product_price" name="product[price]" size="30" type="text" value="1.499,90" />',
+ text_field(:product, :price, :value => 1499.90)
+ end
+
+ test "doesn't override given string :value" do
@product.price = 1299.9
assert_dom_equal '<input id="product_price" name="product[price]" size="30" type="text" value="1.499,90" />',
text_field(:product, :price, :value => "1.499,90")

0 comments on commit 0277c1f

Please sign in to comment.