From 02753dcd88ab662b1c7b13729a92b450fb8175cd Mon Sep 17 00:00:00 2001 From: "Eric D. Helms" Date: Thu, 29 Oct 2015 14:03:44 -0400 Subject: [PATCH] Allow multiple links with custom link field --- lib/redmine/field_format.rb | 42 ++++++++++++++++------ .../lib/redmine/field_format/link_format_test.rb | 8 +++++ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/redmine/field_format.rb b/lib/redmine/field_format.rb index d52cd52..f28851b 100644 --- a/lib/redmine/field_format.rb +++ b/lib/redmine/field_format.rb @@ -323,24 +323,46 @@ def query_filter_options(custom_field, query) class LinkFormat < StringFormat add 'link' + self.multiple_supported = true self.searchable_supported = false self.form_partial = 'custom_fields/formats/link' def formatted_value(view, custom_field, value, customized=nil, html=false) - if html - if custom_field.url_pattern.present? - url = url_from_pattern(custom_field, value, customized) - else - url = value.to_s - unless url =~ %r{\A[a-z]+://}i - # no protocol found, use http by default - url = "http://" + url + values = value.is_a?(Array) ? value : [value] + links = [] + + values.each do |val| + if html + if custom_field.url_pattern.present? + url = url_from_pattern(custom_field, val, customized) + else + url = val.to_s + unless url =~ %r{\A[a-z]+://}i + # no protocol found, use http by default + url = "http://" + url + end end + links << view.link_to(val.to_s, url) + else + links << val.to_s end - view.link_to value.to_s, url + end + + links.length == 1 ? links[0] : links + end + + def edit_tag(view, tag_id, tag_name, custom_value, options={}) + if custom_value.value.is_a?(String) + view_response = view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10)) else - value.to_s + view_response = view.text_field_tag(tag_name, '', options.merge(:id => tag_id, :size => 10)) + + custom_value.value.each do |value| + view_response += view.text_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10)) + end end + + view_response end end diff --git a/test/unit/lib/redmine/field_format/link_format_test.rb b/test/unit/lib/redmine/field_format/link_format_test.rb index 767a3de..0b9a702 100644 --- a/test/unit/lib/redmine/field_format/link_format_test.rb +++ b/test/unit/lib/redmine/field_format/link_format_test.rb @@ -87,4 +87,12 @@ def test_link_field_without_url_pattern_should_link_to_value_with_http_by_defaul assert_equal "foo.bar", field.format.formatted_custom_value(self, custom_value, false) assert_equal 'foo.bar', field.format.formatted_custom_value(self, custom_value, true) end + + def test_link_field_with_multiple_values_returns_array_of_links + field = IssueCustomField.new(:field_format => 'link') + custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => ["foo.bar", "bar.foo"]) + + assert_equal ["foo.bar", "bar.foo"], field.format.formatted_custom_value(self, custom_value, false) + assert_equal ['foo.bar', 'bar.foo'], field.format.formatted_custom_value(self, custom_value, true) + end end