Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: clemens/delocalize
base: master
...
head fork: gregoriokusowski/delocalize
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
15 lib/delocalize/rails_ext/action_view.rb
@@ -22,15 +22,20 @@ def to_input_field_tag(field_type, options = {})
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
+ if column.type == :integer
+ opts.merge!({:precision => 0, :delimiter => ''})
+ end
- hidden_for_integer = field_type == 'hidden' && column.type == :integer
+ # checks for :not_a_number numericality errors.
+ has_errors = object.respond_to?(:errors) && !Array(object.errors[method_name]).try(:empty?)
+ has_errors_that_dont_allow_formatting = has_errors && value.to_s != Numeric.parse_localized(number_with_precision(value, opts)).to_s
# 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
+ # we don't format integer hidden fields because this breaks nested_attributes
+ unless has_errors_that_dont_allow_formatting
+ options[:value] = number_with_precision(value, opts)
end
+
elsif column.date? || column.time?
options[:value] = value ? I18n.l(value, :format => options.delete(:format)) : nil
end
View
21 test/delocalize_test.rb
@@ -277,6 +277,27 @@ def setup
text_field(:product, :price)
end
+ test "doesn't convert the value if the field have nested numericality error messages for that property" do
+ @product = ProductWithNestedMessage.new(:price => 'not a price')
+ @product.errors.add(:price, :not_a_number)
+ assert_dom_equal %(<div class="field_with_errors"><input id="product_price" name="product[price]" size="30" type="text" value="not a price" /></div>),
+ text_field(:product, :price)
+ end
+
+ test "doesn't convert the value if the field have nested numericality error messages" do
+ @product = ProductWithNestedMessage.new(:weight => 'not a weight')
+ @product.errors.add(:weight, :not_a_number)
+ assert_dom_equal %(<div class="field_with_errors"><input id="product_weight" name="product[weight]" size="30" type="text" value="not a weight" /></div>),
+ text_field(:product, :weight)
+ end
+
+ test "doesn't convert the value if the field have a custom numericality error message" do
+ @product = ProductWithCustomMessage.new(:price => 'more than I can pay for!')
+ @product.valid?
+ assert_dom_equal %(<div class="field_with_errors"><input id="product_price" name="product[price]" size="30" type="text" value="more than I can pay for!" /></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?
View
21 test/test_helper.rb
@@ -49,7 +49,21 @@
}
},
:activerecord => {
+ # http://guides.rubyonrails.org/i18n.html#highlighter_92251
+ # activerecord.errors.models.user.attributes.name.not_a_number
+ # activerecord.errors.models.user.not_a_number
+ # activerecord.errors.messages.not_a_number
:errors => {
+ :models => {
+ :product_with_nested_message => {
+ :not_a_number => 'is not a property from nested example',
+ :attributes => {
+ :price => {
+ :not_a_number => 'is not a price'
+ }
+ }
+ }
+ },
:messages => {
:not_a_number => 'is not a number'
}
@@ -88,6 +102,13 @@ class ProductWithBusinessValidation < Product
end
end
+class ProductWithNestedMessage < Product
+end
+
+class ProductWithCustomMessage < Product
+ validates_numericality_of :price, :message => 'custom message'
+end
+
config = YAML.load_file(File.dirname(__FILE__) + '/database.yml')
ActiveRecord::Base.establish_connection(config['test'])

No commit comments for this range

Something went wrong with that request. Please try again.