diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ea9bf8c52..2edb37c7e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -251,22 +251,31 @@ module ApplicationHelper end # Helper that formats object for html or text rendering - def format_object(object, html=true, &block) + def format_object(object, *args, &block) + options = args.last.is_a?(Hash) ? args.pop : {} + # The :html option is supposed to be passed as a keyword argument like `format_object(object, html: true)`, + # but a old syntax like `format_object(object, true)` is also supported for backward compatibility. + # TODO: Remove the old syntax in Redmine 7.0. + html = options.fetch(:html, args[0].nil? ? true : args[0]) + th_sep = options[:thousand_separator] || false + if block object = yield object end case object when Array - formatted_objects = object.map {|o| format_object(o, html)} + formatted_objects = object.map {|o| format_object(o, html: html, thousand_separator: th_sep)} html ? safe_join(formatted_objects, ', ') : formatted_objects.join(', ') when Time, ActiveSupport::TimeWithZone format_time(object) when Date format_date(object) when Integer - object.to_s + delimiter = th_sep ? ::I18n.t('number.format.delimiter') : nil + number_with_delimiter(object, delimiter: delimiter) when Float - number_with_delimiter(sprintf('%.2f', object), delimiter: nil) + delimiter = th_sep ? ::I18n.t('number.format.delimiter') : nil + number_with_delimiter(sprintf('%.2f', object), delimiter: delimiter) when User, Group html ? link_to_principal(object) : object.to_s when Project @@ -299,10 +308,11 @@ module ApplicationHelper if object.custom_field f = object.custom_field.format.formatted_custom_value(self, object, html) + th_sep = object.custom_field.thousand_separator? if f.nil? || f.is_a?(String) f else - format_object(f, html, &block) + format_object(f, html: html, thousand_separator: th_sep, &block) end else object.value.to_s diff --git a/app/helpers/custom_fields_helper.rb b/app/helpers/custom_fields_helper.rb index 2a188cd0a..a6149909d 100644 --- a/app/helpers/custom_fields_helper.rb +++ b/app/helpers/custom_fields_helper.rb @@ -164,12 +164,12 @@ module CustomFieldsHelper # Return a string used to display a custom value def show_value(custom_value, html=true) - format_object(custom_value, html) + format_object(custom_value, html: html) end # Return a string used to display a custom value def format_value(value, custom_field) - format_object(custom_field.format.formatted_value(self, custom_field, value, false), false) + format_object(custom_field.format.formatted_value(self, custom_field, value, false), html: false) end # Return an array of custom field formats which can be used in select_tag diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb index 47f7db0f2..b5ce6addd 100644 --- a/app/helpers/queries_helper.rb +++ b/app/helpers/queries_helper.rb @@ -305,7 +305,7 @@ module QueriesHelper when :watcher_users value.to_a.join("\n") else - format_object(value, false) do |value| + format_object(value, html: false) do |value| case value.class.name when 'Float' sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator)) diff --git a/app/helpers/roles_helper.rb b/app/helpers/roles_helper.rb index f360c1176..9c56a6a74 100644 --- a/app/helpers/roles_helper.rb +++ b/app/helpers/roles_helper.rb @@ -35,7 +35,7 @@ module RolesHelper ] fields = names + roles.collect do |role| if role.setable_permissions.include?(p) - format_object(role.permissions.include?(p.name), false) + format_object(role.permissions.include?(p.name), html: false) else '' end diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb index 17c903c9a..92104495c 100644 --- a/app/helpers/timelog_helper.rb +++ b/app/helpers/timelog_helper.rb @@ -84,7 +84,7 @@ module TimelogHelper "##{obj.id}" end else - format_object(obj, html) + format_object(obj, html: html) end elsif cf = criteria_options[:custom_field] format_value(value, cf) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 97bc70228..d86dda5f6 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -100,7 +100,9 @@ class CustomField < ApplicationRecord 'user_role', 'version_status', 'extensions_allowed', - 'full_width_layout') + 'full_width_layout', + 'thousand_separator' + ) def copy_from(arg, options={}) return if arg.blank? @@ -225,6 +227,10 @@ class CustomField < ApplicationRecord text_formatting == 'full' end + def thousand_separator? + thousand_separator == '1' + end + # Returns a ORDER BY clause that can used to sort customized # objects by their value of the custom field. # Returns nil if the custom field can not be used for sorting. diff --git a/app/views/custom_fields/formats/_numeric.html.erb b/app/views/custom_fields/formats/_numeric.html.erb index cc0c798a6..1730008b1 100644 --- a/app/views/custom_fields/formats/_numeric.html.erb +++ b/app/views/custom_fields/formats/_numeric.html.erb @@ -1,3 +1,4 @@ <%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %>

<%= f.text_field(:default_value) %>

+

<%= f.check_box :thousand_separator %>

<%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index 05e01b77a..471df368c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -74,7 +74,7 @@ en: number: format: separator: "." - delimiter: "" + delimiter: "," precision: 3 human: diff --git a/lib/redmine/field_format.rb b/lib/redmine/field_format.rb index 148cccadc..70a2db3e8 100644 --- a/lib/redmine/field_format.rb +++ b/lib/redmine/field_format.rb @@ -250,7 +250,7 @@ module Redmine casted = cast_value(custom_field, value, customized) if html && custom_field.url_pattern.present? texts_and_urls = Array.wrap(casted).map do |single_value| - text = view.format_object(single_value, false).to_s + text = view.format_object(single_value, html: false).to_s url = url_from_pattern(custom_field, single_value, customized) [text, url] end @@ -476,6 +476,7 @@ module Redmine class Numeric < Unbounded self.form_partial = 'custom_fields/formats/numeric' self.totalable_supported = true + field_attributes :thousand_separator def order_statement(custom_field) # Make the database cast values into numeric diff --git a/test/unit/lib/redmine/field_format/numeric_format_test.rb b/test/unit/lib/redmine/field_format/numeric_format_test.rb index 9e624f309..db86a0f5c 100644 --- a/test/unit/lib/redmine/field_format/numeric_format_test.rb +++ b/test/unit/lib/redmine/field_format/numeric_format_test.rb @@ -56,7 +56,7 @@ class Redmine::NumericFieldFormatTest < ActionView::TestCase 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) + assert_equal expected, format_object(issue.reload.custom_field_values.last, html: false) end end end