end
def validate_single_value(custom_field, value, customized=nil)
+ value = normalize_float(value)
errs = super
- errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
+ errs << ::I18n.t('activerecord.errors.messages.invalid') unless Kernel.Float(value, exception: false)
errs
end
end
end
+ # Will consider language specific separator in user input
+ # and normalize them to a unified format to be accepted by Kernel.Float().
+ #
+ # @param value [String] A string represenation of a float value.
+ #
+ # @note The delimiter cannot be used here if it is a decimal point since it
+ # will clash with the dot separator.
+ def normalize_float(value)
+ separator = ::I18n.t('number.format.separator')
+ value.gsub(/[#{separator}]/, separator => '.')
+ end
+
def day_name(day)
::I18n.t('date.day_names')[day % 7]
end
class Redmine::NumericFieldFormatTest < ActionView::TestCase
include ApplicationHelper
+ fixtures :projects, :users, :issue_statuses, :enumerations,
+ :trackers, :projects_trackers, :roles, :member_roles,
+ :members, :enabled_modules
+
def setup
User.current = nil
end
assert_equal 3, field.format.formatted_custom_value(self, custom_value, false)
assert_equal '<a href="http://foo/3" class="external">3</a>', field.format.formatted_custom_value(self, custom_value, true)
end
+
+ def test_float_field_value_should_validate_when_given_with_various_separator
+ field = IssueCustomField.generate!(field_format: 'float')
+ issue = Issue.generate!(tracker: Tracker.find(1), status: IssueStatus.find(1), priority: IssuePriority.find(6))
+ to_test = {'en' => '3.33', 'de' => '3,33'}
+ to_test.each do |locale, expected|
+ with_locale locale do
+ assert field.format.validate_single_value(field, expected, issue)
+ end
+ end
+ end
+
+ def test_float_field_should_format_with_various_locale_separator
+ field = IssueCustomField.generate!(field_format: 'float')
+ issue = Issue.generate!(tracker: Tracker.find(1), status: IssueStatus.find(1), priority: IssuePriority.find(6))
+ issue.custom_field_values = { field.id => '1234.56' }
+ issue.save!
+ to_test = {'en' => '1234.56', 'de' => '1234,56'}
+ to_test.each do |locale, expected|
+ with_locale locale do
+ assert_equal expected, format_object(issue.reload.custom_field_values.last, false)
+ end
+ end
+ end
end