Add an option to render Integer and Float custom fields with thousands delimiters (#39997).

Patch by Go MAEDA (user:maeda).


git-svn-id: https://svn.redmine.org/redmine/trunk@22935 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Go MAEDA
2024-07-18 04:56:20 +00:00
parent b374db2fdb
commit ee31bf8fe9
6 changed files with 30 additions and 5 deletions

View File

@@ -253,6 +253,7 @@ module ApplicationHelper
# Helper that formats object for html or text rendering
# Options:
# * :html - If true, format the object as HTML (default: true)
# * :thousands_delimiter - If true, format the numeric object with thousands delimiter (default: false)
def format_object(object, *args, &block)
options =
if args.first.is_a?(Hash)
@@ -266,22 +267,26 @@ module ApplicationHelper
end
html = options.fetch(:html, true)
thousands_delimiter = options.fetch(:thousands_delimiter, false)
delimiter_char = thousands_delimiter ? ::I18n.t('number.format.delimiter') : nil
if block
object = yield object
end
case object
when Array
formatted_objects = object.map {|o| format_object(o, html: html)}
formatted_objects = object.map do |o|
format_object(o, html: html, thousands_delimiter: thousands_delimiter)
end
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
number_with_delimiter(object, delimiter: delimiter_char)
when Float
number_with_delimiter(sprintf('%.2f', object), delimiter: nil)
number_with_delimiter(sprintf('%.2f', object), delimiter: delimiter_char)
when User, Group
html ? link_to_principal(object) : object.to_s
when Project
@@ -317,7 +322,7 @@ module ApplicationHelper
if f.nil? || f.is_a?(String)
f
else
format_object(f, html: html, &block)
format_object(f, html: html, thousands_delimiter: object.custom_field.thousands_delimiter?, &block)
end
else
object.value.to_s

View File

@@ -100,7 +100,9 @@ class CustomField < ApplicationRecord
'user_role',
'version_status',
'extensions_allowed',
'full_width_layout')
'full_width_layout',
'thousands_delimiter'
)
def copy_from(arg, options={})
return if arg.blank?
@@ -225,6 +227,10 @@ class CustomField < ApplicationRecord
text_formatting == 'full'
end
def thousands_delimiter?
thousands_delimiter == '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.

View File

@@ -1,3 +1,4 @@
<%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %>
<p><%= f.check_box :thousands_delimiter %></p>
<p><%= f.text_field(:default_value) %></p>
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>

View File

@@ -421,6 +421,7 @@ en:
field_any_searchable: Any searchable text
field_estimated_remaining_hours: Estimated remaining time
field_last_activity_date: Last activity
field_thousands_delimiter: Thousands delimiter
setting_app_title: Application title
setting_welcome_text: Welcome text

View File

@@ -476,6 +476,7 @@ module Redmine
class Numeric < Unbounded
self.form_partial = 'custom_fields/formats/numeric'
self.totalable_supported = true
field_attributes :thousands_delimiter
def order_statement(custom_field)
# Make the database cast values into numeric

View File

@@ -60,4 +60,15 @@ class Redmine::NumericFieldFormatTest < ActionView::TestCase
end
end
end
def test_integer_field_should_format_with_thousands_delimiter
field = IssueCustomField.generate!(field_format: 'int', thousands_delimiter: '1')
custom_value = CustomValue.new(custom_field: field, customized: Issue.find(1), value: '1234567')
to_test = {'en' => '1,234,567', 'de' => '1.234.567', 'fr' => '1 234 567'}
to_test.each do |locale, expected|
with_locale locale do
assert_equal expected, format_object(custom_value, html: false), locale
end
end
end
end