mirror of
https://github.com/redmine/redmine.git
synced 2025-11-08 06:15:59 +01:00
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:
@@ -253,6 +253,7 @@ module ApplicationHelper
|
|||||||
# Helper that formats object for html or text rendering
|
# Helper that formats object for html or text rendering
|
||||||
# Options:
|
# Options:
|
||||||
# * :html - If true, format the object as HTML (default: true)
|
# * :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)
|
def format_object(object, *args, &block)
|
||||||
options =
|
options =
|
||||||
if args.first.is_a?(Hash)
|
if args.first.is_a?(Hash)
|
||||||
@@ -266,22 +267,26 @@ module ApplicationHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
html = options.fetch(:html, true)
|
html = options.fetch(:html, true)
|
||||||
|
thousands_delimiter = options.fetch(:thousands_delimiter, false)
|
||||||
|
delimiter_char = thousands_delimiter ? ::I18n.t('number.format.delimiter') : nil
|
||||||
|
|
||||||
if block
|
if block
|
||||||
object = yield object
|
object = yield object
|
||||||
end
|
end
|
||||||
case object
|
case object
|
||||||
when Array
|
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(', ')
|
html ? safe_join(formatted_objects, ', ') : formatted_objects.join(', ')
|
||||||
when Time, ActiveSupport::TimeWithZone
|
when Time, ActiveSupport::TimeWithZone
|
||||||
format_time(object)
|
format_time(object)
|
||||||
when Date
|
when Date
|
||||||
format_date(object)
|
format_date(object)
|
||||||
when Integer
|
when Integer
|
||||||
object.to_s
|
number_with_delimiter(object, delimiter: delimiter_char)
|
||||||
when Float
|
when Float
|
||||||
number_with_delimiter(sprintf('%.2f', object), delimiter: nil)
|
number_with_delimiter(sprintf('%.2f', object), delimiter: delimiter_char)
|
||||||
when User, Group
|
when User, Group
|
||||||
html ? link_to_principal(object) : object.to_s
|
html ? link_to_principal(object) : object.to_s
|
||||||
when Project
|
when Project
|
||||||
@@ -317,7 +322,7 @@ module ApplicationHelper
|
|||||||
if f.nil? || f.is_a?(String)
|
if f.nil? || f.is_a?(String)
|
||||||
f
|
f
|
||||||
else
|
else
|
||||||
format_object(f, html: html, &block)
|
format_object(f, html: html, thousands_delimiter: object.custom_field.thousands_delimiter?, &block)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
object.value.to_s
|
object.value.to_s
|
||||||
|
|||||||
@@ -100,7 +100,9 @@ class CustomField < ApplicationRecord
|
|||||||
'user_role',
|
'user_role',
|
||||||
'version_status',
|
'version_status',
|
||||||
'extensions_allowed',
|
'extensions_allowed',
|
||||||
'full_width_layout')
|
'full_width_layout',
|
||||||
|
'thousands_delimiter'
|
||||||
|
)
|
||||||
|
|
||||||
def copy_from(arg, options={})
|
def copy_from(arg, options={})
|
||||||
return if arg.blank?
|
return if arg.blank?
|
||||||
@@ -225,6 +227,10 @@ class CustomField < ApplicationRecord
|
|||||||
text_formatting == 'full'
|
text_formatting == 'full'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def thousands_delimiter?
|
||||||
|
thousands_delimiter == '1'
|
||||||
|
end
|
||||||
|
|
||||||
# Returns a ORDER BY clause that can used to sort customized
|
# Returns a ORDER BY clause that can used to sort customized
|
||||||
# objects by their value of the custom field.
|
# objects by their value of the custom field.
|
||||||
# Returns nil if the custom field can not be used for sorting.
|
# Returns nil if the custom field can not be used for sorting.
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
<%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %>
|
<%= 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(:default_value) %></p>
|
||||||
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
|
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
|
||||||
|
|||||||
@@ -421,6 +421,7 @@ en:
|
|||||||
field_any_searchable: Any searchable text
|
field_any_searchable: Any searchable text
|
||||||
field_estimated_remaining_hours: Estimated remaining time
|
field_estimated_remaining_hours: Estimated remaining time
|
||||||
field_last_activity_date: Last activity
|
field_last_activity_date: Last activity
|
||||||
|
field_thousands_delimiter: Thousands delimiter
|
||||||
|
|
||||||
setting_app_title: Application title
|
setting_app_title: Application title
|
||||||
setting_welcome_text: Welcome text
|
setting_welcome_text: Welcome text
|
||||||
|
|||||||
@@ -476,6 +476,7 @@ module Redmine
|
|||||||
class Numeric < Unbounded
|
class Numeric < Unbounded
|
||||||
self.form_partial = 'custom_fields/formats/numeric'
|
self.form_partial = 'custom_fields/formats/numeric'
|
||||||
self.totalable_supported = true
|
self.totalable_supported = true
|
||||||
|
field_attributes :thousands_delimiter
|
||||||
|
|
||||||
def order_statement(custom_field)
|
def order_statement(custom_field)
|
||||||
# Make the database cast values into numeric
|
# Make the database cast values into numeric
|
||||||
|
|||||||
@@ -60,4 +60,15 @@ class Redmine::NumericFieldFormatTest < ActionView::TestCase
|
|||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user