##// END OF EJS Templates
Custom field List Link values to URL breaks on entries with spaces (#23067)....
Jean-Philippe Lang -
r15157:bbd24fe350a3
parent child
Show More
@@ -1,26 +1,29
1 <h2><%= l(:label_home) %></h2>
1 <h2><%= l(:label_home) %></h2>
2
2
3 <div class="splitcontentleft">
3 <div class="splitcontentleft">
4 <div class="wiki">
4 <div class="wiki">
5 <%= textilizable Setting.welcome_text %>
5 <%= textilizable Setting.welcome_text %>
6 </div>
6 </div>
7 <%= call_hook(:view_welcome_index_left) %>
7 <%= call_hook(:view_welcome_index_left) %>
8 </div>
8 </div>
9
9
10 <%= link_to "Test", "http://foo/test bar" %>
11 <%= link_to "Test", "http://foo/test%20bar" %>
12
10 <div class="splitcontentright">
13 <div class="splitcontentright">
11 <% if @news.any? %>
14 <% if @news.any? %>
12 <div class="news box">
15 <div class="news box">
13 <h3><%=l(:label_news_latest)%></h3>
16 <h3><%=l(:label_news_latest)%></h3>
14 <%= render :partial => 'news/news', :collection => @news %>
17 <%= render :partial => 'news/news', :collection => @news %>
15 <%= link_to l(:label_news_view_all), :controller => 'news' %>
18 <%= link_to l(:label_news_view_all), :controller => 'news' %>
16 </div>
19 </div>
17 <% end %>
20 <% end %>
18 <%= call_hook(:view_welcome_index_right) %>
21 <%= call_hook(:view_welcome_index_right) %>
19 </div>
22 </div>
20
23
21 <% content_for :header_tags do %>
24 <% content_for :header_tags do %>
22 <%= auto_discovery_link_tag(:atom, {:controller => 'news', :action => 'index', :key => User.current.rss_key, :format => 'atom'},
25 <%= auto_discovery_link_tag(:atom, {:controller => 'news', :action => 'index', :key => User.current.rss_key, :format => 'atom'},
23 :title => "#{Setting.app_title}: #{l(:label_news_latest)}") %>
26 :title => "#{Setting.app_title}: #{l(:label_news_latest)}") %>
24 <%= auto_discovery_link_tag(:atom, {:controller => 'activities', :action => 'index', :key => User.current.rss_key, :format => 'atom'},
27 <%= auto_discovery_link_tag(:atom, {:controller => 'activities', :action => 'index', :key => User.current.rss_key, :format => 'atom'},
25 :title => "#{Setting.app_title}: #{l(:label_activity)}") %>
28 :title => "#{Setting.app_title}: #{l(:label_activity)}") %>
26 <% end %>
29 <% end %>
@@ -1,825 +1,827
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'uri'
19
18 module Redmine
20 module Redmine
19 module FieldFormat
21 module FieldFormat
20 def self.add(name, klass)
22 def self.add(name, klass)
21 all[name.to_s] = klass.instance
23 all[name.to_s] = klass.instance
22 end
24 end
23
25
24 def self.delete(name)
26 def self.delete(name)
25 all.delete(name.to_s)
27 all.delete(name.to_s)
26 end
28 end
27
29
28 def self.all
30 def self.all
29 @formats ||= Hash.new(Base.instance)
31 @formats ||= Hash.new(Base.instance)
30 end
32 end
31
33
32 def self.available_formats
34 def self.available_formats
33 all.keys
35 all.keys
34 end
36 end
35
37
36 def self.find(name)
38 def self.find(name)
37 all[name.to_s]
39 all[name.to_s]
38 end
40 end
39
41
40 # Return an array of custom field formats which can be used in select_tag
42 # Return an array of custom field formats which can be used in select_tag
41 def self.as_select(class_name=nil)
43 def self.as_select(class_name=nil)
42 formats = all.values.select do |format|
44 formats = all.values.select do |format|
43 format.class.customized_class_names.nil? || format.class.customized_class_names.include?(class_name)
45 format.class.customized_class_names.nil? || format.class.customized_class_names.include?(class_name)
44 end
46 end
45 formats.map {|format| [::I18n.t(format.label), format.name] }.sort_by(&:first)
47 formats.map {|format| [::I18n.t(format.label), format.name] }.sort_by(&:first)
46 end
48 end
47
49
48 class Base
50 class Base
49 include Singleton
51 include Singleton
50 include Redmine::I18n
52 include Redmine::I18n
51 include Redmine::Helpers::URL
53 include Redmine::Helpers::URL
52 include ERB::Util
54 include ERB::Util
53
55
54 class_attribute :format_name
56 class_attribute :format_name
55 self.format_name = nil
57 self.format_name = nil
56
58
57 # Set this to true if the format supports multiple values
59 # Set this to true if the format supports multiple values
58 class_attribute :multiple_supported
60 class_attribute :multiple_supported
59 self.multiple_supported = false
61 self.multiple_supported = false
60
62
61 # Set this to true if the format supports textual search on custom values
63 # Set this to true if the format supports textual search on custom values
62 class_attribute :searchable_supported
64 class_attribute :searchable_supported
63 self.searchable_supported = false
65 self.searchable_supported = false
64
66
65 # Set this to true if field values can be summed up
67 # Set this to true if field values can be summed up
66 class_attribute :totalable_supported
68 class_attribute :totalable_supported
67 self.totalable_supported = false
69 self.totalable_supported = false
68
70
69 # Restricts the classes that the custom field can be added to
71 # Restricts the classes that the custom field can be added to
70 # Set to nil for no restrictions
72 # Set to nil for no restrictions
71 class_attribute :customized_class_names
73 class_attribute :customized_class_names
72 self.customized_class_names = nil
74 self.customized_class_names = nil
73
75
74 # Name of the partial for editing the custom field
76 # Name of the partial for editing the custom field
75 class_attribute :form_partial
77 class_attribute :form_partial
76 self.form_partial = nil
78 self.form_partial = nil
77
79
78 class_attribute :change_as_diff
80 class_attribute :change_as_diff
79 self.change_as_diff = false
81 self.change_as_diff = false
80
82
81 def self.add(name)
83 def self.add(name)
82 self.format_name = name
84 self.format_name = name
83 Redmine::FieldFormat.add(name, self)
85 Redmine::FieldFormat.add(name, self)
84 end
86 end
85 private_class_method :add
87 private_class_method :add
86
88
87 def self.field_attributes(*args)
89 def self.field_attributes(*args)
88 CustomField.store_accessor :format_store, *args
90 CustomField.store_accessor :format_store, *args
89 end
91 end
90
92
91 field_attributes :url_pattern
93 field_attributes :url_pattern
92
94
93 def name
95 def name
94 self.class.format_name
96 self.class.format_name
95 end
97 end
96
98
97 def label
99 def label
98 "label_#{name}"
100 "label_#{name}"
99 end
101 end
100
102
101 def cast_custom_value(custom_value)
103 def cast_custom_value(custom_value)
102 cast_value(custom_value.custom_field, custom_value.value, custom_value.customized)
104 cast_value(custom_value.custom_field, custom_value.value, custom_value.customized)
103 end
105 end
104
106
105 def cast_value(custom_field, value, customized=nil)
107 def cast_value(custom_field, value, customized=nil)
106 if value.blank?
108 if value.blank?
107 nil
109 nil
108 elsif value.is_a?(Array)
110 elsif value.is_a?(Array)
109 casted = value.map do |v|
111 casted = value.map do |v|
110 cast_single_value(custom_field, v, customized)
112 cast_single_value(custom_field, v, customized)
111 end
113 end
112 casted.compact.sort
114 casted.compact.sort
113 else
115 else
114 cast_single_value(custom_field, value, customized)
116 cast_single_value(custom_field, value, customized)
115 end
117 end
116 end
118 end
117
119
118 def cast_single_value(custom_field, value, customized=nil)
120 def cast_single_value(custom_field, value, customized=nil)
119 value.to_s
121 value.to_s
120 end
122 end
121
123
122 def target_class
124 def target_class
123 nil
125 nil
124 end
126 end
125
127
126 def possible_custom_value_options(custom_value)
128 def possible_custom_value_options(custom_value)
127 possible_values_options(custom_value.custom_field, custom_value.customized)
129 possible_values_options(custom_value.custom_field, custom_value.customized)
128 end
130 end
129
131
130 def possible_values_options(custom_field, object=nil)
132 def possible_values_options(custom_field, object=nil)
131 []
133 []
132 end
134 end
133
135
134 def value_from_keyword(custom_field, keyword, object)
136 def value_from_keyword(custom_field, keyword, object)
135 possible_values_options = possible_values_options(custom_field, object)
137 possible_values_options = possible_values_options(custom_field, object)
136 if possible_values_options.present?
138 if possible_values_options.present?
137 keyword = keyword.to_s
139 keyword = keyword.to_s
138 if v = possible_values_options.detect {|text, id| keyword.casecmp(text) == 0}
140 if v = possible_values_options.detect {|text, id| keyword.casecmp(text) == 0}
139 if v.is_a?(Array)
141 if v.is_a?(Array)
140 v.last
142 v.last
141 else
143 else
142 v
144 v
143 end
145 end
144 end
146 end
145 else
147 else
146 keyword
148 keyword
147 end
149 end
148 end
150 end
149
151
150 # Returns the validation errors for custom_field
152 # Returns the validation errors for custom_field
151 # Should return an empty array if custom_field is valid
153 # Should return an empty array if custom_field is valid
152 def validate_custom_field(custom_field)
154 def validate_custom_field(custom_field)
153 errors = []
155 errors = []
154 pattern = custom_field.url_pattern
156 pattern = custom_field.url_pattern
155 if pattern.present? && !uri_with_safe_scheme?(url_pattern_without_tokens(pattern))
157 if pattern.present? && !uri_with_safe_scheme?(url_pattern_without_tokens(pattern))
156 errors << [:url_pattern, :invalid]
158 errors << [:url_pattern, :invalid]
157 end
159 end
158 errors
160 errors
159 end
161 end
160
162
161 # Returns the validation error messages for custom_value
163 # Returns the validation error messages for custom_value
162 # Should return an empty array if custom_value is valid
164 # Should return an empty array if custom_value is valid
163 def validate_custom_value(custom_value)
165 def validate_custom_value(custom_value)
164 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
166 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
165 errors = values.map do |value|
167 errors = values.map do |value|
166 validate_single_value(custom_value.custom_field, value, custom_value.customized)
168 validate_single_value(custom_value.custom_field, value, custom_value.customized)
167 end
169 end
168 errors.flatten.uniq
170 errors.flatten.uniq
169 end
171 end
170
172
171 def validate_single_value(custom_field, value, customized=nil)
173 def validate_single_value(custom_field, value, customized=nil)
172 []
174 []
173 end
175 end
174
176
175 def formatted_custom_value(view, custom_value, html=false)
177 def formatted_custom_value(view, custom_value, html=false)
176 formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html)
178 formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html)
177 end
179 end
178
180
179 def formatted_value(view, custom_field, value, customized=nil, html=false)
181 def formatted_value(view, custom_field, value, customized=nil, html=false)
180 casted = cast_value(custom_field, value, customized)
182 casted = cast_value(custom_field, value, customized)
181 if html && custom_field.url_pattern.present?
183 if html && custom_field.url_pattern.present?
182 texts_and_urls = Array.wrap(casted).map do |single_value|
184 texts_and_urls = Array.wrap(casted).map do |single_value|
183 text = view.format_object(single_value, false).to_s
185 text = view.format_object(single_value, false).to_s
184 url = url_from_pattern(custom_field, single_value, customized)
186 url = url_from_pattern(custom_field, single_value, customized)
185 [text, url]
187 [text, url]
186 end
188 end
187 links = texts_and_urls.sort_by(&:first).map {|text, url| view.link_to_if uri_with_safe_scheme?(url), text, url}
189 links = texts_and_urls.sort_by(&:first).map {|text, url| view.link_to_if uri_with_safe_scheme?(url), text, url}
188 links.join(', ').html_safe
190 links.join(', ').html_safe
189 else
191 else
190 casted
192 casted
191 end
193 end
192 end
194 end
193
195
194 # Returns an URL generated with the custom field URL pattern
196 # Returns an URL generated with the custom field URL pattern
195 # and variables substitution:
197 # and variables substitution:
196 # %value% => the custom field value
198 # %value% => the custom field value
197 # %id% => id of the customized object
199 # %id% => id of the customized object
198 # %project_id% => id of the project of the customized object if defined
200 # %project_id% => id of the project of the customized object if defined
199 # %project_identifier% => identifier of the project of the customized object if defined
201 # %project_identifier% => identifier of the project of the customized object if defined
200 # %m1%, %m2%... => capture groups matches of the custom field regexp if defined
202 # %m1%, %m2%... => capture groups matches of the custom field regexp if defined
201 def url_from_pattern(custom_field, value, customized)
203 def url_from_pattern(custom_field, value, customized)
202 url = custom_field.url_pattern.to_s.dup
204 url = custom_field.url_pattern.to_s.dup
203 url.gsub!('%value%') {value.to_s}
205 url.gsub!('%value%') {value.to_s}
204 url.gsub!('%id%') {customized.id.to_s}
206 url.gsub!('%id%') {customized.id.to_s}
205 url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
207 url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
206 url.gsub!('%project_identifier%') {(customized.respond_to?(:project) ? customized.project.try(:identifier) : nil).to_s}
208 url.gsub!('%project_identifier%') {(customized.respond_to?(:project) ? customized.project.try(:identifier) : nil).to_s}
207 if custom_field.regexp.present?
209 if custom_field.regexp.present?
208 url.gsub!(%r{%m(\d+)%}) do
210 url.gsub!(%r{%m(\d+)%}) do
209 m = $1.to_i
211 m = $1.to_i
210 if matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
212 if matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
211 matches[m].to_s
213 matches[m].to_s
212 end
214 end
213 end
215 end
214 end
216 end
215 url
217 URI.encode(url)
216 end
218 end
217 protected :url_from_pattern
219 protected :url_from_pattern
218
220
219 # Returns the URL pattern with substitution tokens removed,
221 # Returns the URL pattern with substitution tokens removed,
220 # for validation purpose
222 # for validation purpose
221 def url_pattern_without_tokens(url_pattern)
223 def url_pattern_without_tokens(url_pattern)
222 url_pattern.to_s.gsub(/%(value|id|project_id|project_identifier|m\d+)%/, '')
224 url_pattern.to_s.gsub(/%(value|id|project_id|project_identifier|m\d+)%/, '')
223 end
225 end
224 protected :url_pattern_without_tokens
226 protected :url_pattern_without_tokens
225
227
226 def edit_tag(view, tag_id, tag_name, custom_value, options={})
228 def edit_tag(view, tag_id, tag_name, custom_value, options={})
227 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id))
229 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id))
228 end
230 end
229
231
230 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
232 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
231 view.text_field_tag(tag_name, value, options.merge(:id => tag_id)) +
233 view.text_field_tag(tag_name, value, options.merge(:id => tag_id)) +
232 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
234 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
233 end
235 end
234
236
235 def bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
237 def bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
236 if custom_field.is_required?
238 if custom_field.is_required?
237 ''.html_safe
239 ''.html_safe
238 else
240 else
239 view.content_tag('label',
241 view.content_tag('label',
240 view.check_box_tag(tag_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{tag_id}"}) + l(:button_clear),
242 view.check_box_tag(tag_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{tag_id}"}) + l(:button_clear),
241 :class => 'inline'
243 :class => 'inline'
242 )
244 )
243 end
245 end
244 end
246 end
245 protected :bulk_clear_tag
247 protected :bulk_clear_tag
246
248
247 def query_filter_options(custom_field, query)
249 def query_filter_options(custom_field, query)
248 {:type => :string}
250 {:type => :string}
249 end
251 end
250
252
251 def before_custom_field_save(custom_field)
253 def before_custom_field_save(custom_field)
252 end
254 end
253
255
254 # Returns a ORDER BY clause that can used to sort customized
256 # Returns a ORDER BY clause that can used to sort customized
255 # objects by their value of the custom field.
257 # objects by their value of the custom field.
256 # Returns nil if the custom field can not be used for sorting.
258 # Returns nil if the custom field can not be used for sorting.
257 def order_statement(custom_field)
259 def order_statement(custom_field)
258 # COALESCE is here to make sure that blank and NULL values are sorted equally
260 # COALESCE is here to make sure that blank and NULL values are sorted equally
259 "COALESCE(#{join_alias custom_field}.value, '')"
261 "COALESCE(#{join_alias custom_field}.value, '')"
260 end
262 end
261
263
262 # Returns a GROUP BY clause that can used to group by custom value
264 # Returns a GROUP BY clause that can used to group by custom value
263 # Returns nil if the custom field can not be used for grouping.
265 # Returns nil if the custom field can not be used for grouping.
264 def group_statement(custom_field)
266 def group_statement(custom_field)
265 nil
267 nil
266 end
268 end
267
269
268 # Returns a JOIN clause that is added to the query when sorting by custom values
270 # Returns a JOIN clause that is added to the query when sorting by custom values
269 def join_for_order_statement(custom_field)
271 def join_for_order_statement(custom_field)
270 alias_name = join_alias(custom_field)
272 alias_name = join_alias(custom_field)
271
273
272 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
274 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
273 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
275 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
274 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
276 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
275 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
277 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
276 " AND (#{custom_field.visibility_by_project_condition})" +
278 " AND (#{custom_field.visibility_by_project_condition})" +
277 " AND #{alias_name}.value <> ''" +
279 " AND #{alias_name}.value <> ''" +
278 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
280 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
279 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
281 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
280 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
282 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
281 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)"
283 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)"
282 end
284 end
283
285
284 def join_alias(custom_field)
286 def join_alias(custom_field)
285 "cf_#{custom_field.id}"
287 "cf_#{custom_field.id}"
286 end
288 end
287 protected :join_alias
289 protected :join_alias
288 end
290 end
289
291
290 class Unbounded < Base
292 class Unbounded < Base
291 def validate_single_value(custom_field, value, customized=nil)
293 def validate_single_value(custom_field, value, customized=nil)
292 errs = super
294 errs = super
293 value = value.to_s
295 value = value.to_s
294 unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
296 unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
295 errs << ::I18n.t('activerecord.errors.messages.invalid')
297 errs << ::I18n.t('activerecord.errors.messages.invalid')
296 end
298 end
297 if custom_field.min_length && value.length < custom_field.min_length
299 if custom_field.min_length && value.length < custom_field.min_length
298 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => custom_field.min_length)
300 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => custom_field.min_length)
299 end
301 end
300 if custom_field.max_length && custom_field.max_length > 0 && value.length > custom_field.max_length
302 if custom_field.max_length && custom_field.max_length > 0 && value.length > custom_field.max_length
301 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => custom_field.max_length)
303 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => custom_field.max_length)
302 end
304 end
303 errs
305 errs
304 end
306 end
305 end
307 end
306
308
307 class StringFormat < Unbounded
309 class StringFormat < Unbounded
308 add 'string'
310 add 'string'
309 self.searchable_supported = true
311 self.searchable_supported = true
310 self.form_partial = 'custom_fields/formats/string'
312 self.form_partial = 'custom_fields/formats/string'
311 field_attributes :text_formatting
313 field_attributes :text_formatting
312
314
313 def formatted_value(view, custom_field, value, customized=nil, html=false)
315 def formatted_value(view, custom_field, value, customized=nil, html=false)
314 if html
316 if html
315 if custom_field.url_pattern.present?
317 if custom_field.url_pattern.present?
316 super
318 super
317 elsif custom_field.text_formatting == 'full'
319 elsif custom_field.text_formatting == 'full'
318 view.textilizable(value, :object => customized)
320 view.textilizable(value, :object => customized)
319 else
321 else
320 value.to_s
322 value.to_s
321 end
323 end
322 else
324 else
323 value.to_s
325 value.to_s
324 end
326 end
325 end
327 end
326 end
328 end
327
329
328 class TextFormat < Unbounded
330 class TextFormat < Unbounded
329 add 'text'
331 add 'text'
330 self.searchable_supported = true
332 self.searchable_supported = true
331 self.form_partial = 'custom_fields/formats/text'
333 self.form_partial = 'custom_fields/formats/text'
332 self.change_as_diff = true
334 self.change_as_diff = true
333
335
334 def formatted_value(view, custom_field, value, customized=nil, html=false)
336 def formatted_value(view, custom_field, value, customized=nil, html=false)
335 if html
337 if html
336 if value.present?
338 if value.present?
337 if custom_field.text_formatting == 'full'
339 if custom_field.text_formatting == 'full'
338 view.textilizable(value, :object => customized)
340 view.textilizable(value, :object => customized)
339 else
341 else
340 view.simple_format(html_escape(value))
342 view.simple_format(html_escape(value))
341 end
343 end
342 else
344 else
343 ''
345 ''
344 end
346 end
345 else
347 else
346 value.to_s
348 value.to_s
347 end
349 end
348 end
350 end
349
351
350 def edit_tag(view, tag_id, tag_name, custom_value, options={})
352 def edit_tag(view, tag_id, tag_name, custom_value, options={})
351 view.text_area_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :rows => 3))
353 view.text_area_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :rows => 3))
352 end
354 end
353
355
354 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
356 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
355 view.text_area_tag(tag_name, value, options.merge(:id => tag_id, :rows => 3)) +
357 view.text_area_tag(tag_name, value, options.merge(:id => tag_id, :rows => 3)) +
356 '<br />'.html_safe +
358 '<br />'.html_safe +
357 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
359 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
358 end
360 end
359
361
360 def query_filter_options(custom_field, query)
362 def query_filter_options(custom_field, query)
361 {:type => :text}
363 {:type => :text}
362 end
364 end
363 end
365 end
364
366
365 class LinkFormat < StringFormat
367 class LinkFormat < StringFormat
366 add 'link'
368 add 'link'
367 self.searchable_supported = false
369 self.searchable_supported = false
368 self.form_partial = 'custom_fields/formats/link'
370 self.form_partial = 'custom_fields/formats/link'
369
371
370 def formatted_value(view, custom_field, value, customized=nil, html=false)
372 def formatted_value(view, custom_field, value, customized=nil, html=false)
371 if html && value.present?
373 if html && value.present?
372 if custom_field.url_pattern.present?
374 if custom_field.url_pattern.present?
373 url = url_from_pattern(custom_field, value, customized)
375 url = url_from_pattern(custom_field, value, customized)
374 else
376 else
375 url = value.to_s
377 url = value.to_s
376 unless url =~ %r{\A[a-z]+://}i
378 unless url =~ %r{\A[a-z]+://}i
377 # no protocol found, use http by default
379 # no protocol found, use http by default
378 url = "http://" + url
380 url = "http://" + url
379 end
381 end
380 end
382 end
381 view.link_to value.to_s.truncate(40), url
383 view.link_to value.to_s.truncate(40), url
382 else
384 else
383 value.to_s
385 value.to_s
384 end
386 end
385 end
387 end
386 end
388 end
387
389
388 class Numeric < Unbounded
390 class Numeric < Unbounded
389 self.form_partial = 'custom_fields/formats/numeric'
391 self.form_partial = 'custom_fields/formats/numeric'
390 self.totalable_supported = true
392 self.totalable_supported = true
391
393
392 def order_statement(custom_field)
394 def order_statement(custom_field)
393 # Make the database cast values into numeric
395 # Make the database cast values into numeric
394 # Postgresql will raise an error if a value can not be casted!
396 # Postgresql will raise an error if a value can not be casted!
395 # CustomValue validations should ensure that it doesn't occur
397 # CustomValue validations should ensure that it doesn't occur
396 "CAST(CASE #{join_alias custom_field}.value WHEN '' THEN '0' ELSE #{join_alias custom_field}.value END AS decimal(30,3))"
398 "CAST(CASE #{join_alias custom_field}.value WHEN '' THEN '0' ELSE #{join_alias custom_field}.value END AS decimal(30,3))"
397 end
399 end
398
400
399 # Returns totals for the given scope
401 # Returns totals for the given scope
400 def total_for_scope(custom_field, scope)
402 def total_for_scope(custom_field, scope)
401 scope.joins(:custom_values).
403 scope.joins(:custom_values).
402 where(:custom_values => {:custom_field_id => custom_field.id}).
404 where(:custom_values => {:custom_field_id => custom_field.id}).
403 where.not(:custom_values => {:value => ''}).
405 where.not(:custom_values => {:value => ''}).
404 sum("CAST(#{CustomValue.table_name}.value AS decimal(30,3))")
406 sum("CAST(#{CustomValue.table_name}.value AS decimal(30,3))")
405 end
407 end
406
408
407 def cast_total_value(custom_field, value)
409 def cast_total_value(custom_field, value)
408 cast_single_value(custom_field, value)
410 cast_single_value(custom_field, value)
409 end
411 end
410 end
412 end
411
413
412 class IntFormat < Numeric
414 class IntFormat < Numeric
413 add 'int'
415 add 'int'
414
416
415 def label
417 def label
416 "label_integer"
418 "label_integer"
417 end
419 end
418
420
419 def cast_single_value(custom_field, value, customized=nil)
421 def cast_single_value(custom_field, value, customized=nil)
420 value.to_i
422 value.to_i
421 end
423 end
422
424
423 def validate_single_value(custom_field, value, customized=nil)
425 def validate_single_value(custom_field, value, customized=nil)
424 errs = super
426 errs = super
425 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value.to_s =~ /^[+-]?\d+$/
427 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value.to_s =~ /^[+-]?\d+$/
426 errs
428 errs
427 end
429 end
428
430
429 def query_filter_options(custom_field, query)
431 def query_filter_options(custom_field, query)
430 {:type => :integer}
432 {:type => :integer}
431 end
433 end
432
434
433 def group_statement(custom_field)
435 def group_statement(custom_field)
434 order_statement(custom_field)
436 order_statement(custom_field)
435 end
437 end
436 end
438 end
437
439
438 class FloatFormat < Numeric
440 class FloatFormat < Numeric
439 add 'float'
441 add 'float'
440
442
441 def cast_single_value(custom_field, value, customized=nil)
443 def cast_single_value(custom_field, value, customized=nil)
442 value.to_f
444 value.to_f
443 end
445 end
444
446
445 def cast_total_value(custom_field, value)
447 def cast_total_value(custom_field, value)
446 value.to_f.round(2)
448 value.to_f.round(2)
447 end
449 end
448
450
449 def validate_single_value(custom_field, value, customized=nil)
451 def validate_single_value(custom_field, value, customized=nil)
450 errs = super
452 errs = super
451 errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
453 errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
452 errs
454 errs
453 end
455 end
454
456
455 def query_filter_options(custom_field, query)
457 def query_filter_options(custom_field, query)
456 {:type => :float}
458 {:type => :float}
457 end
459 end
458 end
460 end
459
461
460 class DateFormat < Unbounded
462 class DateFormat < Unbounded
461 add 'date'
463 add 'date'
462 self.form_partial = 'custom_fields/formats/date'
464 self.form_partial = 'custom_fields/formats/date'
463
465
464 def cast_single_value(custom_field, value, customized=nil)
466 def cast_single_value(custom_field, value, customized=nil)
465 value.to_date rescue nil
467 value.to_date rescue nil
466 end
468 end
467
469
468 def validate_single_value(custom_field, value, customized=nil)
470 def validate_single_value(custom_field, value, customized=nil)
469 if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)
471 if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)
470 []
472 []
471 else
473 else
472 [::I18n.t('activerecord.errors.messages.not_a_date')]
474 [::I18n.t('activerecord.errors.messages.not_a_date')]
473 end
475 end
474 end
476 end
475
477
476 def edit_tag(view, tag_id, tag_name, custom_value, options={})
478 def edit_tag(view, tag_id, tag_name, custom_value, options={})
477 view.date_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10)) +
479 view.date_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10)) +
478 view.calendar_for(tag_id)
480 view.calendar_for(tag_id)
479 end
481 end
480
482
481 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
483 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
482 view.date_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10)) +
484 view.date_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10)) +
483 view.calendar_for(tag_id) +
485 view.calendar_for(tag_id) +
484 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
486 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
485 end
487 end
486
488
487 def query_filter_options(custom_field, query)
489 def query_filter_options(custom_field, query)
488 {:type => :date}
490 {:type => :date}
489 end
491 end
490
492
491 def group_statement(custom_field)
493 def group_statement(custom_field)
492 order_statement(custom_field)
494 order_statement(custom_field)
493 end
495 end
494 end
496 end
495
497
496 class List < Base
498 class List < Base
497 self.multiple_supported = true
499 self.multiple_supported = true
498 field_attributes :edit_tag_style
500 field_attributes :edit_tag_style
499
501
500 def edit_tag(view, tag_id, tag_name, custom_value, options={})
502 def edit_tag(view, tag_id, tag_name, custom_value, options={})
501 if custom_value.custom_field.edit_tag_style == 'check_box'
503 if custom_value.custom_field.edit_tag_style == 'check_box'
502 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
504 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
503 else
505 else
504 select_edit_tag(view, tag_id, tag_name, custom_value, options)
506 select_edit_tag(view, tag_id, tag_name, custom_value, options)
505 end
507 end
506 end
508 end
507
509
508 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
510 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
509 opts = []
511 opts = []
510 opts << [l(:label_no_change_option), ''] unless custom_field.multiple?
512 opts << [l(:label_no_change_option), ''] unless custom_field.multiple?
511 opts << [l(:label_none), '__none__'] unless custom_field.is_required?
513 opts << [l(:label_none), '__none__'] unless custom_field.is_required?
512 opts += possible_values_options(custom_field, objects)
514 opts += possible_values_options(custom_field, objects)
513 view.select_tag(tag_name, view.options_for_select(opts, value), options.merge(:multiple => custom_field.multiple?))
515 view.select_tag(tag_name, view.options_for_select(opts, value), options.merge(:multiple => custom_field.multiple?))
514 end
516 end
515
517
516 def query_filter_options(custom_field, query)
518 def query_filter_options(custom_field, query)
517 {:type => :list_optional, :values => query_filter_values(custom_field, query)}
519 {:type => :list_optional, :values => query_filter_values(custom_field, query)}
518 end
520 end
519
521
520 protected
522 protected
521
523
522 # Returns the values that are available in the field filter
524 # Returns the values that are available in the field filter
523 def query_filter_values(custom_field, query)
525 def query_filter_values(custom_field, query)
524 possible_values_options(custom_field, query.project)
526 possible_values_options(custom_field, query.project)
525 end
527 end
526
528
527 # Renders the edit tag as a select tag
529 # Renders the edit tag as a select tag
528 def select_edit_tag(view, tag_id, tag_name, custom_value, options={})
530 def select_edit_tag(view, tag_id, tag_name, custom_value, options={})
529 blank_option = ''.html_safe
531 blank_option = ''.html_safe
530 unless custom_value.custom_field.multiple?
532 unless custom_value.custom_field.multiple?
531 if custom_value.custom_field.is_required?
533 if custom_value.custom_field.is_required?
532 unless custom_value.custom_field.default_value.present?
534 unless custom_value.custom_field.default_value.present?
533 blank_option = view.content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
535 blank_option = view.content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
534 end
536 end
535 else
537 else
536 blank_option = view.content_tag('option', '&nbsp;'.html_safe, :value => '')
538 blank_option = view.content_tag('option', '&nbsp;'.html_safe, :value => '')
537 end
539 end
538 end
540 end
539 options_tags = blank_option + view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
541 options_tags = blank_option + view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
540 s = view.select_tag(tag_name, options_tags, options.merge(:id => tag_id, :multiple => custom_value.custom_field.multiple?))
542 s = view.select_tag(tag_name, options_tags, options.merge(:id => tag_id, :multiple => custom_value.custom_field.multiple?))
541 if custom_value.custom_field.multiple?
543 if custom_value.custom_field.multiple?
542 s << view.hidden_field_tag(tag_name, '')
544 s << view.hidden_field_tag(tag_name, '')
543 end
545 end
544 s
546 s
545 end
547 end
546
548
547 # Renders the edit tag as check box or radio tags
549 # Renders the edit tag as check box or radio tags
548 def check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
550 def check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
549 opts = []
551 opts = []
550 unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required?
552 unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required?
551 opts << ["(#{l(:label_none)})", '']
553 opts << ["(#{l(:label_none)})", '']
552 end
554 end
553 opts += possible_custom_value_options(custom_value)
555 opts += possible_custom_value_options(custom_value)
554 s = ''.html_safe
556 s = ''.html_safe
555 tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag
557 tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag
556 opts.each do |label, value|
558 opts.each do |label, value|
557 value ||= label
559 value ||= label
558 checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value
560 checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value
559 tag = view.send(tag_method, tag_name, value, checked, :id => tag_id)
561 tag = view.send(tag_method, tag_name, value, checked, :id => tag_id)
560 # set the id on the first tag only
562 # set the id on the first tag only
561 tag_id = nil
563 tag_id = nil
562 s << view.content_tag('label', tag + ' ' + label)
564 s << view.content_tag('label', tag + ' ' + label)
563 end
565 end
564 if custom_value.custom_field.multiple?
566 if custom_value.custom_field.multiple?
565 s << view.hidden_field_tag(tag_name, '')
567 s << view.hidden_field_tag(tag_name, '')
566 end
568 end
567 css = "#{options[:class]} check_box_group"
569 css = "#{options[:class]} check_box_group"
568 view.content_tag('span', s, options.merge(:class => css))
570 view.content_tag('span', s, options.merge(:class => css))
569 end
571 end
570 end
572 end
571
573
572 class ListFormat < List
574 class ListFormat < List
573 add 'list'
575 add 'list'
574 self.searchable_supported = true
576 self.searchable_supported = true
575 self.form_partial = 'custom_fields/formats/list'
577 self.form_partial = 'custom_fields/formats/list'
576
578
577 def possible_custom_value_options(custom_value)
579 def possible_custom_value_options(custom_value)
578 options = possible_values_options(custom_value.custom_field)
580 options = possible_values_options(custom_value.custom_field)
579 missing = [custom_value.value].flatten.reject(&:blank?) - options
581 missing = [custom_value.value].flatten.reject(&:blank?) - options
580 if missing.any?
582 if missing.any?
581 options += missing
583 options += missing
582 end
584 end
583 options
585 options
584 end
586 end
585
587
586 def possible_values_options(custom_field, object=nil)
588 def possible_values_options(custom_field, object=nil)
587 custom_field.possible_values
589 custom_field.possible_values
588 end
590 end
589
591
590 def validate_custom_field(custom_field)
592 def validate_custom_field(custom_field)
591 errors = []
593 errors = []
592 errors << [:possible_values, :blank] if custom_field.possible_values.blank?
594 errors << [:possible_values, :blank] if custom_field.possible_values.blank?
593 errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array
595 errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array
594 errors
596 errors
595 end
597 end
596
598
597 def validate_custom_value(custom_value)
599 def validate_custom_value(custom_value)
598 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
600 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
599 invalid_values = values - Array.wrap(custom_value.value_was) - custom_value.custom_field.possible_values
601 invalid_values = values - Array.wrap(custom_value.value_was) - custom_value.custom_field.possible_values
600 if invalid_values.any?
602 if invalid_values.any?
601 [::I18n.t('activerecord.errors.messages.inclusion')]
603 [::I18n.t('activerecord.errors.messages.inclusion')]
602 else
604 else
603 []
605 []
604 end
606 end
605 end
607 end
606
608
607 def group_statement(custom_field)
609 def group_statement(custom_field)
608 order_statement(custom_field)
610 order_statement(custom_field)
609 end
611 end
610 end
612 end
611
613
612 class BoolFormat < List
614 class BoolFormat < List
613 add 'bool'
615 add 'bool'
614 self.multiple_supported = false
616 self.multiple_supported = false
615 self.form_partial = 'custom_fields/formats/bool'
617 self.form_partial = 'custom_fields/formats/bool'
616
618
617 def label
619 def label
618 "label_boolean"
620 "label_boolean"
619 end
621 end
620
622
621 def cast_single_value(custom_field, value, customized=nil)
623 def cast_single_value(custom_field, value, customized=nil)
622 value == '1' ? true : false
624 value == '1' ? true : false
623 end
625 end
624
626
625 def possible_values_options(custom_field, object=nil)
627 def possible_values_options(custom_field, object=nil)
626 [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
628 [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
627 end
629 end
628
630
629 def group_statement(custom_field)
631 def group_statement(custom_field)
630 order_statement(custom_field)
632 order_statement(custom_field)
631 end
633 end
632
634
633 def edit_tag(view, tag_id, tag_name, custom_value, options={})
635 def edit_tag(view, tag_id, tag_name, custom_value, options={})
634 case custom_value.custom_field.edit_tag_style
636 case custom_value.custom_field.edit_tag_style
635 when 'check_box'
637 when 'check_box'
636 single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
638 single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
637 when 'radio'
639 when 'radio'
638 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
640 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
639 else
641 else
640 select_edit_tag(view, tag_id, tag_name, custom_value, options)
642 select_edit_tag(view, tag_id, tag_name, custom_value, options)
641 end
643 end
642 end
644 end
643
645
644 # Renders the edit tag as a simple check box
646 # Renders the edit tag as a simple check box
645 def single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
647 def single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
646 s = ''.html_safe
648 s = ''.html_safe
647 s << view.hidden_field_tag(tag_name, '0', :id => nil)
649 s << view.hidden_field_tag(tag_name, '0', :id => nil)
648 s << view.check_box_tag(tag_name, '1', custom_value.value.to_s == '1', :id => tag_id)
650 s << view.check_box_tag(tag_name, '1', custom_value.value.to_s == '1', :id => tag_id)
649 view.content_tag('span', s, options)
651 view.content_tag('span', s, options)
650 end
652 end
651 end
653 end
652
654
653 class RecordList < List
655 class RecordList < List
654 self.customized_class_names = %w(Issue TimeEntry Version Document Project)
656 self.customized_class_names = %w(Issue TimeEntry Version Document Project)
655
657
656 def cast_single_value(custom_field, value, customized=nil)
658 def cast_single_value(custom_field, value, customized=nil)
657 target_class.find_by_id(value.to_i) if value.present?
659 target_class.find_by_id(value.to_i) if value.present?
658 end
660 end
659
661
660 def target_class
662 def target_class
661 @target_class ||= self.class.name[/^(.*::)?(.+)Format$/, 2].constantize rescue nil
663 @target_class ||= self.class.name[/^(.*::)?(.+)Format$/, 2].constantize rescue nil
662 end
664 end
663
665
664 def reset_target_class
666 def reset_target_class
665 @target_class = nil
667 @target_class = nil
666 end
668 end
667
669
668 def possible_custom_value_options(custom_value)
670 def possible_custom_value_options(custom_value)
669 options = possible_values_options(custom_value.custom_field, custom_value.customized)
671 options = possible_values_options(custom_value.custom_field, custom_value.customized)
670 missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last)
672 missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last)
671 if missing.any?
673 if missing.any?
672 options += target_class.where(:id => missing.map(&:to_i)).map {|o| [o.to_s, o.id.to_s]}
674 options += target_class.where(:id => missing.map(&:to_i)).map {|o| [o.to_s, o.id.to_s]}
673 end
675 end
674 options
676 options
675 end
677 end
676
678
677 def order_statement(custom_field)
679 def order_statement(custom_field)
678 if target_class.respond_to?(:fields_for_order_statement)
680 if target_class.respond_to?(:fields_for_order_statement)
679 target_class.fields_for_order_statement(value_join_alias(custom_field))
681 target_class.fields_for_order_statement(value_join_alias(custom_field))
680 end
682 end
681 end
683 end
682
684
683 def group_statement(custom_field)
685 def group_statement(custom_field)
684 "COALESCE(#{join_alias custom_field}.value, '')"
686 "COALESCE(#{join_alias custom_field}.value, '')"
685 end
687 end
686
688
687 def join_for_order_statement(custom_field)
689 def join_for_order_statement(custom_field)
688 alias_name = join_alias(custom_field)
690 alias_name = join_alias(custom_field)
689
691
690 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
692 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
691 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
693 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
692 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
694 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
693 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
695 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
694 " AND (#{custom_field.visibility_by_project_condition})" +
696 " AND (#{custom_field.visibility_by_project_condition})" +
695 " AND #{alias_name}.value <> ''" +
697 " AND #{alias_name}.value <> ''" +
696 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
698 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
697 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
699 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
698 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
700 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
699 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)" +
701 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)" +
700 " LEFT OUTER JOIN #{target_class.table_name} #{value_join_alias custom_field}" +
702 " LEFT OUTER JOIN #{target_class.table_name} #{value_join_alias custom_field}" +
701 " ON CAST(CASE #{alias_name}.value WHEN '' THEN '0' ELSE #{alias_name}.value END AS decimal(30,0)) = #{value_join_alias custom_field}.id"
703 " ON CAST(CASE #{alias_name}.value WHEN '' THEN '0' ELSE #{alias_name}.value END AS decimal(30,0)) = #{value_join_alias custom_field}.id"
702 end
704 end
703
705
704 def value_join_alias(custom_field)
706 def value_join_alias(custom_field)
705 join_alias(custom_field) + "_" + custom_field.field_format
707 join_alias(custom_field) + "_" + custom_field.field_format
706 end
708 end
707 protected :value_join_alias
709 protected :value_join_alias
708 end
710 end
709
711
710 class EnumerationFormat < RecordList
712 class EnumerationFormat < RecordList
711 add 'enumeration'
713 add 'enumeration'
712 self.form_partial = 'custom_fields/formats/enumeration'
714 self.form_partial = 'custom_fields/formats/enumeration'
713
715
714 def label
716 def label
715 "label_field_format_enumeration"
717 "label_field_format_enumeration"
716 end
718 end
717
719
718 def target_class
720 def target_class
719 @target_class ||= CustomFieldEnumeration
721 @target_class ||= CustomFieldEnumeration
720 end
722 end
721
723
722 def possible_values_options(custom_field, object=nil)
724 def possible_values_options(custom_field, object=nil)
723 possible_values_records(custom_field, object).map {|u| [u.name, u.id.to_s]}
725 possible_values_records(custom_field, object).map {|u| [u.name, u.id.to_s]}
724 end
726 end
725
727
726 def possible_values_records(custom_field, object=nil)
728 def possible_values_records(custom_field, object=nil)
727 custom_field.enumerations.active
729 custom_field.enumerations.active
728 end
730 end
729
731
730 def value_from_keyword(custom_field, keyword, object)
732 def value_from_keyword(custom_field, keyword, object)
731 value = custom_field.enumerations.where("LOWER(name) LIKE LOWER(?)", keyword).first
733 value = custom_field.enumerations.where("LOWER(name) LIKE LOWER(?)", keyword).first
732 value ? value.id : nil
734 value ? value.id : nil
733 end
735 end
734 end
736 end
735
737
736 class UserFormat < RecordList
738 class UserFormat < RecordList
737 add 'user'
739 add 'user'
738 self.form_partial = 'custom_fields/formats/user'
740 self.form_partial = 'custom_fields/formats/user'
739 field_attributes :user_role
741 field_attributes :user_role
740
742
741 def possible_values_options(custom_field, object=nil)
743 def possible_values_options(custom_field, object=nil)
742 possible_values_records(custom_field, object).map {|u| [u.name, u.id.to_s]}
744 possible_values_records(custom_field, object).map {|u| [u.name, u.id.to_s]}
743 end
745 end
744
746
745 def possible_values_records(custom_field, object=nil)
747 def possible_values_records(custom_field, object=nil)
746 if object.is_a?(Array)
748 if object.is_a?(Array)
747 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
749 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
748 projects.map {|project| possible_values_records(custom_field, project)}.reduce(:&) || []
750 projects.map {|project| possible_values_records(custom_field, project)}.reduce(:&) || []
749 elsif object.respond_to?(:project) && object.project
751 elsif object.respond_to?(:project) && object.project
750 scope = object.project.users
752 scope = object.project.users
751 if custom_field.user_role.is_a?(Array)
753 if custom_field.user_role.is_a?(Array)
752 role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i)
754 role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i)
753 if role_ids.any?
755 if role_ids.any?
754 scope = scope.where("#{Member.table_name}.id IN (SELECT DISTINCT member_id FROM #{MemberRole.table_name} WHERE role_id IN (?))", role_ids)
756 scope = scope.where("#{Member.table_name}.id IN (SELECT DISTINCT member_id FROM #{MemberRole.table_name} WHERE role_id IN (?))", role_ids)
755 end
757 end
756 end
758 end
757 scope.sorted
759 scope.sorted
758 else
760 else
759 []
761 []
760 end
762 end
761 end
763 end
762
764
763 def value_from_keyword(custom_field, keyword, object)
765 def value_from_keyword(custom_field, keyword, object)
764 users = possible_values_records(custom_field, object).to_a
766 users = possible_values_records(custom_field, object).to_a
765 user = Principal.detect_by_keyword(users, keyword)
767 user = Principal.detect_by_keyword(users, keyword)
766 user ? user.id : nil
768 user ? user.id : nil
767 end
769 end
768
770
769 def before_custom_field_save(custom_field)
771 def before_custom_field_save(custom_field)
770 super
772 super
771 if custom_field.user_role.is_a?(Array)
773 if custom_field.user_role.is_a?(Array)
772 custom_field.user_role.map!(&:to_s).reject!(&:blank?)
774 custom_field.user_role.map!(&:to_s).reject!(&:blank?)
773 end
775 end
774 end
776 end
775 end
777 end
776
778
777 class VersionFormat < RecordList
779 class VersionFormat < RecordList
778 add 'version'
780 add 'version'
779 self.form_partial = 'custom_fields/formats/version'
781 self.form_partial = 'custom_fields/formats/version'
780 field_attributes :version_status
782 field_attributes :version_status
781
783
782 def possible_values_options(custom_field, object=nil)
784 def possible_values_options(custom_field, object=nil)
783 versions_options(custom_field, object)
785 versions_options(custom_field, object)
784 end
786 end
785
787
786 def before_custom_field_save(custom_field)
788 def before_custom_field_save(custom_field)
787 super
789 super
788 if custom_field.version_status.is_a?(Array)
790 if custom_field.version_status.is_a?(Array)
789 custom_field.version_status.map!(&:to_s).reject!(&:blank?)
791 custom_field.version_status.map!(&:to_s).reject!(&:blank?)
790 end
792 end
791 end
793 end
792
794
793 protected
795 protected
794
796
795 def query_filter_values(custom_field, query)
797 def query_filter_values(custom_field, query)
796 versions_options(custom_field, query.project, true)
798 versions_options(custom_field, query.project, true)
797 end
799 end
798
800
799 def versions_options(custom_field, object, all_statuses=false)
801 def versions_options(custom_field, object, all_statuses=false)
800 if object.is_a?(Array)
802 if object.is_a?(Array)
801 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
803 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
802 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
804 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
803 elsif object.respond_to?(:project) && object.project
805 elsif object.respond_to?(:project) && object.project
804 scope = object.project.shared_versions
806 scope = object.project.shared_versions
805 filtered_versions_options(custom_field, scope, all_statuses)
807 filtered_versions_options(custom_field, scope, all_statuses)
806 elsif object.nil?
808 elsif object.nil?
807 scope = Version.visible.where(:sharing => 'system')
809 scope = Version.visible.where(:sharing => 'system')
808 filtered_versions_options(custom_field, scope, all_statuses)
810 filtered_versions_options(custom_field, scope, all_statuses)
809 else
811 else
810 []
812 []
811 end
813 end
812 end
814 end
813
815
814 def filtered_versions_options(custom_field, scope, all_statuses=false)
816 def filtered_versions_options(custom_field, scope, all_statuses=false)
815 if !all_statuses && custom_field.version_status.is_a?(Array)
817 if !all_statuses && custom_field.version_status.is_a?(Array)
816 statuses = custom_field.version_status.map(&:to_s).reject(&:blank?)
818 statuses = custom_field.version_status.map(&:to_s).reject(&:blank?)
817 if statuses.any?
819 if statuses.any?
818 scope = scope.where(:status => statuses.map(&:to_s))
820 scope = scope.where(:status => statuses.map(&:to_s))
819 end
821 end
820 end
822 end
821 scope.sort.collect{|u| [u.to_s, u.id.to_s] }
823 scope.sort.collect{|u| [u.to_s, u.id.to_s] }
822 end
824 end
823 end
825 end
824 end
826 end
825 end
827 end
@@ -1,77 +1,85
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../../../../test_helper', __FILE__)
18 require File.expand_path('../../../../../test_helper', __FILE__)
19
19
20 class Redmine::FieldFormatTest < ActionView::TestCase
20 class Redmine::FieldFormatTest < ActionView::TestCase
21 include ApplicationHelper
21 include ApplicationHelper
22
22
23 def setup
23 def setup
24 set_language_if_valid 'en'
24 set_language_if_valid 'en'
25 end
25 end
26
26
27 def test_string_field_with_text_formatting_disabled_should_not_format_text
27 def test_string_field_with_text_formatting_disabled_should_not_format_text
28 field = IssueCustomField.new(:field_format => 'string')
28 field = IssueCustomField.new(:field_format => 'string')
29 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*")
29 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*")
30
30
31 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false)
31 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false)
32 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, true)
32 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, true)
33 end
33 end
34
34
35 def test_string_field_with_text_formatting_enabled_should_format_text
35 def test_string_field_with_text_formatting_enabled_should_format_text
36 field = IssueCustomField.new(:field_format => 'string', :text_formatting => 'full')
36 field = IssueCustomField.new(:field_format => 'string', :text_formatting => 'full')
37 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*")
37 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*")
38
38
39 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false)
39 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false)
40 assert_include "<strong>foo</strong>", field.format.formatted_custom_value(self, custom_value, true)
40 assert_include "<strong>foo</strong>", field.format.formatted_custom_value(self, custom_value, true)
41 end
41 end
42
42
43 def test_text_field_with_text_formatting_disabled_should_not_format_text
43 def test_text_field_with_text_formatting_disabled_should_not_format_text
44 field = IssueCustomField.new(:field_format => 'text')
44 field = IssueCustomField.new(:field_format => 'text')
45 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar")
45 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar")
46
46
47 assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false)
47 assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false)
48 assert_include "*foo*\n<br />bar", field.format.formatted_custom_value(self, custom_value, true)
48 assert_include "*foo*\n<br />bar", field.format.formatted_custom_value(self, custom_value, true)
49 end
49 end
50
50
51 def test_text_field_with_text_formatting_enabled_should_format_text
51 def test_text_field_with_text_formatting_enabled_should_format_text
52 field = IssueCustomField.new(:field_format => 'text', :text_formatting => 'full')
52 field = IssueCustomField.new(:field_format => 'text', :text_formatting => 'full')
53 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar")
53 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar")
54
54
55 assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false)
55 assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false)
56 assert_include "<strong>foo</strong>", field.format.formatted_custom_value(self, custom_value, true)
56 assert_include "<strong>foo</strong>", field.format.formatted_custom_value(self, custom_value, true)
57 end
57 end
58
58
59 def test_should_validate_url_pattern_with_safe_scheme
59 def test_should_validate_url_pattern_with_safe_scheme
60 field = IssueCustomField.new(:field_format => 'string', :name => 'URL', :url_pattern => 'http://foo/%value%')
60 field = IssueCustomField.new(:field_format => 'string', :name => 'URL', :url_pattern => 'http://foo/%value%')
61 assert_save field
61 assert_save field
62 end
62 end
63
63
64 def test_should_not_validate_url_pattern_with_unsafe_scheme
64 def test_should_not_validate_url_pattern_with_unsafe_scheme
65 field = IssueCustomField.new(:field_format => 'string', :name => 'URL', :url_pattern => 'foo://foo/%value%')
65 field = IssueCustomField.new(:field_format => 'string', :name => 'URL', :url_pattern => 'foo://foo/%value%')
66 assert !field.save
66 assert !field.save
67 assert_include "URL is invalid", field.errors.full_messages
67 assert_include "URL is invalid", field.errors.full_messages
68 end
68 end
69
69
70 def test_text_field_with_url_pattern_should_format_as_link
70 def test_text_field_with_url_pattern_should_format_as_link
71 field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%')
71 field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%')
72 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "bar")
72 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "bar")
73
73
74 assert_equal "bar", field.format.formatted_custom_value(self, custom_value, false)
74 assert_equal "bar", field.format.formatted_custom_value(self, custom_value, false)
75 assert_equal '<a href="http://foo/bar">bar</a>', field.format.formatted_custom_value(self, custom_value, true)
75 assert_equal '<a href="http://foo/bar">bar</a>', field.format.formatted_custom_value(self, custom_value, true)
76 end
76 end
77
78 def test_text_field_with_url_pattern_and_value_containing_a_space_should_format_as_link
79 field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%')
80 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "foo bar")
81
82 assert_equal "foo bar", field.format.formatted_custom_value(self, custom_value, false)
83 assert_equal '<a href="http://foo/foo%20bar">foo bar</a>', field.format.formatted_custom_value(self, custom_value, true)
84 end
77 end
85 end
General Comments 0
You need to be logged in to leave comments. Login now