Project

General

Profile

Feature #21115 ยป 13.patch

Eric Helms, 2015-10-30 14:01

View differences:

lib/redmine/field_format.rb
323 323

  
324 324
    class LinkFormat < StringFormat
325 325
      add 'link'
326
      self.multiple_supported = true
326 327
      self.searchable_supported = false
327 328
      self.form_partial = 'custom_fields/formats/link'
328 329

  
329 330
      def formatted_value(view, custom_field, value, customized=nil, html=false)
330
        if html
331
          if custom_field.url_pattern.present?
332
            url = url_from_pattern(custom_field, value, customized)
333
          else
334
            url = value.to_s
335
            unless url =~ %r{\A[a-z]+://}i
336
              # no protocol found, use http by default
337
              url = "http://" + url
331
        values = value.is_a?(Array) ? value : [value]
332
        links = []
333

  
334
        values.each do |val|
335
          if html
336
            if custom_field.url_pattern.present?
337
              url = url_from_pattern(custom_field, val, customized)
338
            else
339
              url = val.to_s
340
              unless url =~ %r{\A[a-z]+://}i
341
                # no protocol found, use http by default
342
                url = "http://" + url
343
              end
338 344
            end
345
            links << view.link_to(val.to_s, url)
346
          else
347
            links << val.to_s
339 348
          end
340
          view.link_to value.to_s, url
349
        end
350

  
351
        links.length == 1 ? links[0] : links
352
      end
353

  
354
      def edit_tag(view, tag_id, tag_name, custom_value, options={})
355
        if custom_value.value.is_a?(String)
356
          view_response = view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10))
341 357
        else
342
          value.to_s
358
          view_response = view.text_field_tag(tag_name, '', options.merge(:id => tag_id, :size => 10))
359

  
360
          custom_value.value.each do |value|
361
            view_response += view.text_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10))
362
          end
343 363
        end
364

  
365
        view_response
344 366
      end
345 367
    end
346 368

  
test/unit/lib/redmine/field_format/link_format_test.rb
87 87
    assert_equal "foo.bar", field.format.formatted_custom_value(self, custom_value, false)
88 88
    assert_equal '<a href="http://foo.bar">foo.bar</a>', field.format.formatted_custom_value(self, custom_value, true)
89 89
  end
90

  
91
  def test_link_field_with_multiple_values_returns_array_of_links
92
    field = IssueCustomField.new(:field_format => 'link')
93
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => ["foo.bar", "bar.foo"])
94

  
95
    assert_equal ["foo.bar", "bar.foo"], field.format.formatted_custom_value(self, custom_value, false)
96
    assert_equal ['<a href="http://foo.bar">foo.bar</a>', '<a href="http://bar.foo">bar.foo</a>'], field.format.formatted_custom_value(self, custom_value, true)
97
  end
90 98
end
    (1-1/1)