Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convert numeric values if a numeric field has non-numericality errors…

…. Closes
  • Loading branch information...
commit 26d3d9923808e5d1c27dea32171ef8a24794ab47 1 parent d51982f
@fernandoluizao fernandoluizao authored
View
2  .gitignore
@@ -2,3 +2,5 @@ pkg/*
log/*
test/rails2_app/log/*
test/rails3_app/log/*
+Gemfile.lock
+
View
7 lib/delocalize/rails_ext/action_view.rb
@@ -26,8 +26,8 @@ def to_input_field_tag(field_type, options = {})
hidden_for_integer = field_type == 'hidden' && column.type == :integer
- # the number will be formatted only if it has no errors
- if object.respond_to?(:errors) && !Array(object.errors[method_name]).try(:any?)
+ # 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
@@ -44,7 +44,7 @@ def to_input_field_tag(field_type, options = {})
# TODO: does it make sense to also override FormTagHelper methods?
# ActionView::Helpers::FormTagHelper.class_eval do
# include ActionView::Helpers::NumberHelper
-#
+#
# alias original_text_field_tag text_field_tag
# def text_field_tag(name, value = nil, options = {})
# value = options.delete(:value) if options.key?(:value)
@@ -54,3 +54,4 @@ def to_input_field_tag(field_type, options = {})
# original_text_field_tag(name, value, options)
# end
# end
+
View
10 test/delocalize_test.rb
@@ -270,13 +270,20 @@ def setup
text_field(:product, :price, :value => "1.499,90")
end
- test "doesn't convert the value if field has errors" do
+ test "doesn't convert the value if field has numericality errors" do
@product = ProductWithValidation.new(:price => 'this is not a number')
@product.valid?
assert_dom_equal %(<div class="field_with_errors"><input id="product_price" name="product[price]" size="30" type="text" value="this is not a number" /></div>),
text_field(:product, :price)
end
+ test "should convert the value if field have non-numericality errors, but have other errors, e.g. business rules" do
+ @product = ProductWithBusinessValidation.new(:price => '1.337,66')
+ @product.valid?
+ assert_dom_equal %(<div class="field_with_errors"><input id="product_price" name="product[price]" size="30" type="text" value="1.337,66" /></div>),
+ text_field(:product, :price)
+ end
+
test "doesn't raise an exception when object isn't an ActiveReccord" do
@product = NonArProduct.new
assert_nothing_raised {
@@ -294,3 +301,4 @@ def setup
text_field(:product, :some_value_with_default)
end
end
+
View
16 test/test_helper.rb
@@ -47,6 +47,13 @@
:separator => ',',
:delimiter => '.'
}
+ },
+ :activerecord => {
+ :errors => {
+ :messages => {
+ :not_a_number => 'is not a number'
+ }
+ }
}
}
@@ -73,6 +80,14 @@ class ProductWithValidation < Product
validates_presence_of :price
end
+class ProductWithBusinessValidation < Product
+ validate do |record|
+ if record.price > 10
+ record.errors.add(:price, :invalid)
+ end
+ end
+end
+
config = YAML.load_file(File.dirname(__FILE__) + '/database.yml')
ActiveRecord::Base.establish_connection(config['test'])
@@ -86,3 +101,4 @@ class ProductWithValidation < Product
t.integer :times_sold
t.decimal :some_value_with_default, :default => 0, :precision => 20, :scale => 2
end
+
Please sign in to comment.
Something went wrong with that request. Please try again.