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
253 253
  # Helper that formats object for html or text rendering
254 254
  # Options:
255 255
  # * :html - If true, format the object as HTML (default: true)
256
  # * :thousands_delimiter - If true, format the numeric object with thousands delimiter (default: false)
256 257
  def format_object(object, *args, &block)
257 258
    options = args.last.is_a?(Hash) ? args.pop : {}
258 259
    html =
......
267 268
        )
268 269
        args[0]
269 270
      end
271
    thousands_delimiter = options.fetch(:thousands_delimiter, false)
272
    delimiter = thousands_delimiter ? ::I18n.t('number.format.delimiter') : nil
270 273

  
271 274
    if block
272 275
      object = yield object
273 276
    end
274 277
    case object
275 278
    when Array
276
      formatted_objects = object.map {|o| format_object(o, html: html)}
279
      formatted_objects = object.map do |o|
280
        format_object(o, html: html, thousands_delimiter: delimiter)
281
      end
277 282
      html ? safe_join(formatted_objects, ', ') : formatted_objects.join(', ')
278 283
    when Time, ActiveSupport::TimeWithZone
279 284
      format_time(object)
280 285
    when Date
281 286
      format_date(object)
282 287
    when Integer
283
      object.to_s
288
      number_with_delimiter(object, delimiter: delimiter)
284 289
    when Float
285
      number_with_delimiter(sprintf('%.2f', object), delimiter: nil)
290
      number_with_delimiter(sprintf('%.2f', object), delimiter: delimiter)
286 291
    when User, Group
287 292
      html ? link_to_principal(object) : object.to_s
288 293
    when Project
......
318 323
        if f.nil? || f.is_a?(String)
319 324
          f
320 325
        else
321
          format_object(f, html: html, &block)
326
          thousands_delimiter = object.custom_field.thousands_delimiter?
327
          format_object(f, html: html, thousands_delimiter: thousands_delimiter, &block)
322 328
        end
323 329
      else
324 330
        object.value.to_s
app/models/custom_field.rb
100 100
    'user_role',
101 101
    'version_status',
102 102
    'extensions_allowed',
103
    'full_width_layout')
103
    'full_width_layout',
104
    'thousands_delimiter'
105
  )
104 106

  
105 107
  def copy_from(arg, options={})
106 108
    return if arg.blank?
......
225 227
    text_formatting == 'full'
226 228
  end
227 229

  
230
  def thousands_delimiter?
231
    thousands_delimiter == '1'
232
  end
233

  
228 234
  # Returns a ORDER BY clause that can used to sort customized
229 235
  # objects by their value of the custom field.
230 236
  # Returns nil if the custom field can not be used for sorting.
app/views/custom_fields/formats/_numeric.html.erb
1 1
<%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %>
2
<p><%= f.check_box :thousands_delimiter %></p>
2 3
<p><%= f.text_field(:default_value) %></p>
3 4
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
config/locales/en.yml
421 421
  field_any_searchable: Any searchable text
422 422
  field_estimated_remaining_hours: Estimated remaining time
423 423
  field_last_activity_date: Last activity
424
  field_thousands_delimiter: Thousands delimiter
424 425

  
425 426
  setting_app_title: Application title
426 427
  setting_welcome_text: Welcome text
lib/redmine/field_format.rb
476 476
    class Numeric < Unbounded
477 477
      self.form_partial = 'custom_fields/formats/numeric'
478 478
      self.totalable_supported = true
479
      field_attributes :thousands_delimiter
479 480

  
480 481
      def order_statement(custom_field)
481 482
        # Make the database cast values into numeric
test/unit/lib/redmine/field_format/numeric_format_test.rb
60 60
      end
61 61
    end
62 62
  end
63

  
64
  def test_integer_field_should_format_with_thousands_delimiter
65
    field = IssueCustomField.generate!(field_format: 'int', thousands_delimiter: '1')
66
    custom_value = CustomValue.new(custom_field: field, customized: Issue.find(1), value: '1234567')
67
    to_test = {'en' => '1,234,567', 'de' => '1.234.567', 'fr' => '1 234 567'}
68
    to_test.each do |locale, expected|
69
      with_locale locale do
70
        assert_equal expected, format_object(custom_value, html: false), locale
71
      end
72
    end
73
  end
63 74
end
(6-6/6)