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

git-svn-id: http://svn.redmine.org/redmine/trunk@12442 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2013-12-22 14:09:33 +00:00
parent b8b358a85f
commit 4333a80bc1
55 changed files with 123 additions and 17 deletions

View File

@ -1,2 +1,3 @@
<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>

View File

@ -1,2 +1,3 @@
<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>

View File

@ -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>

View File

@ -1,2 +1,3 @@
<%= 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>

View File

@ -1,3 +1,4 @@
<%= 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>

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,8 +146,44 @@ 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))
@ -236,11 +274,17 @@ module Redmine
field_attributes :text_formatting
def formatted_value(view, custom_field, value, customized=nil, html=false)
if html && custom_field.text_formatting == 'full'
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
end
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

View File

@ -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')