Commit b0c5005f authored by jplang's avatar jplang

Adds support for links on values for various custom field formats (#1358).

git-svn-id: https://svn.redmine.org/redmine/trunk@12442 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 512429c4
<p><%= f.select :default_value, [[]]+@custom_field.possible_values_options %></p>
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
<p><%= edit_tag_style_tag f %></p>
<p><%= f.text_field(:default_value, :size => 10) %></p>
<%= calendar_for('custom_field_default_value') %>
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
......@@ -3,4 +3,5 @@
<em class="info"><%= l(:text_custom_field_possible_values_info) %></em>
</p>
<p><%= f.text_field(:default_value) %></p>
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
<p><%= edit_tag_style_tag f %></p>
<%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %>
<p><%= f.text_field(:default_value) %></p>
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
<%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %>
<p><%= f.check_box :text_formatting, {:label => :setting_text_formatting}, 'full', '' %></p>
<p><%= f.check_box :text_formatting, {:label => :setting_text_formatting, :data => {:disables => '#custom_field_url_pattern'}}, 'full', '' %></p>
<p><%= f.text_field(:default_value) %></p>
<p><%= f.text_field :url_pattern, :size => 50, :label => :label_link_values_to %></p>
......@@ -1105,3 +1105,4 @@ ar:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1202,3 +1202,4 @@ az:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1102,3 +1102,4 @@ bg:
description_date_from: Въведете начална дата
description_date_to: Въведете крайна дата
text_repository_identifier_info: 'Позволени са малки букви (a-z), цифри, тирета и _.<br />Промяна след създаването му не е възможна.'
label_link_values_to: Link values to URL
......@@ -1118,3 +1118,4 @@ bs:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1107,3 +1107,4 @@ ca:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1108,3 +1108,4 @@ cs:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1122,3 +1122,4 @@ da:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1117,3 +1117,4 @@ de:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1105,3 +1105,4 @@ el:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1107,3 +1107,4 @@ en-GB:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -907,6 +907,7 @@ en:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
button_login: Login
button_submit: Submit
......
......@@ -1140,3 +1140,4 @@ es:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1118,3 +1118,4 @@ et:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1106,3 +1106,4 @@ eu:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1107,3 +1107,4 @@ fa:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1126,3 +1126,4 @@ fi:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -883,6 +883,7 @@ fr:
label_only: seulement
label_drop_down_list: liste déroulante
label_checkboxes: cases à cocher
label_link_values_to: Lier les valeurs vers l'URL
button_login: Connexion
button_submit: Soumettre
......
......@@ -1116,3 +1116,4 @@ gl:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1110,3 +1110,4 @@ he:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1106,3 +1106,4 @@ hr:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1124,3 +1124,4 @@
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1109,3 +1109,4 @@ id:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1104,3 +1104,4 @@ it:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1132,3 +1132,4 @@ ja:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1154,3 +1154,4 @@ ko:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1164,3 +1164,4 @@ lt:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1099,3 +1099,4 @@ lv:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1105,3 +1105,4 @@ mk:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1106,3 +1106,4 @@ mn:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1084,3 +1084,4 @@ nl:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1095,3 +1095,4 @@
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1127,3 +1127,4 @@ pl:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1125,3 +1125,4 @@ pt-BR:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1111,3 +1111,4 @@ pt:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1100,3 +1100,4 @@ ro:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1214,3 +1214,4 @@ ru:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1095,3 +1095,4 @@ sk:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1105,3 +1105,4 @@ sl:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1101,3 +1101,4 @@ sq:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1107,3 +1107,4 @@ sr-YU:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1106,3 +1106,4 @@ sr:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1143,3 +1143,4 @@ sv:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1102,3 +1102,4 @@ th:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1114,3 +1114,4 @@ tr:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1100,3 +1100,4 @@ uk:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1158,3 +1158,4 @@ vi:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -1185,3 +1185,4 @@
description_date_from: 輸入起始日期
description_date_to: 輸入結束日期
text_repository_identifier_info: '僅允許使用小寫英文字母 (a-z), 阿拉伯數字, 虛線與底線。<br />一旦儲存之後, 代碼便無法再次被更改。'
label_link_values_to: Link values to URL
......@@ -1106,3 +1106,4 @@ zh:
label_only: only
label_drop_down_list: drop-down list
label_checkboxes: checkboxes
label_link_values_to: Link values to URL
......@@ -80,6 +80,8 @@ module Redmine
CustomField.store_accessor :format_store, *args
end
field_attributes :url_pattern
def name
self.class.format_name
end
......@@ -144,9 +146,45 @@ module Redmine
end
def formatted_value(view, custom_field, value, customized=nil, html=false)
cast_value(custom_field, value, customized)
casted = cast_value(custom_field, value, customized)
if custom_field.url_pattern.present?
texts_and_urls = Array.wrap(casted).map do |single_value|
text = view.format_object(single_value, false).to_s
url = url_from_pattern(custom_field, single_value, customized)
[text, url]
end
links = texts_and_urls.sort_by(&:first).map {|text, url| view.link_to text, url}
links.join(', ').html_safe
else
casted
end
end
# Returns an URL generated with the custom field URL pattern
# and variables substitution:
# %value% => the custom field value
# %id% => id of the customized object
# %project_id% => id of the project of the customized object if defined
# %project_identifier% => identifier of the project of the customized object if defined
# %m1%, %m2%... => capture groups matches of the custom field regexp if defined
def url_from_pattern(custom_field, value, customized)
url = custom_field.url_pattern.to_s.dup
url.gsub!('%value%') {value.to_s}
url.gsub!('%id%') {customized.id.to_s}
url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
url.gsub!('%project_identifier%') {(customized.respond_to?(:project) ? customized.project.try(:identifier) : nil).to_s}
if custom_field.regexp.present?
url.gsub!(%r{%m(\d+)%}) do
m = $1.to_i
if matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
matches[m].to_s
end
end
end
url
end
protected :url_from_pattern
def edit_tag(view, tag_id, tag_name, custom_value, options={})
view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id))
end
......@@ -236,8 +274,14 @@ module Redmine
field_attributes :text_formatting
def formatted_value(view, custom_field, value, customized=nil, html=false)
if html && custom_field.text_formatting == 'full'
view.textilizable(value, :object => customized)
if html
if custom_field.url_pattern.present?
super
elsif custom_field.text_formatting == 'full'
view.textilizable(value, :object => customized)
else
value.to_s
end
else
value.to_s
end
......@@ -280,23 +324,11 @@ module Redmine
add 'link'
self.searchable_supported = false
self.form_partial = 'custom_fields/formats/link'
field_attributes :url_pattern
def formatted_value(view, custom_field, value, customized=nil, html=false)
if html
if custom_field.url_pattern.present?
url = custom_field.url_pattern.to_s.dup
url.gsub!('%value%') {value.to_s}
url.gsub!('%id%') {customized.id.to_s}
url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
url.gsub!('%project_identifier%') {(customized.respond_to?(:project) ? customized.project.try(:identifier) : nil).to_s}
if custom_field.regexp.present?
url.gsub!(%r{%m(\d+)%}) do
m = $1.to_i
matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
matches[m].to_s if matches
end
end
url = url_from_pattern(custom_field, value, customized)
else
url = value.to_s
unless url =~ %r{\A[a-z]+://}i
......
......@@ -116,6 +116,27 @@ class Redmine::ListFieldFormatTest < ActionView::TestCase
end
end
def test_field_with_url_pattern_should_link_value
field = IssueCustomField.new(:field_format => 'list', :url_pattern => 'http://localhost/%value%')
formatted = field.format.formatted_value(self, field, 'foo', Issue.new, true)
assert_equal '<a href="http://localhost/foo">foo</a>', formatted
assert formatted.html_safe?
end
def test_field_with_url_pattern_and_multiple_values_should_link_values
field = IssueCustomField.new(:field_format => 'list', :url_pattern => 'http://localhost/%value%')
formatted = field.format.formatted_value(self, field, ['foo', 'bar'], Issue.new, true)
assert_equal '<a href="http://localhost/bar">bar</a>, <a href="http://localhost/foo">foo</a>', formatted
assert formatted.html_safe?
end
def test_field_with_url_pattern_should_not_link_blank_value
field = IssueCustomField.new(:field_format => 'list', :url_pattern => 'http://localhost/%value%')
formatted = field.format.formatted_value(self, field, '', Issue.new, true)
assert_equal '', formatted
assert formatted.html_safe?
end
def test_edit_tag_with_check_box_style_and_multiple_should_select_current_values
field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz'], :is_required => false,
:multiple => true, :edit_tag_style => 'check_box')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment