Project

General

Profile

Patch #41508 » add_value_mask_attribute_to_string_field_format.patch

Lorenzo Meneghetti, 2024-10-19 17:30

View differences:

app/models/custom_field.rb
101 101
    'version_status',
102 102
    'extensions_allowed',
103 103
    'full_width_layout',
104
    'thousands_delimiter'
104
    'thousands_delimiter',
105
    'value_mask'
105 106
  )
106 107

  
107 108
  def copy_from(arg, options={})
app/views/custom_fields/formats/_string.html.erb
2 2
<p><%= f.check_box :text_formatting, {:label => :setting_text_formatting, :data => {:disables => '#custom_field_url_pattern'}}, 'full', '' %></p>
3 3
<p><%= f.text_field(:default_value) %></p>
4 4
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
5
<p><%= f.text_field :value_mask, size: 50, label: 'Value mask' %></p>
lib/redmine/field_format.rb
250 250
        casted = cast_value(custom_field, value, customized)
251 251
        if html && custom_field.url_pattern.present?
252 252
          texts_and_urls = Array.wrap(casted).map do |single_value|
253
            text = view.format_object(single_value, html: false).to_s
253
            text = custom_field.value_mask.present? ? value_from_pattern(custom_field, single_value) : view.format_object(single_value, html: false).to_s
254 254
            url = url_from_pattern(custom_field, single_value, customized)
255 255
            [text, url]
256 256
          end
......
258 258
            view.link_to text, url
259 259
          end
260 260
          sanitize_html links.join(', ')
261
        elsif html && custom_field.value_mask.present?
262
          value_from_pattern(custom_field, single_value)
261 263
        else
262 264
          casted
263 265
        end
......
267 269
        Redmine::WikiFormatting::HtmlSanitizer.call(html).html_safe
268 270
      end
269 271

  
272
      # Returns a value generated with the custom field Regex pattern
273
      # %m1%, %m2%... => capture groups matches of the custom field regexp if defined
274
      def value_from_pattern(custom_field, value)
275
        val = custom_field.value_mask.to_s.dup
276
        if custom_field.regexp.present?
277
          val.gsub!(%r{%m(\d+)%}) do
278
            m = $1.to_i
279
            if matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
280
              val = matches[m].to_s
281
            end
282
          end
283
        end
284
        val
285
      end
286
      protected :value_from_pattern
287

  
270 288
      # Returns an URL generated with the custom field URL pattern
271 289
      # and variables substitution:
272 290
      # %value% => the custom field value
......
396 414
      add 'string'
397 415
      self.searchable_supported = true
398 416
      self.form_partial = 'custom_fields/formats/string'
399
      field_attributes :text_formatting
417
      field_attributes :text_formatting, :value_mask
400 418

  
401 419
      def formatted_value(view, custom_field, value, customized=nil, html=false)
402 420
        if html
403
          if custom_field.url_pattern.present?
421
          if custom_field.url_pattern.present? || custom_field.value_mask.present?
404 422
            super
405 423
          elsif custom_field.text_formatting == 'full'
406 424
            view.textilizable(value, :object => customized)
(3-3/3)