]> source.dussan.org Git - redmine.git/commitdiff
Refactor ApplicationHelper#format_object to accept options as a hash (#39997).
authorGo MAEDA <maeda@farend.jp>
Thu, 18 Jul 2024 04:26:49 +0000 (04:26 +0000)
committerGo MAEDA <maeda@farend.jp>
Thu, 18 Jul 2024 04:26:49 +0000 (04:26 +0000)
Patch by Go MAEDA (user:maeda).

git-svn-id: https://svn.redmine.org/redmine/trunk@22933 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/application_helper.rb
app/helpers/custom_fields_helper.rb
app/helpers/queries_helper.rb
app/helpers/roles_helper.rb
app/helpers/timelog_helper.rb
lib/redmine/field_format.rb
test/unit/lib/redmine/field_format/numeric_format_test.rb

index ea9bf8c52fc7c45052e584959ae0581b65febd91..f7b92a9830c705979db9b6dec2b965ffbcccf45f 100644 (file)
@@ -251,13 +251,28 @@ module ApplicationHelper
   end
 
   # Helper that formats object for html or text rendering
-  def format_object(object, html=true, &block)
+  # Options:
+  # * :html - If true, format the object as HTML (default: true)
+  def format_object(object, *args, &block)
+    options =
+      if args.first.is_a?(Hash)
+        args.first
+      elsif !args.empty?
+        # Support the old syntax `format_object(object, html_flag)`
+        # TODO: Display a deprecation warning in a future version, then remove this
+        {:html => args.first}
+      else
+        {}
+      end
+
+    html = options.fetch(:html, true)
+
     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)}
       html ? safe_join(formatted_objects, ', ') : formatted_objects.join(', ')
     when Time, ActiveSupport::TimeWithZone
       format_time(object)
@@ -302,7 +317,7 @@ module ApplicationHelper
         if f.nil? || f.is_a?(String)
           f
         else
-          format_object(f, html, &block)
+          format_object(f, html: html, &block)
         end
       else
         object.value.to_s
index 2a188cd0a2a583d420ac9422477389dc55bed225..a6149909d73cd4022ad9c3044abf8ec9860c03dd 100644 (file)
@@ -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
index 47f7db0f2495fe6d010eb054ad88c9dc6111c851..b5ce6addd2023e8f7d5b007398cbf22464515c1b 100644 (file)
@@ -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))
index f360c1176335f6199918b10ef729f22ca332b43f..9c56a6a74d5b96113820f750ebb1ca5504a8ea83 100644 (file)
@@ -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
index 17c903c9a696d6f3630451c81b8c99512817b16e..92104495c5e1ccdca9868441717e122f0a385429 100644 (file)
@@ -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)
index 148cccadc88826c67086990f543feb0db86d0ec9..7d7fe42bc4baa6c66ad9d08e47b03a1ed004dff0 100644 (file)
@@ -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
index 9e624f309086cfc1b02ec9a7f2c9423120eeeb1f..db86a0f5ce8fbb62978dca39a1b3b4b4a0afa845 100644 (file)
@@ -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