Project

General

Profile

Feature #39997 » 0003-Add-option-to-render-Integer-and-Float-custom-fields.patch

Go MAEDA, 2024-07-14 11:08

View differences:

app/helpers/application_helper.rb
# 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 = args.last.is_a?(Hash) ? args.pop : {}
html =
......
)
args[0]
end
thousands_delimiter = options.fetch(:thousands_delimiter, false)
delimiter = 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: 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)
when Float
number_with_delimiter(sprintf('%.2f', object), delimiter: nil)
number_with_delimiter(sprintf('%.2f', object), delimiter: delimiter)
when User, Group
html ? link_to_principal(object) : object.to_s
when Project
......
if f.nil? || f.is_a?(String)
f
else
format_object(f, html: html, &block)
thousands_delimiter = object.custom_field.thousands_delimiter?
format_object(f, html: html, thousands_delimiter: thousands_delimiter, &block)
end
else
object.value.to_s
app/models/custom_field.rb
'user_role',
'version_status',
'extensions_allowed',
'full_width_layout')
'full_width_layout',
'thousands_delimiter'
)
def copy_from(arg, options={})
return if arg.blank?
......
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.
app/views/custom_fields/formats/_numeric.html.erb
<%= 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>
config/locales/en.yml
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
lib/redmine/field_format.rb
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
test/unit/lib/redmine/field_format/numeric_format_test.rb
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
(6-6/6)