@@ -0,0 +1,2 | |||||
|
1 | <p><%= f.select :default_value, [[]]+@custom_field.possible_values_options %></p> | |||
|
2 | <p><%= edit_tag_style_tag f %></p> |
@@ -0,0 +1,2 | |||||
|
1 | <p><%= f.text_field(:default_value, :size => 10) %></p> | |||
|
2 | <%= calendar_for('custom_field_default_value') %> |
@@ -0,0 +1,3 | |||||
|
1 | <%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %> | |||
|
2 | <p><%= f.text_field :url_pattern, :size => 50, :label => :field_url %></p> | |||
|
3 | <p><%= f.text_field(:default_value) %></p> |
@@ -0,0 +1,6 | |||||
|
1 | <p> | |||
|
2 | <%= f.text_area :possible_values, :value => @custom_field.possible_values.to_a.join("\n"), :rows => 15 %> | |||
|
3 | <em class="info"><%= l(:text_custom_field_possible_values_info) %></em> | |||
|
4 | </p> | |||
|
5 | <p><%= f.text_field(:default_value) %></p> | |||
|
6 | <p><%= edit_tag_style_tag f %></p> |
@@ -0,0 +1,2 | |||||
|
1 | <%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %> | |||
|
2 | <p><%= f.text_field(:default_value) %></p> |
@@ -0,0 +1,10 | |||||
|
1 | <p> | |||
|
2 | <label for="custom_field_min_length"><%=l(:label_min_max_length)%></label> | |||
|
3 | <%= f.text_field :min_length, :size => 5, :no_label => true %> - | |||
|
4 | <%= f.text_field :max_length, :size => 5, :no_label => true %> | |||
|
5 | <em class="info"><%= l(:text_min_max_length_info) %></em> | |||
|
6 | </p> | |||
|
7 | <p> | |||
|
8 | <%= f.text_field :regexp, :size => 50 %> | |||
|
9 | <em class="info"><%= l(:text_regexp_info) %></em> | |||
|
10 | </p> |
@@ -0,0 +1,3 | |||||
|
1 | <%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %> | |||
|
2 | <p><%= f.check_box :text_formatting, {:label => :setting_text_formatting}, 'full', '' %></p> | |||
|
3 | <p><%= f.text_field(:default_value) %></p> |
@@ -0,0 +1,3 | |||||
|
1 | <%= render :partial => 'custom_fields/formats/regexp', :locals => {:f => f, :custom_field => custom_field} %> | |||
|
2 | <p><%= f.check_box :text_formatting, {:label => :setting_text_formatting}, 'full', '' %></p> | |||
|
3 | <p><%= f.text_area(:default_value, :rows => 5) %></p> |
@@ -0,0 +1,24 | |||||
|
1 | <p> | |||
|
2 | <label><%= l(:label_role) %></label> | |||
|
3 | <label class="block"> | |||
|
4 | <%= radio_button_tag 'status', 1, custom_field.user_role.blank?, :id => 'custom_field_user_role_all', | |||
|
5 | :data => {:disables => '.custom_field_user_role input'} %> | |||
|
6 | <%= l(:label_all) %> | |||
|
7 | </label> | |||
|
8 | <label class="block"> | |||
|
9 | <%= radio_button_tag 'status', 0, custom_field.user_role.present?, :id => 'custom_field_user_role_only', | |||
|
10 | :data => {:enables => '.custom_field_user_role input'} %> | |||
|
11 | <%= l(:label_only) %>: | |||
|
12 | </label> | |||
|
13 | <% Role.givable.sorted.each do |role| %> | |||
|
14 | <label class="block custom_field_user_role" style="padding-left:2em;"> | |||
|
15 | <%= check_box_tag 'custom_field[user_role][]', | |||
|
16 | role.id, | |||
|
17 | custom_field.user_role.is_a?(Array) && custom_field.user_role.include?(role.id.to_s), | |||
|
18 | :id => nil %> | |||
|
19 | <%= role.name %> | |||
|
20 | </label> | |||
|
21 | <% end %> | |||
|
22 | <%= hidden_field_tag 'custom_field[user_role][]', '' %> | |||
|
23 | </p> | |||
|
24 | <p><%= edit_tag_style_tag f %></p> |
@@ -0,0 +1,24 | |||||
|
1 | <p> | |||
|
2 | <label><%= l(:field_status) %></label> | |||
|
3 | <label class="block"> | |||
|
4 | <%= radio_button_tag 'status', 1, custom_field.version_status.blank?, :id => 'custom_field_version_status_all', | |||
|
5 | :data => {:disables => '.custom_field_version_status input'} %> | |||
|
6 | <%= l(:label_all) %> | |||
|
7 | </label> | |||
|
8 | <label class="block"> | |||
|
9 | <%= radio_button_tag 'status', 0, custom_field.version_status.present?, :id => 'custom_field_version_status_only', | |||
|
10 | :data => {:enables => '.custom_field_version_status input'} %> | |||
|
11 | <%= l(:label_only) %>: | |||
|
12 | </label> | |||
|
13 | <% Version::VERSION_STATUSES.each do |status| %> | |||
|
14 | <label class="block custom_field_version_status" style="padding-left:2em;"> | |||
|
15 | <%= check_box_tag 'custom_field[version_status][]', | |||
|
16 | status, | |||
|
17 | custom_field.version_status.is_a?(Array) && custom_field.version_status.include?(status), | |||
|
18 | :id => nil %> | |||
|
19 | <%= l("version_status_#{status}") %> | |||
|
20 | </label> | |||
|
21 | <% end %> | |||
|
22 | <%= hidden_field_tag 'custom_field[version_status][]', '' %> | |||
|
23 | </p> | |||
|
24 | <p><%= edit_tag_style_tag f %></p> |
@@ -0,0 +1,9 | |||||
|
1 | class AddCustomFieldsFormatStore < ActiveRecord::Migration | |||
|
2 | def up | |||
|
3 | add_column :custom_fields, :format_store, :text | |||
|
4 | end | |||
|
5 | ||||
|
6 | def down | |||
|
7 | remove_column :custom_fields, :format_store | |||
|
8 | end | |||
|
9 | end |
@@ -0,0 +1,9 | |||||
|
1 | class AddCustomFieldsDescription < ActiveRecord::Migration | |||
|
2 | def up | |||
|
3 | add_column :custom_fields, :description, :text | |||
|
4 | end | |||
|
5 | ||||
|
6 | def down | |||
|
7 | remove_column :custom_fields, :description | |||
|
8 | end | |||
|
9 | end |
This diff has been collapsed as it changes many lines, (646 lines changed) Show them Hide them | |||||
@@ -0,0 +1,646 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | module Redmine | |||
|
19 | module FieldFormat | |||
|
20 | def self.add(name, klass) | |||
|
21 | all[name.to_s] = klass.instance | |||
|
22 | end | |||
|
23 | ||||
|
24 | def self.delete(name) | |||
|
25 | all.delete(name.to_s) | |||
|
26 | end | |||
|
27 | ||||
|
28 | def self.all | |||
|
29 | @formats ||= Hash.new(Base.instance) | |||
|
30 | end | |||
|
31 | ||||
|
32 | def self.available_formats | |||
|
33 | all.keys | |||
|
34 | end | |||
|
35 | ||||
|
36 | def self.find(name) | |||
|
37 | all[name.to_s] | |||
|
38 | end | |||
|
39 | ||||
|
40 | # Return an array of custom field formats which can be used in select_tag | |||
|
41 | def self.as_select(class_name=nil) | |||
|
42 | formats = all.values | |||
|
43 | formats.select! do |format| | |||
|
44 | format.class.customized_class_names.nil? || format.class.customized_class_names.include?(class_name) | |||
|
45 | end | |||
|
46 | formats.map {|format| [::I18n.t(format.label), format.name] }.sort_by(&:first) | |||
|
47 | end | |||
|
48 | ||||
|
49 | class Base | |||
|
50 | include Singleton | |||
|
51 | include Redmine::I18n | |||
|
52 | include ERB::Util | |||
|
53 | ||||
|
54 | class_attribute :format_name | |||
|
55 | self.format_name = nil | |||
|
56 | ||||
|
57 | # Set this to true if the format supports multiple values | |||
|
58 | class_attribute :multiple_supported | |||
|
59 | self.multiple_supported = false | |||
|
60 | ||||
|
61 | # Set this to true if the format supports textual search on custom values | |||
|
62 | class_attribute :searchable_supported | |||
|
63 | self.searchable_supported = false | |||
|
64 | ||||
|
65 | # Restricts the classes that the custom field can be added to | |||
|
66 | # Set to nil for no restrictions | |||
|
67 | class_attribute :customized_class_names | |||
|
68 | self.customized_class_names = nil | |||
|
69 | ||||
|
70 | # Name of the partial for editing the custom field | |||
|
71 | class_attribute :form_partial | |||
|
72 | self.form_partial = nil | |||
|
73 | ||||
|
74 | def self.add(name) | |||
|
75 | self.format_name = name | |||
|
76 | Redmine::FieldFormat.add(name, self) | |||
|
77 | end | |||
|
78 | private_class_method :add | |||
|
79 | ||||
|
80 | def self.field_attributes(*args) | |||
|
81 | CustomField.store_accessor :format_store, *args | |||
|
82 | end | |||
|
83 | ||||
|
84 | def name | |||
|
85 | self.class.format_name | |||
|
86 | end | |||
|
87 | ||||
|
88 | def label | |||
|
89 | "label_#{name}" | |||
|
90 | end | |||
|
91 | ||||
|
92 | def cast_custom_value(custom_value) | |||
|
93 | cast_value(custom_value.custom_field, custom_value.value, custom_value.customized) | |||
|
94 | end | |||
|
95 | ||||
|
96 | def cast_value(custom_field, value, customized=nil) | |||
|
97 | if value.blank? | |||
|
98 | nil | |||
|
99 | elsif value.is_a?(Array) | |||
|
100 | value.map do |v| | |||
|
101 | cast_single_value(custom_field, v, customized) | |||
|
102 | end.sort | |||
|
103 | else | |||
|
104 | cast_single_value(custom_field, value, customized) | |||
|
105 | end | |||
|
106 | end | |||
|
107 | ||||
|
108 | def cast_single_value(custom_field, value, customized=nil) | |||
|
109 | value.to_s | |||
|
110 | end | |||
|
111 | ||||
|
112 | def target_class | |||
|
113 | nil | |||
|
114 | end | |||
|
115 | ||||
|
116 | def possible_custom_value_options(custom_value) | |||
|
117 | possible_values_options(custom_value.custom_field, custom_value.customized) | |||
|
118 | end | |||
|
119 | ||||
|
120 | def possible_values_options(custom_field, object=nil) | |||
|
121 | custom_field.possible_values | |||
|
122 | end | |||
|
123 | ||||
|
124 | # Returns the validation errors for custom_field | |||
|
125 | # Should return an empty array if custom_field is valid | |||
|
126 | def validate_custom_field(custom_field) | |||
|
127 | [] | |||
|
128 | end | |||
|
129 | ||||
|
130 | # Returns the validation error messages for custom_value | |||
|
131 | # Should return an empty array if custom_value is valid | |||
|
132 | def validate_custom_value(custom_value) | |||
|
133 | errors = Array.wrap(custom_value.value).reject(&:blank?).map do |value| | |||
|
134 | validate_single_value(custom_value.custom_field, value, custom_value.customized) | |||
|
135 | end | |||
|
136 | errors.flatten.uniq | |||
|
137 | end | |||
|
138 | ||||
|
139 | def validate_single_value(custom_field, value, customized=nil) | |||
|
140 | [] | |||
|
141 | end | |||
|
142 | ||||
|
143 | def formatted_custom_value(view, custom_value, html=false) | |||
|
144 | formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html) | |||
|
145 | end | |||
|
146 | ||||
|
147 | def formatted_value(view, custom_field, value, customized=nil, html=false) | |||
|
148 | cast_value(custom_field, value, customized) | |||
|
149 | end | |||
|
150 | ||||
|
151 | def edit_tag(view, tag_id, tag_name, custom_value, options={}) | |||
|
152 | view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id)) | |||
|
153 | end | |||
|
154 | ||||
|
155 | def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={}) | |||
|
156 | view.text_field_tag(tag_name, value, options.merge(:id => tag_id)) + | |||
|
157 | bulk_clear_tag(view, tag_id, tag_name, custom_field, value) | |||
|
158 | end | |||
|
159 | ||||
|
160 | def bulk_clear_tag(view, tag_id, tag_name, custom_field, value) | |||
|
161 | if custom_field.is_required? | |||
|
162 | ''.html_safe | |||
|
163 | else | |||
|
164 | view.content_tag('label', | |||
|
165 | view.check_box_tag(tag_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{tag_id}"}) + l(:button_clear), | |||
|
166 | :class => 'inline' | |||
|
167 | ) | |||
|
168 | end | |||
|
169 | end | |||
|
170 | protected :bulk_clear_tag | |||
|
171 | ||||
|
172 | def query_filter_options(custom_field, query) | |||
|
173 | {:type => :string} | |||
|
174 | end | |||
|
175 | ||||
|
176 | def before_custom_field_save(custom_field) | |||
|
177 | end | |||
|
178 | ||||
|
179 | # Returns a ORDER BY clause that can used to sort customized | |||
|
180 | # objects by their value of the custom field. | |||
|
181 | # Returns nil if the custom field can not be used for sorting. | |||
|
182 | def order_statement(custom_field) | |||
|
183 | # COALESCE is here to make sure that blank and NULL values are sorted equally | |||
|
184 | "COALESCE(#{join_alias custom_field}.value, '')" | |||
|
185 | end | |||
|
186 | ||||
|
187 | # Returns a GROUP BY clause that can used to group by custom value | |||
|
188 | # Returns nil if the custom field can not be used for grouping. | |||
|
189 | def group_statement(custom_field) | |||
|
190 | nil | |||
|
191 | end | |||
|
192 | ||||
|
193 | # Returns a JOIN clause that is added to the query when sorting by custom values | |||
|
194 | def join_for_order_statement(custom_field) | |||
|
195 | alias_name = join_alias(custom_field) | |||
|
196 | ||||
|
197 | "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" + | |||
|
198 | " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" + | |||
|
199 | " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" + | |||
|
200 | " AND #{alias_name}.custom_field_id = #{custom_field.id}" + | |||
|
201 | " AND (#{custom_field.visibility_by_project_condition})" + | |||
|
202 | " AND #{alias_name}.value <> ''" + | |||
|
203 | " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" + | |||
|
204 | " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" + | |||
|
205 | " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" + | |||
|
206 | " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)" | |||
|
207 | end | |||
|
208 | ||||
|
209 | def join_alias(custom_field) | |||
|
210 | "cf_#{custom_field.id}" | |||
|
211 | end | |||
|
212 | protected :join_alias | |||
|
213 | end | |||
|
214 | ||||
|
215 | class Unbounded < Base | |||
|
216 | def validate_single_value(custom_field, value, customized=nil) | |||
|
217 | errs = super | |||
|
218 | if value.present? | |||
|
219 | unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp) | |||
|
220 | errs << ::I18n.t('activerecord.errors.messages.invalid') | |||
|
221 | end | |||
|
222 | if custom_field.min_length > 0 and value.length < custom_field.min_length | |||
|
223 | errs << ::I18n.t('activerecord.errors.messages.too_short', :count => custom_field.min_length) | |||
|
224 | end | |||
|
225 | if custom_field.max_length > 0 and value.length > custom_field.max_length | |||
|
226 | errs << ::I18n.t('activerecord.errors.messages.too_long', :count => custom_field.max_length) | |||
|
227 | end | |||
|
228 | end | |||
|
229 | errs | |||
|
230 | end | |||
|
231 | end | |||
|
232 | ||||
|
233 | class StringFormat < Unbounded | |||
|
234 | add 'string' | |||
|
235 | self.searchable_supported = true | |||
|
236 | self.form_partial = 'custom_fields/formats/string' | |||
|
237 | field_attributes :text_formatting | |||
|
238 | ||||
|
239 | def formatted_value(view, custom_field, value, customized=nil, html=false) | |||
|
240 | if html && custom_field.text_formatting == 'full' | |||
|
241 | view.textilizable(value, :object => customized) | |||
|
242 | else | |||
|
243 | value.to_s | |||
|
244 | end | |||
|
245 | end | |||
|
246 | end | |||
|
247 | ||||
|
248 | class TextFormat < Unbounded | |||
|
249 | add 'text' | |||
|
250 | self.searchable_supported = true | |||
|
251 | self.form_partial = 'custom_fields/formats/text' | |||
|
252 | ||||
|
253 | def formatted_value(view, custom_field, value, customized=nil, html=false) | |||
|
254 | if html | |||
|
255 | if custom_field.text_formatting == 'full' | |||
|
256 | view.textilizable(value, :object => customized) | |||
|
257 | else | |||
|
258 | view.simple_format(html_escape(value)) | |||
|
259 | end | |||
|
260 | else | |||
|
261 | value.to_s | |||
|
262 | end | |||
|
263 | end | |||
|
264 | ||||
|
265 | def edit_tag(view, tag_id, tag_name, custom_value, options={}) | |||
|
266 | view.text_area_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :rows => 3)) | |||
|
267 | end | |||
|
268 | ||||
|
269 | def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={}) | |||
|
270 | view.text_area_tag(tag_name, value, options.merge(:id => tag_id, :rows => 3)) + | |||
|
271 | '<br />'.html_safe + | |||
|
272 | bulk_clear_tag(view, tag_id, tag_name, custom_field, value) | |||
|
273 | end | |||
|
274 | ||||
|
275 | def query_filter_options(custom_field, query) | |||
|
276 | {:type => :text} | |||
|
277 | end | |||
|
278 | end | |||
|
279 | ||||
|
280 | class LinkFormat < StringFormat | |||
|
281 | add 'link' | |||
|
282 | self.searchable_supported = false | |||
|
283 | self.form_partial = 'custom_fields/formats/link' | |||
|
284 | field_attributes :url_pattern | |||
|
285 | ||||
|
286 | def formatted_value(view, custom_field, value, customized=nil, html=false) | |||
|
287 | if html | |||
|
288 | if custom_field.url_pattern.present? | |||
|
289 | url = custom_field.url_pattern.to_s.dup | |||
|
290 | url.gsub!('%value%') {value.to_s} | |||
|
291 | url.gsub!('%id%') {customized.id.to_s} | |||
|
292 | url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s} | |||
|
293 | if custom_field.regexp.present? | |||
|
294 | url.gsub!(%r{%m(\d+)%}) do | |||
|
295 | m = $1.to_i | |||
|
296 | matches ||= value.to_s.match(Regexp.new(custom_field.regexp)) | |||
|
297 | matches[m].to_s if matches | |||
|
298 | end | |||
|
299 | end | |||
|
300 | else | |||
|
301 | url = value.to_s | |||
|
302 | unless url =~ %r{\A[a-z]+://}i | |||
|
303 | # no protocol found, use http by default | |||
|
304 | url = "http://" + url | |||
|
305 | end | |||
|
306 | end | |||
|
307 | view.link_to value.to_s, url | |||
|
308 | else | |||
|
309 | value.to_s | |||
|
310 | end | |||
|
311 | end | |||
|
312 | end | |||
|
313 | ||||
|
314 | class Numeric < Unbounded | |||
|
315 | self.form_partial = 'custom_fields/formats/numeric' | |||
|
316 | ||||
|
317 | def order_statement(custom_field) | |||
|
318 | # Make the database cast values into numeric | |||
|
319 | # Postgresql will raise an error if a value can not be casted! | |||
|
320 | # CustomValue validations should ensure that it doesn't occur | |||
|
321 | "CAST(CASE #{join_alias custom_field}.value WHEN '' THEN '0' ELSE #{join_alias custom_field}.value END AS decimal(30,3))" | |||
|
322 | end | |||
|
323 | end | |||
|
324 | ||||
|
325 | class IntFormat < Numeric | |||
|
326 | add 'int' | |||
|
327 | ||||
|
328 | def label | |||
|
329 | "label_integer" | |||
|
330 | end | |||
|
331 | ||||
|
332 | def cast_single_value(custom_field, value, customized=nil) | |||
|
333 | value.to_i | |||
|
334 | end | |||
|
335 | ||||
|
336 | def validate_single_value(custom_field, value, customized=nil) | |||
|
337 | errs = super | |||
|
338 | errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/ | |||
|
339 | errs | |||
|
340 | end | |||
|
341 | ||||
|
342 | def query_filter_options(custom_field, query) | |||
|
343 | {:type => :integer} | |||
|
344 | end | |||
|
345 | ||||
|
346 | def group_statement(custom_field) | |||
|
347 | order_statement(custom_field) | |||
|
348 | end | |||
|
349 | end | |||
|
350 | ||||
|
351 | class FloatFormat < Numeric | |||
|
352 | add 'float' | |||
|
353 | ||||
|
354 | def cast_single_value(custom_field, value, customized=nil) | |||
|
355 | value.to_f | |||
|
356 | end | |||
|
357 | ||||
|
358 | def validate_single_value(custom_field, value, customized=nil) | |||
|
359 | errs = super | |||
|
360 | errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil) | |||
|
361 | errs | |||
|
362 | end | |||
|
363 | ||||
|
364 | def query_filter_options(custom_field, query) | |||
|
365 | {:type => :float} | |||
|
366 | end | |||
|
367 | end | |||
|
368 | ||||
|
369 | class DateFormat < Unbounded | |||
|
370 | add 'date' | |||
|
371 | self.form_partial = 'custom_fields/formats/date' | |||
|
372 | ||||
|
373 | def cast_single_value(custom_field, value, customized=nil) | |||
|
374 | value.to_date rescue nil | |||
|
375 | end | |||
|
376 | ||||
|
377 | def validate_single_value(custom_field, value, customized=nil) | |||
|
378 | if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false) | |||
|
379 | [] | |||
|
380 | else | |||
|
381 | [::I18n.t('activerecord.errors.messages.not_a_date')] | |||
|
382 | end | |||
|
383 | end | |||
|
384 | ||||
|
385 | def edit_tag(view, tag_id, tag_name, custom_value, options={}) | |||
|
386 | view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10)) + | |||
|
387 | view.calendar_for(tag_id) | |||
|
388 | end | |||
|
389 | ||||
|
390 | def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={}) | |||
|
391 | view.text_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10)) + | |||
|
392 | view.calendar_for(tag_id) + | |||
|
393 | bulk_clear_tag(view, tag_id, tag_name, custom_field, value) | |||
|
394 | end | |||
|
395 | ||||
|
396 | def query_filter_options(custom_field, query) | |||
|
397 | {:type => :date} | |||
|
398 | end | |||
|
399 | ||||
|
400 | def group_statement(custom_field) | |||
|
401 | order_statement(custom_field) | |||
|
402 | end | |||
|
403 | end | |||
|
404 | ||||
|
405 | class List < Base | |||
|
406 | self.multiple_supported = true | |||
|
407 | field_attributes :edit_tag_style | |||
|
408 | ||||
|
409 | def edit_tag(view, tag_id, tag_name, custom_value, options={}) | |||
|
410 | if custom_value.custom_field.edit_tag_style == 'check_box' | |||
|
411 | check_box_edit_tag(view, tag_id, tag_name, custom_value, options) | |||
|
412 | else | |||
|
413 | select_edit_tag(view, tag_id, tag_name, custom_value, options) | |||
|
414 | end | |||
|
415 | end | |||
|
416 | ||||
|
417 | def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={}) | |||
|
418 | opts = [] | |||
|
419 | opts << [l(:label_no_change_option), ''] unless custom_field.multiple? | |||
|
420 | opts << [l(:label_none), '__none__'] unless custom_field.is_required? | |||
|
421 | opts += possible_values_options(custom_field, objects) | |||
|
422 | view.select_tag(tag_name, view.options_for_select(opts, value), options.merge(:multiple => custom_field.multiple?)) | |||
|
423 | end | |||
|
424 | ||||
|
425 | def query_filter_options(custom_field, query) | |||
|
426 | {:type => :list_optional, :values => possible_values_options(custom_field, query.project)} | |||
|
427 | end | |||
|
428 | ||||
|
429 | protected | |||
|
430 | ||||
|
431 | # Renders the edit tag as a select tag | |||
|
432 | def select_edit_tag(view, tag_id, tag_name, custom_value, options={}) | |||
|
433 | blank_option = ''.html_safe | |||
|
434 | unless custom_value.custom_field.multiple? | |||
|
435 | if custom_value.custom_field.is_required? | |||
|
436 | unless custom_value.custom_field.default_value.present? | |||
|
437 | blank_option = view.content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '') | |||
|
438 | end | |||
|
439 | else | |||
|
440 | blank_option = view.content_tag('option', ' '.html_safe, :value => '') | |||
|
441 | end | |||
|
442 | end | |||
|
443 | options_tags = blank_option + view.options_for_select(possible_custom_value_options(custom_value), custom_value.value) | |||
|
444 | s = view.select_tag(tag_name, options_tags, options.merge(:id => tag_id, :multiple => custom_value.custom_field.multiple?)) | |||
|
445 | if custom_value.custom_field.multiple? | |||
|
446 | s << view.hidden_field_tag(tag_name, '') | |||
|
447 | end | |||
|
448 | s | |||
|
449 | end | |||
|
450 | ||||
|
451 | # Renders the edit tag as check box or radio tags | |||
|
452 | def check_box_edit_tag(view, tag_id, tag_name, custom_value, options={}) | |||
|
453 | opts = [] | |||
|
454 | unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required? | |||
|
455 | opts << ["(#{l(:label_none)})", ''] | |||
|
456 | end | |||
|
457 | opts += possible_custom_value_options(custom_value) | |||
|
458 | s = ''.html_safe | |||
|
459 | tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag | |||
|
460 | opts.each do |label, value| | |||
|
461 | value ||= label | |||
|
462 | checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value | |||
|
463 | tag = view.send(tag_method, tag_name, value, checked, :id => tag_id) | |||
|
464 | # set the id on the first tag only | |||
|
465 | tag_id = nil | |||
|
466 | s << view.content_tag('label', tag + ' ' + label) | |||
|
467 | end | |||
|
468 | css = "#{options[:class]} check_box_group" | |||
|
469 | view.content_tag('span', s, options.merge(:class => css)) | |||
|
470 | end | |||
|
471 | end | |||
|
472 | ||||
|
473 | class ListFormat < List | |||
|
474 | add 'list' | |||
|
475 | self.searchable_supported = true | |||
|
476 | self.form_partial = 'custom_fields/formats/list' | |||
|
477 | ||||
|
478 | def possible_custom_value_options(custom_value) | |||
|
479 | options = super | |||
|
480 | missing = [custom_value.value].flatten.reject(&:blank?) - options | |||
|
481 | if missing.any? | |||
|
482 | options += missing | |||
|
483 | end | |||
|
484 | options | |||
|
485 | end | |||
|
486 | ||||
|
487 | def validate_custom_field(custom_field) | |||
|
488 | errors = [] | |||
|
489 | errors << [:possible_values, :blank] if custom_field.possible_values.blank? | |||
|
490 | errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array | |||
|
491 | errors | |||
|
492 | end | |||
|
493 | ||||
|
494 | def validate_custom_value(custom_value) | |||
|
495 | invalid_values = Array.wrap(custom_value.value) - Array.wrap(custom_value.value_was) - custom_value.custom_field.possible_values | |||
|
496 | if invalid_values.select(&:present?).any? | |||
|
497 | [::I18n.t('activerecord.errors.messages.inclusion')] | |||
|
498 | else | |||
|
499 | [] | |||
|
500 | end | |||
|
501 | end | |||
|
502 | ||||
|
503 | def group_statement(custom_field) | |||
|
504 | order_statement(custom_field) | |||
|
505 | end | |||
|
506 | end | |||
|
507 | ||||
|
508 | class BoolFormat < List | |||
|
509 | add 'bool' | |||
|
510 | self.multiple_supported = false | |||
|
511 | self.form_partial = 'custom_fields/formats/bool' | |||
|
512 | ||||
|
513 | def label | |||
|
514 | "label_boolean" | |||
|
515 | end | |||
|
516 | ||||
|
517 | def cast_single_value(custom_field, value, customized=nil) | |||
|
518 | value == '1' ? true : false | |||
|
519 | end | |||
|
520 | ||||
|
521 | def possible_values_options(custom_field, object=nil) | |||
|
522 | [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']] | |||
|
523 | end | |||
|
524 | ||||
|
525 | def group_statement(custom_field) | |||
|
526 | order_statement(custom_field) | |||
|
527 | end | |||
|
528 | end | |||
|
529 | ||||
|
530 | class RecordList < List | |||
|
531 | self.customized_class_names = %w(Issue TimeEntry Version Project) | |||
|
532 | ||||
|
533 | def cast_single_value(custom_field, value, customized=nil) | |||
|
534 | target_class.find_by_id(value.to_i) if value.present? | |||
|
535 | end | |||
|
536 | ||||
|
537 | def target_class | |||
|
538 | @target_class ||= self.class.name[/^(.*::)?(.+)Format$/, 2].constantize rescue nil | |||
|
539 | end | |||
|
540 | ||||
|
541 | def possible_custom_value_options(custom_value) | |||
|
542 | options = possible_values_options(custom_value.custom_field, custom_value.customized) | |||
|
543 | missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last) | |||
|
544 | if missing.any? | |||
|
545 | options += target_class.find_all_by_id(missing.map(&:to_i)).map {|o| [o.to_s, o.id.to_s]} | |||
|
546 | options.sort_by!(&:first) | |||
|
547 | end | |||
|
548 | options | |||
|
549 | end | |||
|
550 | ||||
|
551 | def order_statement(custom_field) | |||
|
552 | if target_class.respond_to?(:fields_for_order_statement) | |||
|
553 | target_class.fields_for_order_statement(value_join_alias(custom_field)) | |||
|
554 | end | |||
|
555 | end | |||
|
556 | ||||
|
557 | def group_statement(custom_field) | |||
|
558 | "COALESCE(#{join_alias custom_field}.value, '')" | |||
|
559 | end | |||
|
560 | ||||
|
561 | def join_for_order_statement(custom_field) | |||
|
562 | alias_name = join_alias(custom_field) | |||
|
563 | ||||
|
564 | "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" + | |||
|
565 | " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" + | |||
|
566 | " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" + | |||
|
567 | " AND #{alias_name}.custom_field_id = #{custom_field.id}" + | |||
|
568 | " AND (#{custom_field.visibility_by_project_condition})" + | |||
|
569 | " AND #{alias_name}.value <> ''" + | |||
|
570 | " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" + | |||
|
571 | " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" + | |||
|
572 | " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" + | |||
|
573 | " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)" + | |||
|
574 | " LEFT OUTER JOIN #{target_class.table_name} #{value_join_alias custom_field}" + | |||
|
575 | " ON CAST(CASE #{alias_name}.value WHEN '' THEN '0' ELSE #{alias_name}.value END AS decimal(30,0)) = #{value_join_alias custom_field}.id" | |||
|
576 | end | |||
|
577 | ||||
|
578 | def value_join_alias(custom_field) | |||
|
579 | join_alias(custom_field) + "_" + custom_field.field_format | |||
|
580 | end | |||
|
581 | protected :value_join_alias | |||
|
582 | end | |||
|
583 | ||||
|
584 | class UserFormat < RecordList | |||
|
585 | add 'user' | |||
|
586 | self.form_partial = 'custom_fields/formats/user' | |||
|
587 | field_attributes :user_role | |||
|
588 | ||||
|
589 | def possible_values_options(custom_field, object=nil) | |||
|
590 | if object.is_a?(Array) | |||
|
591 | projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq | |||
|
592 | projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || [] | |||
|
593 | elsif object.respond_to?(:project) && object.project | |||
|
594 | scope = object.project.users | |||
|
595 | if custom_field.user_role.is_a?(Array) | |||
|
596 | role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i) | |||
|
597 | if role_ids.any? | |||
|
598 | scope = scope.where("#{Member.table_name}.id IN (SELECT DISTINCT member_id FROM #{MemberRole.table_name} WHERE role_id IN (?))", role_ids) | |||
|
599 | end | |||
|
600 | end | |||
|
601 | scope.sorted.collect {|u| [u.to_s, u.id.to_s]} | |||
|
602 | else | |||
|
603 | [] | |||
|
604 | end | |||
|
605 | end | |||
|
606 | ||||
|
607 | def before_custom_field_save(custom_field) | |||
|
608 | super | |||
|
609 | if custom_field.user_role.is_a?(Array) | |||
|
610 | custom_field.user_role.map!(&:to_s).reject!(&:blank?) | |||
|
611 | end | |||
|
612 | end | |||
|
613 | end | |||
|
614 | ||||
|
615 | class VersionFormat < RecordList | |||
|
616 | add 'version' | |||
|
617 | self.form_partial = 'custom_fields/formats/version' | |||
|
618 | field_attributes :version_status | |||
|
619 | ||||
|
620 | def possible_values_options(custom_field, object=nil) | |||
|
621 | if object.is_a?(Array) | |||
|
622 | projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq | |||
|
623 | projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || [] | |||
|
624 | elsif object.respond_to?(:project) && object.project | |||
|
625 | scope = object.project.shared_versions | |||
|
626 | if custom_field.version_status.is_a?(Array) | |||
|
627 | statuses = custom_field.version_status.map(&:to_s).reject(&:blank?) | |||
|
628 | if statuses.any? | |||
|
629 | scope = scope.where(:status => statuses.map(&:to_s)) | |||
|
630 | end | |||
|
631 | end | |||
|
632 | scope.sort.collect {|u| [u.to_s, u.id.to_s]} | |||
|
633 | else | |||
|
634 | [] | |||
|
635 | end | |||
|
636 | end | |||
|
637 | ||||
|
638 | def before_custom_field_save(custom_field) | |||
|
639 | super | |||
|
640 | if custom_field.version_status.is_a?(Array) | |||
|
641 | custom_field.version_status.map!(&:to_s).reject!(&:blank?) | |||
|
642 | end | |||
|
643 | end | |||
|
644 | end | |||
|
645 | end | |||
|
646 | end |
@@ -0,0 +1,55 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | require File.expand_path('../../../../../test_helper', __FILE__) | |||
|
19 | require 'redmine/field_format' | |||
|
20 | ||||
|
21 | class Redmine::FieldFormatTest < ActionView::TestCase | |||
|
22 | include ApplicationHelper | |||
|
23 | ||||
|
24 | def test_string_field_with_text_formatting_disabled_should_not_format_text | |||
|
25 | field = IssueCustomField.new(:field_format => 'string') | |||
|
26 | custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*") | |||
|
27 | ||||
|
28 | assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false) | |||
|
29 | assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, true) | |||
|
30 | end | |||
|
31 | ||||
|
32 | def test_string_field_with_text_formatting_enabled_should_format_text | |||
|
33 | field = IssueCustomField.new(:field_format => 'string', :text_formatting => 'full') | |||
|
34 | custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*") | |||
|
35 | ||||
|
36 | assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false) | |||
|
37 | assert_include "<strong>foo</strong>", field.format.formatted_custom_value(self, custom_value, true) | |||
|
38 | end | |||
|
39 | ||||
|
40 | def test_text_field_with_text_formatting_disabled_should_not_format_text | |||
|
41 | field = IssueCustomField.new(:field_format => 'text') | |||
|
42 | custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar") | |||
|
43 | ||||
|
44 | assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false) | |||
|
45 | assert_include "*foo*\n<br />bar", field.format.formatted_custom_value(self, custom_value, true) | |||
|
46 | end | |||
|
47 | ||||
|
48 | def test_text_field_with_text_formatting_enabled_should_format_text | |||
|
49 | field = IssueCustomField.new(:field_format => 'text', :text_formatting => 'full') | |||
|
50 | custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar") | |||
|
51 | ||||
|
52 | assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false) | |||
|
53 | assert_include "<strong>foo</strong>", field.format.formatted_custom_value(self, custom_value, true) | |||
|
54 | end | |||
|
55 | end |
@@ -0,0 +1,79 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | require File.expand_path('../../../../../test_helper', __FILE__) | |||
|
19 | require 'redmine/field_format' | |||
|
20 | ||||
|
21 | class Redmine::LinkFieldFormatTest < ActionView::TestCase | |||
|
22 | include ApplicationHelper | |||
|
23 | ||||
|
24 | def test_link_field_should_substitute_value | |||
|
25 | field = IssueCustomField.new(:field_format => 'link', :url_pattern => 'http://foo/%value%') | |||
|
26 | custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "bar") | |||
|
27 | ||||
|
28 | assert_equal "bar", field.format.formatted_custom_value(self, custom_value, false) | |||
|
29 | assert_equal '<a href="http://foo/bar">bar</a>', field.format.formatted_custom_value(self, custom_value, true) | |||
|
30 | end | |||
|
31 | ||||
|
32 | def test_link_field_should_substitue_object_id_in_url | |||
|
33 | object = Issue.new | |||
|
34 | object.stubs(:id).returns(10) | |||
|
35 | ||||
|
36 | field = IssueCustomField.new(:field_format => 'link', :url_pattern => 'http://foo/%id%') | |||
|
37 | custom_value = CustomValue.new(:custom_field => field, :customized => object, :value => "bar") | |||
|
38 | ||||
|
39 | assert_equal "bar", field.format.formatted_custom_value(self, custom_value, false) | |||
|
40 | assert_equal '<a href="http://foo/10">bar</a>', field.format.formatted_custom_value(self, custom_value, true) | |||
|
41 | end | |||
|
42 | ||||
|
43 | def test_link_field_should_substitue_project_id_in_url | |||
|
44 | project = Project.new | |||
|
45 | project.stubs(:id).returns(52) | |||
|
46 | object = Issue.new | |||
|
47 | object.stubs(:project).returns(project) | |||
|
48 | ||||
|
49 | field = IssueCustomField.new(:field_format => 'link', :url_pattern => 'http://foo/%project_id%') | |||
|
50 | custom_value = CustomValue.new(:custom_field => field, :customized => object, :value => "bar") | |||
|
51 | ||||
|
52 | assert_equal "bar", field.format.formatted_custom_value(self, custom_value, false) | |||
|
53 | assert_equal '<a href="http://foo/52">bar</a>', field.format.formatted_custom_value(self, custom_value, true) | |||
|
54 | end | |||
|
55 | ||||
|
56 | def test_link_field_should_substitue_regexp_groups | |||
|
57 | field = IssueCustomField.new(:field_format => 'link', :regexp => /^(.+)-(.+)$/, :url_pattern => 'http://foo/%m2%/%m1%') | |||
|
58 | custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "56-142") | |||
|
59 | ||||
|
60 | assert_equal "56-142", field.format.formatted_custom_value(self, custom_value, false) | |||
|
61 | assert_equal '<a href="http://foo/142/56">56-142</a>', field.format.formatted_custom_value(self, custom_value, true) | |||
|
62 | end | |||
|
63 | ||||
|
64 | def test_link_field_without_url_pattern_should_link_to_value | |||
|
65 | field = IssueCustomField.new(:field_format => 'link') | |||
|
66 | custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "http://foo/bar") | |||
|
67 | ||||
|
68 | assert_equal "http://foo/bar", field.format.formatted_custom_value(self, custom_value, false) | |||
|
69 | assert_equal '<a href="http://foo/bar">http://foo/bar</a>', field.format.formatted_custom_value(self, custom_value, true) | |||
|
70 | end | |||
|
71 | ||||
|
72 | def test_link_field_without_url_pattern_should_link_to_value_with_http_by_default | |||
|
73 | field = IssueCustomField.new(:field_format => 'link') | |||
|
74 | custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "foo.bar") | |||
|
75 | ||||
|
76 | assert_equal "foo.bar", field.format.formatted_custom_value(self, custom_value, false) | |||
|
77 | assert_equal '<a href="http://foo.bar">foo.bar</a>', field.format.formatted_custom_value(self, custom_value, true) | |||
|
78 | end | |||
|
79 | end |
@@ -0,0 +1,135 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | require File.expand_path('../../../../../test_helper', __FILE__) | |||
|
19 | require 'redmine/field_format' | |||
|
20 | ||||
|
21 | class Redmine::ListFieldFormatTest < ActionView::TestCase | |||
|
22 | include ApplicationHelper | |||
|
23 | include Redmine::I18n | |||
|
24 | ||||
|
25 | def setup | |||
|
26 | set_language_if_valid 'en' | |||
|
27 | end | |||
|
28 | ||||
|
29 | def test_possible_existing_value_should_be_valid | |||
|
30 | field = GroupCustomField.create!(:name => 'List', :field_format => 'list', :possible_values => ['Foo', 'Bar']) | |||
|
31 | group = Group.new(:name => 'Group') | |||
|
32 | group.custom_field_values = {field.id => 'Baz'} | |||
|
33 | assert group.save(:validate => false) | |||
|
34 | ||||
|
35 | group = Group.order('id DESC').first | |||
|
36 | assert_equal ['Foo', 'Bar', 'Baz'], field.possible_custom_value_options(group.custom_value_for(field)) | |||
|
37 | assert group.valid? | |||
|
38 | end | |||
|
39 | ||||
|
40 | def test_edit_tag_should_have_id_and_name | |||
|
41 | field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false) | |||
|
42 | value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new) | |||
|
43 | ||||
|
44 | tag = field.format.edit_tag(self, 'abc', 'xyz', value) | |||
|
45 | assert_select_in tag, 'select[id=abc][name=xyz]' | |||
|
46 | end | |||
|
47 | ||||
|
48 | def test_edit_tag_should_contain_possible_values | |||
|
49 | field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false) | |||
|
50 | value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new) | |||
|
51 | ||||
|
52 | tag = field.format.edit_tag(self, 'id', 'name', value) | |||
|
53 | assert_select_in tag, 'select' do | |||
|
54 | assert_select 'option', 3 | |||
|
55 | assert_select 'option[value=]' | |||
|
56 | assert_select 'option[value=Foo]', :text => 'Foo' | |||
|
57 | assert_select 'option[value=Bar]', :text => 'Bar' | |||
|
58 | end | |||
|
59 | end | |||
|
60 | ||||
|
61 | def test_edit_tag_should_select_current_value | |||
|
62 | field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false) | |||
|
63 | value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new, :value => 'Bar') | |||
|
64 | ||||
|
65 | tag = field.format.edit_tag(self, 'id', 'name', value) | |||
|
66 | assert_select_in tag, 'select' do | |||
|
67 | assert_select 'option[selected=selected]', 1 | |||
|
68 | assert_select 'option[value=Bar][selected=selected]', :text => 'Bar' | |||
|
69 | end | |||
|
70 | end | |||
|
71 | ||||
|
72 | def test_edit_tag_with_multiple_should_select_current_values | |||
|
73 | field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz'], :is_required => false, | |||
|
74 | :multiple => true) | |||
|
75 | value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new, :value => ['Bar', 'Baz']) | |||
|
76 | ||||
|
77 | tag = field.format.edit_tag(self, 'id', 'name', value) | |||
|
78 | assert_select_in tag, 'select[multiple=multiple]' do | |||
|
79 | assert_select 'option[selected=selected]', 2 | |||
|
80 | assert_select 'option[value=Bar][selected=selected]', :text => 'Bar' | |||
|
81 | assert_select 'option[value=Baz][selected=selected]', :text => 'Baz' | |||
|
82 | end | |||
|
83 | end | |||
|
84 | ||||
|
85 | def test_edit_tag_with_check_box_style_should_contain_possible_values | |||
|
86 | field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false, | |||
|
87 | :edit_tag_style => 'check_box') | |||
|
88 | value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new) | |||
|
89 | ||||
|
90 | tag = field.format.edit_tag(self, 'id', 'name', value) | |||
|
91 | assert_select_in tag, 'span' do | |||
|
92 | assert_select 'input[type=radio]', 3 | |||
|
93 | assert_select 'label', :text => '(none)' do | |||
|
94 | assert_select 'input[value=]' | |||
|
95 | end | |||
|
96 | assert_select 'label', :text => 'Foo' do | |||
|
97 | assert_select 'input[value=Foo]' | |||
|
98 | end | |||
|
99 | assert_select 'label', :text => 'Bar' do | |||
|
100 | assert_select 'input[value=Bar]' | |||
|
101 | end | |||
|
102 | end | |||
|
103 | end | |||
|
104 | ||||
|
105 | def test_edit_tag_with_check_box_style_should_select_current_value | |||
|
106 | field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false, | |||
|
107 | :edit_tag_style => 'check_box') | |||
|
108 | value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new, :value => 'Bar') | |||
|
109 | ||||
|
110 | tag = field.format.edit_tag(self, 'id', 'name', value) | |||
|
111 | assert_select_in tag, 'span' do | |||
|
112 | assert_select 'input[type=radio][checked=checked]', 1 | |||
|
113 | assert_select 'label', :text => 'Bar' do | |||
|
114 | assert_select 'input[value=Bar][checked=checked]' | |||
|
115 | end | |||
|
116 | end | |||
|
117 | end | |||
|
118 | ||||
|
119 | def test_edit_tag_with_check_box_style_and_multiple_should_select_current_values | |||
|
120 | field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz'], :is_required => false, | |||
|
121 | :multiple => true, :edit_tag_style => 'check_box') | |||
|
122 | value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new, :value => ['Bar', 'Baz']) | |||
|
123 | ||||
|
124 | tag = field.format.edit_tag(self, 'id', 'name', value) | |||
|
125 | assert_select_in tag, 'span' do | |||
|
126 | assert_select 'input[type=checkbox][checked=checked]', 2 | |||
|
127 | assert_select 'label', :text => 'Bar' do | |||
|
128 | assert_select 'input[value=Bar][checked=checked]' | |||
|
129 | end | |||
|
130 | assert_select 'label', :text => 'Baz' do | |||
|
131 | assert_select 'input[value=Baz][checked=checked]' | |||
|
132 | end | |||
|
133 | end | |||
|
134 | end | |||
|
135 | end |
@@ -0,0 +1,60 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | require File.expand_path('../../../../../test_helper', __FILE__) | |||
|
19 | require 'redmine/field_format' | |||
|
20 | ||||
|
21 | class Redmine::UserFieldFormatTest < ActionView::TestCase | |||
|
22 | include ApplicationHelper | |||
|
23 | ||||
|
24 | fixtures :projects, :roles, :users, :members, :member_roles | |||
|
25 | ||||
|
26 | def test_user_role_should_reject_blank_values | |||
|
27 | field = IssueCustomField.new(:name => 'Foo', :field_format => 'user', :user_role => ["1", ""]) | |||
|
28 | field.save! | |||
|
29 | assert_equal ["1"], field.user_role | |||
|
30 | end | |||
|
31 | ||||
|
32 | def test_existing_values_should_be_valid | |||
|
33 | field = IssueCustomField.create!(:name => 'Foo', :field_format => 'user', :is_for_all => true, :trackers => Tracker.all) | |||
|
34 | project = Project.generate! | |||
|
35 | user = User.generate! | |||
|
36 | User.add_to_project(user, project, Role.find_by_name('Manager')) | |||
|
37 | issue = Issue.generate!(:project_id => project.id, :tracker_id => 1, :custom_field_values => {field.id => user.id}) | |||
|
38 | ||||
|
39 | field.user_role = [Role.find_by_name('Developer').id] | |||
|
40 | field.save! | |||
|
41 | ||||
|
42 | issue = Issue.order('id DESC').first | |||
|
43 | assert_include [user.name, user.id.to_s], field.possible_custom_value_options(issue.custom_value_for(field)) | |||
|
44 | assert issue.valid? | |||
|
45 | end | |||
|
46 | ||||
|
47 | def test_possible_values_options_should_return_project_members | |||
|
48 | field = IssueCustomField.new(:field_format => 'user') | |||
|
49 | project = Project.find(1) | |||
|
50 | ||||
|
51 | assert_equal ['Dave Lopper', 'John Smith'], field.possible_values_options(project).map(&:first) | |||
|
52 | end | |||
|
53 | ||||
|
54 | def test_possible_values_options_should_return_project_members_with_selected_role | |||
|
55 | field = IssueCustomField.new(:field_format => 'user', :user_role => ["2"]) | |||
|
56 | project = Project.find(1) | |||
|
57 | ||||
|
58 | assert_equal ['Dave Lopper'], field.possible_values_options(project).map(&:first) | |||
|
59 | end | |||
|
60 | end |
@@ -0,0 +1,61 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | require File.expand_path('../../../../../test_helper', __FILE__) | |||
|
19 | require 'redmine/field_format' | |||
|
20 | ||||
|
21 | class Redmine::VersionFieldFormatTest < ActionView::TestCase | |||
|
22 | include ApplicationHelper | |||
|
23 | ||||
|
24 | fixtures :projects, :versions, :trackers | |||
|
25 | ||||
|
26 | def test_version_status_should_reject_blank_values | |||
|
27 | field = IssueCustomField.new(:name => 'Foo', :field_format => 'version', :version_status => ["open", ""]) | |||
|
28 | field.save! | |||
|
29 | assert_equal ["open"], field.version_status | |||
|
30 | end | |||
|
31 | ||||
|
32 | def test_existing_values_should_be_valid | |||
|
33 | field = IssueCustomField.create!(:name => 'Foo', :field_format => 'version', :is_for_all => true, :trackers => Tracker.all) | |||
|
34 | project = Project.generate! | |||
|
35 | version = Version.generate!(:project => project, :status => 'open') | |||
|
36 | issue = Issue.generate!(:project_id => project.id, :tracker_id => 1, :custom_field_values => {field.id => version.id}) | |||
|
37 | ||||
|
38 | field.version_status = ["open"] | |||
|
39 | field.save! | |||
|
40 | ||||
|
41 | issue = Issue.order('id DESC').first | |||
|
42 | assert_include [version.name, version.id.to_s], field.possible_custom_value_options(issue.custom_value_for(field)) | |||
|
43 | assert issue.valid? | |||
|
44 | end | |||
|
45 | ||||
|
46 | def test_possible_values_options_should_return_project_versions | |||
|
47 | field = IssueCustomField.new(:field_format => 'version') | |||
|
48 | project = Project.find(1) | |||
|
49 | expected = project.shared_versions.sort.map(&:name) | |||
|
50 | ||||
|
51 | assert_equal expected, field.possible_values_options(project).map(&:first) | |||
|
52 | end | |||
|
53 | ||||
|
54 | def test_possible_values_options_should_return_project_versions_with_selected_status | |||
|
55 | field = IssueCustomField.new(:field_format => 'version', :version_status => ["open"]) | |||
|
56 | project = Project.find(1) | |||
|
57 | expected = project.shared_versions.sort.select {|v| v.status == "open"}.map(&:name) | |||
|
58 | ||||
|
59 | assert_equal expected, field.possible_values_options(project).map(&:first) | |||
|
60 | end | |||
|
61 | end |
@@ -36,6 +36,8 class CustomFieldsController < ApplicationController | |||||
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def new |
|
38 | def new | |
|
39 | @custom_field.field_format = 'string' if @custom_field.field_format.blank? | |||
|
40 | @custom_field.default_value = nil | |||
39 | end |
|
41 | end | |
40 |
|
42 | |||
41 | def create |
|
43 | def create | |
@@ -76,8 +78,6 class CustomFieldsController < ApplicationController | |||||
76 | @custom_field = CustomField.new_subclass_instance(params[:type], params[:custom_field]) |
|
78 | @custom_field = CustomField.new_subclass_instance(params[:type], params[:custom_field]) | |
77 | if @custom_field.nil? |
|
79 | if @custom_field.nil? | |
78 | render_404 |
|
80 | render_404 | |
79 | else |
|
|||
80 | @custom_field.default_value = nil |
|
|||
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 |
|
83 |
@@ -158,6 +158,8 module ApplicationHelper | |||||
158 | # Helper that formats object for html or text rendering |
|
158 | # Helper that formats object for html or text rendering | |
159 | def format_object(object, html=true) |
|
159 | def format_object(object, html=true) | |
160 | case object.class.name |
|
160 | case object.class.name | |
|
161 | when 'Array' | |||
|
162 | object.map {|o| format_object(o, html)}.join(', ').html_safe | |||
161 | when 'Time' |
|
163 | when 'Time' | |
162 | format_time(object) |
|
164 | format_time(object) | |
163 | when 'Date' |
|
165 | when 'Date' | |
@@ -171,13 +173,24 module ApplicationHelper | |||||
171 | when 'Project' |
|
173 | when 'Project' | |
172 | html ? link_to_project(object) : object.to_s |
|
174 | html ? link_to_project(object) : object.to_s | |
173 | when 'Version' |
|
175 | when 'Version' | |
174 |
html ? link_to(object.name, version_path(object)) : |
|
176 | html ? link_to(object.name, version_path(object)) : object.to_s | |
175 | when 'TrueClass' |
|
177 | when 'TrueClass' | |
176 | l(:general_text_Yes) |
|
178 | l(:general_text_Yes) | |
177 | when 'FalseClass' |
|
179 | when 'FalseClass' | |
178 | l(:general_text_No) |
|
180 | l(:general_text_No) | |
179 | when 'Issue' |
|
181 | when 'Issue' | |
180 | object.visible? && html ? link_to_issue(object) : "##{object.id}" |
|
182 | object.visible? && html ? link_to_issue(object) : "##{object.id}" | |
|
183 | when 'CustomValue', 'CustomFieldValue' | |||
|
184 | if object.custom_field | |||
|
185 | f = object.custom_field.format.formatted_custom_value(self, object, html) | |||
|
186 | if f.nil? || f.is_a?(String) | |||
|
187 | f | |||
|
188 | else | |||
|
189 | format_object(f, html) | |||
|
190 | end | |||
|
191 | else | |||
|
192 | object.value.to_s | |||
|
193 | end | |||
181 | else |
|
194 | else | |
182 | html ? h(object) : object.to_s |
|
195 | html ? h(object) : object.to_s | |
183 | end |
|
196 | end |
@@ -44,51 +44,39 module CustomFieldsHelper | |||||
44 | CUSTOM_FIELDS_TABS |
|
44 | CUSTOM_FIELDS_TABS | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | # Return custom field html tag corresponding to its format |
|
47 | def render_custom_field_format_partial(form, custom_field) | |
48 | def custom_field_tag(name, custom_value) |
|
48 | partial = custom_field.format.form_partial | |
49 | custom_field = custom_value.custom_field |
|
49 | if partial | |
50 | field_name = "#{name}[custom_field_values][#{custom_field.id}]" |
|
50 | render :partial => custom_field.format.form_partial, :locals => {:f => form, :custom_field => custom_field} | |
51 | field_name << "[]" if custom_field.multiple? |
|
|||
52 | field_id = "#{name}_custom_field_values_#{custom_field.id}" |
|
|||
53 |
|
||||
54 | tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"} |
|
|||
55 |
|
||||
56 | field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format) |
|
|||
57 | case field_format.try(:edit_as) |
|
|||
58 | when "date" |
|
|||
59 | text_field_tag(field_name, custom_value.value, tag_options.merge(:size => 10)) + |
|
|||
60 | calendar_for(field_id) |
|
|||
61 | when "text" |
|
|||
62 | text_area_tag(field_name, custom_value.value, tag_options.merge(:rows => 3)) |
|
|||
63 | when "bool" |
|
|||
64 | hidden_field_tag(field_name, '0') + check_box_tag(field_name, '1', custom_value.true?, tag_options) |
|
|||
65 | when "list" |
|
|||
66 | blank_option = ''.html_safe |
|
|||
67 | unless custom_field.multiple? |
|
|||
68 | if custom_field.is_required? |
|
|||
69 | unless custom_field.default_value.present? |
|
|||
70 | blank_option = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '') |
|
|||
71 | end |
|
|||
72 | else |
|
|||
73 | blank_option = content_tag('option') |
|
|||
74 | end |
|
|||
75 | end |
|
|||
76 | s = select_tag(field_name, blank_option + options_for_select(custom_field.possible_values_options(custom_value.customized), custom_value.value), |
|
|||
77 | tag_options.merge(:multiple => custom_field.multiple?)) |
|
|||
78 | if custom_field.multiple? |
|
|||
79 | s << hidden_field_tag(field_name, '') |
|
|||
80 | end |
|
|||
81 | s |
|
|||
82 | else |
|
|||
83 | text_field_tag(field_name, custom_value.value, tag_options) |
|
|||
84 | end |
|
51 | end | |
85 | end |
|
52 | end | |
86 |
|
53 | |||
|
54 | def custom_field_tag_name(prefix, custom_field) | |||
|
55 | name = "#{prefix}[custom_field_values][#{custom_field.id}]" | |||
|
56 | name << "[]" if custom_field.multiple? | |||
|
57 | name | |||
|
58 | end | |||
|
59 | ||||
|
60 | def custom_field_tag_id(prefix, custom_field) | |||
|
61 | "#{prefix}_custom_field_values_#{custom_field.id}" | |||
|
62 | end | |||
|
63 | ||||
|
64 | # Return custom field html tag corresponding to its format | |||
|
65 | def custom_field_tag(prefix, custom_value) | |||
|
66 | custom_value.custom_field.format.edit_tag self, | |||
|
67 | custom_field_tag_id(prefix, custom_value.custom_field), | |||
|
68 | custom_field_tag_name(prefix, custom_value.custom_field), | |||
|
69 | custom_value, | |||
|
70 | :class => "#{custom_value.custom_field.field_format}_cf" | |||
|
71 | end | |||
|
72 | ||||
87 | # Return custom field label tag |
|
73 | # Return custom field label tag | |
88 | def custom_field_label_tag(name, custom_value, options={}) |
|
74 | def custom_field_label_tag(name, custom_value, options={}) | |
89 | required = options[:required] || custom_value.custom_field.is_required? |
|
75 | required = options[:required] || custom_value.custom_field.is_required? | |
|
76 | title = custom_value.custom_field.description.presence | |||
|
77 | content = content_tag 'span', custom_value.custom_field.name, :title => title | |||
90 |
|
78 | |||
91 |
content_tag "label", |
|
79 | content_tag "label", content + | |
92 | (required ? " <span class=\"required\">*</span>".html_safe : ""), |
|
80 | (required ? " <span class=\"required\">*</span>".html_safe : ""), | |
93 | :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}" |
|
81 | :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}" | |
94 | end |
|
82 | end | |
@@ -98,65 +86,30 module CustomFieldsHelper | |||||
98 | custom_field_label_tag(name, custom_value, options) + custom_field_tag(name, custom_value) |
|
86 | custom_field_label_tag(name, custom_value, options) + custom_field_tag(name, custom_value) | |
99 | end |
|
87 | end | |
100 |
|
88 | |||
101 | def custom_field_tag_for_bulk_edit(name, custom_field, projects=nil, value='') |
|
89 | # Returns the custom field tag for when bulk editing objects | |
102 | field_name = "#{name}[custom_field_values][#{custom_field.id}]" |
|
90 | def custom_field_tag_for_bulk_edit(prefix, custom_field, objects=nil, value='') | |
103 | field_name << "[]" if custom_field.multiple? |
|
91 | custom_field.format.bulk_edit_tag self, | |
104 | field_id = "#{name}_custom_field_values_#{custom_field.id}" |
|
92 | custom_field_tag_id(prefix, custom_field), | |
105 |
|
93 | custom_field_tag_name(prefix, custom_field), | ||
106 | tag_options = {:id => field_id, :class => "#{custom_field.field_format}_cf"} |
|
94 | custom_field, | |
107 |
|
95 | objects, | ||
108 | unset_tag = '' |
|
96 | value, | |
109 | unless custom_field.is_required? |
|
97 | :class => "#{custom_field.field_format}_cf" | |
110 | unset_tag = content_tag('label', |
|
|||
111 | check_box_tag(field_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{field_id}"}) + l(:button_clear), |
|
|||
112 | :class => 'inline' |
|
|||
113 | ) |
|
|||
114 | end |
|
|||
115 |
|
||||
116 | field_format = Redmine::CustomFieldFormat.find_by_name(custom_field.field_format) |
|
|||
117 | case field_format.try(:edit_as) |
|
|||
118 | when "date" |
|
|||
119 | text_field_tag(field_name, value, tag_options.merge(:size => 10)) + |
|
|||
120 | calendar_for(field_id) + |
|
|||
121 | unset_tag |
|
|||
122 | when "text" |
|
|||
123 | text_area_tag(field_name, value, tag_options.merge(:rows => 3)) + |
|
|||
124 | '<br />'.html_safe + |
|
|||
125 | unset_tag |
|
|||
126 | when "bool" |
|
|||
127 | select_tag(field_name, options_for_select([[l(:label_no_change_option), ''], |
|
|||
128 | [l(:general_text_yes), '1'], |
|
|||
129 | [l(:general_text_no), '0']], value), tag_options) |
|
|||
130 | when "list" |
|
|||
131 | options = [] |
|
|||
132 | options << [l(:label_no_change_option), ''] unless custom_field.multiple? |
|
|||
133 | options << [l(:label_none), '__none__'] unless custom_field.is_required? |
|
|||
134 | options += custom_field.possible_values_options(projects) |
|
|||
135 | select_tag(field_name, options_for_select(options, value), tag_options.merge(:multiple => custom_field.multiple?)) |
|
|||
136 | else |
|
|||
137 | text_field_tag(field_name, value, tag_options) + |
|
|||
138 | unset_tag |
|
|||
139 | end |
|
|||
140 | end |
|
98 | end | |
141 |
|
99 | |||
142 | # Return a string used to display a custom value |
|
100 | # Return a string used to display a custom value | |
143 | def show_value(custom_value) |
|
101 | def show_value(custom_value, html=true) | |
144 | return "" unless custom_value |
|
102 | format_object(custom_value, html) | |
145 | format_value(custom_value.value, custom_value.custom_field.field_format) |
|
|||
146 | end |
|
103 | end | |
147 |
|
104 | |||
148 | # Return a string used to display a custom value |
|
105 | # Return a string used to display a custom value | |
149 |
def format_value(value, field |
|
106 | def format_value(value, custom_field) | |
150 | if value.is_a?(Array) |
|
107 | format_object(custom_field.format.formatted_value(self, custom_field, value, false), false) | |
151 | value.collect {|v| format_value(v, field_format)}.compact.sort.join(', ') |
|
|||
152 | else |
|
|||
153 | Redmine::CustomFieldFormat.format_value(value, field_format) |
|
|||
154 | end |
|
|||
155 | end |
|
108 | end | |
156 |
|
109 | |||
157 | # Return an array of custom field formats which can be used in select_tag |
|
110 | # Return an array of custom field formats which can be used in select_tag | |
158 | def custom_field_formats_for_select(custom_field) |
|
111 | def custom_field_formats_for_select(custom_field) | |
159 |
Redmine:: |
|
112 | Redmine::FieldFormat.as_select(custom_field.class.customized_class.name) | |
160 | end |
|
113 | end | |
161 |
|
114 | |||
162 | # Renders the custom_values in api views |
|
115 | # Renders the custom_values in api views | |
@@ -179,4 +132,8 module CustomFieldsHelper | |||||
179 | end |
|
132 | end | |
180 | end unless custom_values.empty? |
|
133 | end unless custom_values.empty? | |
181 | end |
|
134 | end | |
|
135 | ||||
|
136 | def edit_tag_style_tag(form) | |||
|
137 | form.select :edit_tag_style, [[l(:label_drop_down_list), ''], [l(:label_checkboxes), 'check_box']], :label => :label_display | |||
|
138 | end | |||
182 | end |
|
139 | end |
@@ -171,8 +171,9 module IssuesHelper | |||||
171 | s = "<tr>\n" |
|
171 | s = "<tr>\n" | |
172 | n = 0 |
|
172 | n = 0 | |
173 | ordered_values.compact.each do |value| |
|
173 | ordered_values.compact.each do |value| | |
|
174 | css = "cf_#{value.custom_field.id}" | |||
174 | s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0 |
|
175 | s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0 | |
175 |
s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ |
|
176 | s << "\t<th class=\"#{css}\">#{ h(value.custom_field.name) }:</th><td class=\"#{css}\">#{ h(show_value(value)) }</td>\n" | |
176 | n += 1 |
|
177 | n += 1 | |
177 | end |
|
178 | end | |
178 | s << "</tr>\n" |
|
179 | s << "</tr>\n" | |
@@ -239,7 +240,7 module IssuesHelper | |||||
239 | end |
|
240 | end | |
240 | end |
|
241 | end | |
241 | issue.visible_custom_field_values(user).each do |value| |
|
242 | issue.visible_custom_field_values(user).each do |value| | |
242 | items << "#{value.custom_field.name}: #{show_value(value)}" |
|
243 | items << "#{value.custom_field.name}: #{show_value(value, false)}" | |
243 | end |
|
244 | end | |
244 | items |
|
245 | items | |
245 | end |
|
246 | end | |
@@ -324,8 +325,8 module IssuesHelper | |||||
324 | if custom_field |
|
325 | if custom_field | |
325 | multiple = custom_field.multiple? |
|
326 | multiple = custom_field.multiple? | |
326 | label = custom_field.name |
|
327 | label = custom_field.name | |
327 |
value = format_value(detail.value, custom_field |
|
328 | value = format_value(detail.value, custom_field) if detail.value | |
328 |
old_value = format_value(detail.old_value, custom_field |
|
329 | old_value = format_value(detail.old_value, custom_field) if detail.old_value | |
329 | end |
|
330 | end | |
330 | when 'attachment' |
|
331 | when 'attachment' | |
331 | label = l(:label_attachment) |
|
332 | label = l(:label_attachment) |
@@ -96,8 +96,10 module TimelogHelper | |||||
96 | else |
|
96 | else | |
97 | obj |
|
97 | obj | |
98 | end |
|
98 | end | |
|
99 | elsif cf = criteria_options[:custom_field] | |||
|
100 | format_value(value, cf) | |||
99 | else |
|
101 | else | |
100 | format_value(value, criteria_options[:format]) |
|
102 | value.to_s | |
101 | end |
|
103 | end | |
102 | end |
|
104 | end | |
103 |
|
105 |
@@ -22,14 +22,18 class CustomField < ActiveRecord::Base | |||||
22 | has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "custom_field_id" |
|
22 | has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "custom_field_id" | |
23 | acts_as_list :scope => 'type = \'#{self.class}\'' |
|
23 | acts_as_list :scope => 'type = \'#{self.class}\'' | |
24 | serialize :possible_values |
|
24 | serialize :possible_values | |
|
25 | store :format_store | |||
25 |
|
26 | |||
26 | validates_presence_of :name, :field_format |
|
27 | validates_presence_of :name, :field_format | |
27 | validates_uniqueness_of :name, :scope => :type |
|
28 | validates_uniqueness_of :name, :scope => :type | |
28 | validates_length_of :name, :maximum => 30 |
|
29 | validates_length_of :name, :maximum => 30 | |
29 |
validates_inclusion_of :field_format, :in => Proc.new { Redmine:: |
|
30 | validates_inclusion_of :field_format, :in => Proc.new { Redmine::FieldFormat.available_formats } | |
30 | validate :validate_custom_field |
|
31 | validate :validate_custom_field | |
31 |
|
32 | |||
32 | before_validation :set_searchable |
|
33 | before_validation :set_searchable | |
|
34 | before_save do |field| | |||
|
35 | field.format.before_custom_field_save(field) | |||
|
36 | end | |||
33 | after_save :handle_multiplicity_change |
|
37 | after_save :handle_multiplicity_change | |
34 | after_save do |field| |
|
38 | after_save do |field| | |
35 | if field.visible_changed? && field.visible |
|
39 | if field.visible_changed? && field.visible | |
@@ -57,23 +61,29 class CustomField < ActiveRecord::Base | |||||
57 | visible? || user.admin? |
|
61 | visible? || user.admin? | |
58 | end |
|
62 | end | |
59 |
|
63 | |||
|
64 | def format | |||
|
65 | @format ||= Redmine::FieldFormat.find(field_format) | |||
|
66 | end | |||
|
67 | ||||
60 | def field_format=(arg) |
|
68 | def field_format=(arg) | |
61 | # cannot change format of a saved custom field |
|
69 | # cannot change format of a saved custom field | |
62 |
|
|
70 | if new_record? | |
|
71 | @format = nil | |||
|
72 | super | |||
|
73 | end | |||
63 | end |
|
74 | end | |
64 |
|
75 | |||
65 | def set_searchable |
|
76 | def set_searchable | |
66 | # make sure these fields are not searchable |
|
77 | # make sure these fields are not searchable | |
67 | self.searchable = false if %w(int float date bool).include?(field_format) |
|
78 | self.searchable = false unless format.class.searchable_supported | |
68 | # make sure only these fields can have multiple values |
|
79 | # make sure only these fields can have multiple values | |
69 | self.multiple = false unless %w(list user version).include?(field_format) |
|
80 | self.multiple = false unless format.class.multiple_supported | |
70 | true |
|
81 | true | |
71 | end |
|
82 | end | |
72 |
|
83 | |||
73 | def validate_custom_field |
|
84 | def validate_custom_field | |
74 | if self.field_format == "list" |
|
85 | format.validate_custom_field(self).each do |attribute, message| | |
75 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? |
|
86 | errors.add attribute, message | |
76 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array |
|
|||
77 | end |
|
87 | end | |
78 |
|
88 | |||
79 | if regexp.present? |
|
89 | if regexp.present? | |
@@ -84,49 +94,34 class CustomField < ActiveRecord::Base | |||||
84 | end |
|
94 | end | |
85 | end |
|
95 | end | |
86 |
|
96 | |||
87 |
if default_value.present? |
|
97 | if default_value.present? | |
88 | errors.add(:default_value, :invalid) |
|
98 | validate_field_value(default_value).each do |message| | |
|
99 | errors.add :default_value, message | |||
|
100 | end | |||
89 | end |
|
101 | end | |
90 | end |
|
102 | end | |
91 |
|
103 | |||
92 |
def possible_value |
|
104 | def possible_custom_value_options(custom_value) | |
93 | case field_format |
|
105 | format.possible_custom_value_options(custom_value) | |
94 | when 'user', 'version' |
|
106 | end | |
95 | if obj.respond_to?(:project) && obj.project |
|
107 | ||
96 | case field_format |
|
108 | def possible_values_options(object=nil) | |
97 | when 'user' |
|
109 | if object.is_a?(Array) | |
98 | obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} |
|
110 | object.map {|o| format.possible_values_options(self, o)}.reduce(:&) || [] | |
99 | when 'version' |
|
|||
100 | obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} |
|
|||
101 | end |
|
|||
102 | elsif obj.is_a?(Array) |
|
|||
103 | obj.collect {|o| possible_values_options(o)}.reduce(:&) |
|
|||
104 | else |
|
|||
105 | [] |
|
|||
106 | end |
|
|||
107 | when 'bool' |
|
|||
108 | [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']] |
|
|||
109 | else |
|
111 | else | |
110 | possible_values || [] |
|
112 | format.possible_values_options(self, object) || [] | |
111 | end |
|
113 | end | |
112 | end |
|
114 | end | |
113 |
|
115 | |||
114 |
def possible_values |
|
116 | def possible_values | |
115 | case field_format |
|
117 | values = super() | |
116 | when 'user', 'version' |
|
118 | if values.is_a?(Array) | |
117 | possible_values_options(obj).collect(&:last) |
|
119 | values.each do |value| | |
118 | when 'bool' |
|
120 | value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) | |
119 | ['1', '0'] |
|
|||
120 | else |
|
|||
121 | values = super() |
|
|||
122 | if values.is_a?(Array) |
|
|||
123 | values.each do |value| |
|
|||
124 | value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) |
|
|||
125 | end |
|
|||
126 | values |
|
|||
127 | else |
|
|||
128 | [] |
|
|||
129 | end |
|
121 | end | |
|
122 | values | |||
|
123 | else | |||
|
124 | [] | |||
130 | end |
|
125 | end | |
131 | end |
|
126 | end | |
132 |
|
127 | |||
@@ -140,24 +135,7 class CustomField < ActiveRecord::Base | |||||
140 | end |
|
135 | end | |
141 |
|
136 | |||
142 | def cast_value(value) |
|
137 | def cast_value(value) | |
143 | casted = nil |
|
138 | format.cast_value(self, value) | |
144 | unless value.blank? |
|
|||
145 | case field_format |
|
|||
146 | when 'string', 'text', 'list' |
|
|||
147 | casted = value |
|
|||
148 | when 'date' |
|
|||
149 | casted = begin; value.to_date; rescue; nil end |
|
|||
150 | when 'bool' |
|
|||
151 | casted = (value == '1' ? true : false) |
|
|||
152 | when 'int' |
|
|||
153 | casted = value.to_i |
|
|||
154 | when 'float' |
|
|||
155 | casted = value.to_f |
|
|||
156 | when 'user', 'version' |
|
|||
157 | casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) |
|
|||
158 | end |
|
|||
159 | end |
|
|||
160 | casted |
|
|||
161 | end |
|
139 | end | |
162 |
|
140 | |||
163 | def value_from_keyword(keyword, customized) |
|
141 | def value_from_keyword(keyword, customized) | |
@@ -181,83 +159,18 class CustomField < ActiveRecord::Base | |||||
181 | # Returns nil if the custom field can not be used for sorting. |
|
159 | # Returns nil if the custom field can not be used for sorting. | |
182 | def order_statement |
|
160 | def order_statement | |
183 | return nil if multiple? |
|
161 | return nil if multiple? | |
184 | case field_format |
|
162 | format.order_statement(self) | |
185 | when 'string', 'text', 'list', 'date', 'bool' |
|
|||
186 | # COALESCE is here to make sure that blank and NULL values are sorted equally |
|
|||
187 | "COALESCE(#{join_alias}.value, '')" |
|
|||
188 | when 'int', 'float' |
|
|||
189 | # Make the database cast values into numeric |
|
|||
190 | # Postgresql will raise an error if a value can not be casted! |
|
|||
191 | # CustomValue validations should ensure that it doesn't occur |
|
|||
192 | "CAST(CASE #{join_alias}.value WHEN '' THEN '0' ELSE #{join_alias}.value END AS decimal(30,3))" |
|
|||
193 | when 'user', 'version' |
|
|||
194 | value_class.fields_for_order_statement(value_join_alias) |
|
|||
195 | else |
|
|||
196 | nil |
|
|||
197 | end |
|
|||
198 | end |
|
163 | end | |
199 |
|
164 | |||
200 | # Returns a GROUP BY clause that can used to group by custom value |
|
165 | # Returns a GROUP BY clause that can used to group by custom value | |
201 | # Returns nil if the custom field can not be used for grouping. |
|
166 | # Returns nil if the custom field can not be used for grouping. | |
202 | def group_statement |
|
167 | def group_statement | |
203 | return nil if multiple? |
|
168 | return nil if multiple? | |
204 | case field_format |
|
169 | format.group_statement(self) | |
205 | when 'list', 'date', 'bool', 'int' |
|
|||
206 | order_statement |
|
|||
207 | when 'user', 'version' |
|
|||
208 | "COALESCE(#{join_alias}.value, '')" |
|
|||
209 | else |
|
|||
210 | nil |
|
|||
211 | end |
|
|||
212 | end |
|
170 | end | |
213 |
|
171 | |||
214 | def join_for_order_statement |
|
172 | def join_for_order_statement | |
215 | case field_format |
|
173 | format.join_for_order_statement(self) | |
216 | when 'user', 'version' |
|
|||
217 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + |
|
|||
218 | " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + |
|
|||
219 | " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + |
|
|||
220 | " AND #{join_alias}.custom_field_id = #{id}" + |
|
|||
221 | " AND (#{visibility_by_project_condition})" + |
|
|||
222 | " AND #{join_alias}.value <> ''" + |
|
|||
223 | " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + |
|
|||
224 | " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + |
|
|||
225 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + |
|
|||
226 | " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" + |
|
|||
227 | " LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" + |
|
|||
228 | " ON CAST(CASE #{join_alias}.value WHEN '' THEN '0' ELSE #{join_alias}.value END AS decimal(30,0)) = #{value_join_alias}.id" |
|
|||
229 | when 'int', 'float' |
|
|||
230 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + |
|
|||
231 | " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + |
|
|||
232 | " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + |
|
|||
233 | " AND #{join_alias}.custom_field_id = #{id}" + |
|
|||
234 | " AND (#{visibility_by_project_condition})" + |
|
|||
235 | " AND #{join_alias}.value <> ''" + |
|
|||
236 | " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + |
|
|||
237 | " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + |
|
|||
238 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + |
|
|||
239 | " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" |
|
|||
240 | when 'string', 'text', 'list', 'date', 'bool' |
|
|||
241 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + |
|
|||
242 | " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + |
|
|||
243 | " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + |
|
|||
244 | " AND #{join_alias}.custom_field_id = #{id}" + |
|
|||
245 | " AND (#{visibility_by_project_condition})" + |
|
|||
246 | " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + |
|
|||
247 | " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + |
|
|||
248 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + |
|
|||
249 | " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" |
|
|||
250 | else |
|
|||
251 | nil |
|
|||
252 | end |
|
|||
253 | end |
|
|||
254 |
|
||||
255 | def join_alias |
|
|||
256 | "cf_#{id}" |
|
|||
257 | end |
|
|||
258 |
|
||||
259 | def value_join_alias |
|
|||
260 | join_alias + "_" + field_format |
|
|||
261 | end |
|
174 | end | |
262 |
|
175 | |||
263 | def visibility_by_project_condition(project_key=nil, user=User.current) |
|
176 | def visibility_by_project_condition(project_key=nil, user=User.current) | |
@@ -293,12 +206,7 class CustomField < ActiveRecord::Base | |||||
293 |
|
206 | |||
294 | # Returns the class that values represent |
|
207 | # Returns the class that values represent | |
295 | def value_class |
|
208 | def value_class | |
296 | case field_format |
|
209 | format.target_class if format.respond_to?(:target_class) | |
297 | when 'user', 'version' |
|
|||
298 | field_format.classify.constantize |
|
|||
299 | else |
|
|||
300 | nil |
|
|||
301 | end |
|
|||
302 | end |
|
210 | end | |
303 |
|
211 | |||
304 | def self.customized_class |
|
212 | def self.customized_class | |
@@ -317,7 +225,8 class CustomField < ActiveRecord::Base | |||||
317 |
|
225 | |||
318 | # Returns the error messages for the given value |
|
226 | # Returns the error messages for the given value | |
319 | # or an empty array if value is a valid value for the custom field |
|
227 | # or an empty array if value is a valid value for the custom field | |
320 |
def validate_ |
|
228 | def validate_custom_value(custom_value) | |
|
229 | value = custom_value.value | |||
321 | errs = [] |
|
230 | errs = [] | |
322 | if value.is_a?(Array) |
|
231 | if value.is_a?(Array) | |
323 | if !multiple? |
|
232 | if !multiple? | |
@@ -326,16 +235,22 class CustomField < ActiveRecord::Base | |||||
326 | if is_required? && value.detect(&:present?).nil? |
|
235 | if is_required? && value.detect(&:present?).nil? | |
327 | errs << ::I18n.t('activerecord.errors.messages.blank') |
|
236 | errs << ::I18n.t('activerecord.errors.messages.blank') | |
328 | end |
|
237 | end | |
329 | value.each {|v| errs += validate_field_value_format(v)} |
|
|||
330 | else |
|
238 | else | |
331 | if is_required? && value.blank? |
|
239 | if is_required? && value.blank? | |
332 | errs << ::I18n.t('activerecord.errors.messages.blank') |
|
240 | errs << ::I18n.t('activerecord.errors.messages.blank') | |
333 | end |
|
241 | end | |
334 | errs += validate_field_value_format(value) |
|
242 | end | |
|
243 | if custom_value.value.present? | |||
|
244 | errs += format.validate_custom_value(custom_value) | |||
335 | end |
|
245 | end | |
336 | errs |
|
246 | errs | |
337 | end |
|
247 | end | |
338 |
|
248 | |||
|
249 | # Returns the error messages for the default custom field value | |||
|
250 | def validate_field_value(value) | |||
|
251 | validate_custom_value(CustomValue.new(:custom_field => self, :value => value)) | |||
|
252 | end | |||
|
253 | ||||
339 | # Returns true if value is a valid value for the custom field |
|
254 | # Returns true if value is a valid value for the custom field | |
340 | def valid_field_value?(value) |
|
255 | def valid_field_value?(value) | |
341 | validate_field_value(value).empty? |
|
256 | validate_field_value(value).empty? | |
@@ -347,29 +262,6 class CustomField < ActiveRecord::Base | |||||
347 |
|
262 | |||
348 | protected |
|
263 | protected | |
349 |
|
264 | |||
350 | # Returns the error message for the given value regarding its format |
|
|||
351 | def validate_field_value_format(value) |
|
|||
352 | errs = [] |
|
|||
353 | if value.present? |
|
|||
354 | errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp) |
|
|||
355 | errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length |
|
|||
356 | errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length |
|
|||
357 |
|
||||
358 | # Format specific validations |
|
|||
359 | case field_format |
|
|||
360 | when 'int' |
|
|||
361 | errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/ |
|
|||
362 | when 'float' |
|
|||
363 | begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end |
|
|||
364 | when 'date' |
|
|||
365 | errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end |
|
|||
366 | when 'list' |
|
|||
367 | errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value) |
|
|||
368 | end |
|
|||
369 | end |
|
|||
370 | errs |
|
|||
371 | end |
|
|||
372 |
|
||||
373 | # Removes multiple values for the custom field after setting the multiple attribute to false |
|
265 | # Removes multiple values for the custom field after setting the multiple attribute to false | |
374 | # We kepp the value with the highest id for each customized object |
|
266 | # We kepp the value with the highest id for each customized object | |
375 | def handle_multiplicity_change |
|
267 | def handle_multiplicity_change |
@@ -16,7 +16,13 | |||||
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 | class CustomFieldValue |
|
18 | class CustomFieldValue | |
19 | attr_accessor :custom_field, :customized, :value |
|
19 | attr_accessor :custom_field, :customized, :value, :value_was | |
|
20 | ||||
|
21 | def initialize(attributes={}) | |||
|
22 | attributes.each do |name, v| | |||
|
23 | send "#{name}=", v | |||
|
24 | end | |||
|
25 | end | |||
20 |
|
26 | |||
21 | def custom_field_id |
|
27 | def custom_field_id | |
22 | custom_field.id |
|
28 | custom_field.id | |
@@ -43,7 +49,7 class CustomFieldValue | |||||
43 | end |
|
49 | end | |
44 |
|
50 | |||
45 | def validate_value |
|
51 | def validate_value | |
46 |
custom_field.validate_ |
|
52 | custom_field.validate_custom_value(self).each do |message| | |
47 | customized.errors.add(:base, custom_field.name + ' ' + message) |
|
53 | customized.errors.add(:base, custom_field.name + ' ' + message) | |
48 | end |
|
54 | end | |
49 | end |
|
55 | end |
@@ -587,7 +587,7 class Query < ActiveRecord::Base | |||||
587 | db_field = 'value' |
|
587 | db_field = 'value' | |
588 | filter = @available_filters[field] |
|
588 | filter = @available_filters[field] | |
589 | return nil unless filter |
|
589 | return nil unless filter | |
590 | if filter[:format] == 'user' |
|
590 | if filter[:field].format.target_class && filter[:field].format.target_class <= User | |
591 | if value.delete('me') |
|
591 | if value.delete('me') | |
592 | value.push User.current.id.to_s |
|
592 | value.push User.current.id.to_s | |
593 | end |
|
593 | end | |
@@ -764,29 +764,13 class Query < ActiveRecord::Base | |||||
764 |
|
764 | |||
765 | # Adds a filter for the given custom field |
|
765 | # Adds a filter for the given custom field | |
766 | def add_custom_field_filter(field, assoc=nil) |
|
766 | def add_custom_field_filter(field, assoc=nil) | |
767 | case field.field_format |
|
767 | options = field.format.query_filter_options(field, self) | |
768 | when "text" |
|
768 | if field.format.target_class && field.format.target_class <= User | |
769 | options = { :type => :text } |
|
769 | if options[:values].is_a?(Array) && User.current.logged? | |
770 | when "list" |
|
770 | options[:values].unshift ["<< #{l(:label_me)} >>", "me"] | |
771 | options = { :type => :list_optional, :values => field.possible_values } |
|
|||
772 | when "date" |
|
|||
773 | options = { :type => :date } |
|
|||
774 | when "bool" |
|
|||
775 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] } |
|
|||
776 | when "int" |
|
|||
777 | options = { :type => :integer } |
|
|||
778 | when "float" |
|
|||
779 | options = { :type => :float } |
|
|||
780 | when "user", "version" |
|
|||
781 | return unless project |
|
|||
782 | values = field.possible_values_options(project) |
|
|||
783 | if User.current.logged? && field.field_format == 'user' |
|
|||
784 | values.unshift ["<< #{l(:label_me)} >>", "me"] |
|
|||
785 | end |
|
771 | end | |
786 | options = { :type => :list_optional, :values => values } |
|
|||
787 | else |
|
|||
788 | options = { :type => :string } |
|
|||
789 | end |
|
772 | end | |
|
773 | ||||
790 | filter_id = "cf_#{field.id}" |
|
774 | filter_id = "cf_#{field.id}" | |
791 | filter_name = field.name |
|
775 | filter_name = field.name | |
792 | if assoc.present? |
|
776 | if assoc.present? | |
@@ -795,7 +779,6 class Query < ActiveRecord::Base | |||||
795 | end |
|
779 | end | |
796 | add_available_filter filter_id, options.merge({ |
|
780 | add_available_filter filter_id, options.merge({ | |
797 | :name => filter_name, |
|
781 | :name => filter_name, | |
798 | :format => field.field_format, |
|
|||
799 | :field => field |
|
782 | :field => field | |
800 | }) |
|
783 | }) | |
801 | end |
|
784 | end |
@@ -1,14 +1,12 | |||||
1 | <%= error_messages_for 'custom_field' %> |
|
1 | <%= error_messages_for 'custom_field' %> | |
2 |
|
2 | |||
3 | <% if @custom_field.is_a?(IssueCustomField) %> |
|
|||
4 | <div class="splitcontentleft"> |
|
3 | <div class="splitcontentleft"> | |
5 | <% end %> |
|
|||
6 |
|
||||
7 | <div class="box tabular"> |
|
4 | <div class="box tabular"> | |
8 | <p><%= f.text_field :name, :required => true %></p> |
|
5 | <p><%= f.text_field :name, :required => true %></p> | |
|
6 | <p><%= f.text_area :description, :rows => 7 %></p> | |||
9 | <p><%= f.select :field_format, custom_field_formats_for_select(@custom_field), {}, :disabled => !@custom_field.new_record? %></p> |
|
7 | <p><%= f.select :field_format, custom_field_formats_for_select(@custom_field), {}, :disabled => !@custom_field.new_record? %></p> | |
10 |
|
8 | |||
11 |
<% if @custom_field.format |
|
9 | <% if @custom_field.format.multiple_supported %> | |
12 | <p> |
|
10 | <p> | |
13 | <%= f.check_box :multiple %> |
|
11 | <%= f.check_box :multiple %> | |
14 | <% if !@custom_field.new_record? && @custom_field.multiple %> |
|
12 | <% if !@custom_field.new_record? && @custom_field.multiple %> | |
@@ -17,56 +15,38 | |||||
17 | </p> |
|
15 | </p> | |
18 | <% end %> |
|
16 | <% end %> | |
19 |
|
17 | |||
20 | <% unless @custom_field.format_in? 'list', 'bool', 'date', 'user', 'version' %> |
|
18 | <%= render_custom_field_format_partial f, @custom_field %> | |
21 | <p><label for="custom_field_min_length"><%=l(:label_min_max_length)%></label> |
|
|||
22 | <%= f.text_field :min_length, :size => 5, :no_label => true %> - |
|
|||
23 | <%= f.text_field :max_length, :size => 5, :no_label => true %><br />(<%=l(:text_min_max_length_info)%>)</p> |
|
|||
24 | <p><%= f.text_field :regexp, :size => 50 %><br />(<%=l(:text_regexp_info)%>)</p> |
|
|||
25 | <% end %> |
|
|||
26 |
|
||||
27 | <% if @custom_field.format_in? 'list' %> |
|
|||
28 | <p> |
|
|||
29 | <%= f.text_area :possible_values, :value => @custom_field.possible_values.to_a.join("\n"), :rows => 15 %> |
|
|||
30 | <em class="info"><%= l(:text_custom_field_possible_values_info) %></em> |
|
|||
31 | </p> |
|
|||
32 | <% end %> |
|
|||
33 |
|
||||
34 | <% case @custom_field.field_format %> |
|
|||
35 | <% when 'bool' %> |
|
|||
36 | <p><%= f.check_box(:default_value) %></p> |
|
|||
37 | <% when 'text' %> |
|
|||
38 | <p><%= f.text_area(:default_value, :rows => 8) %></p> |
|
|||
39 | <% when 'date' %> |
|
|||
40 | <p><%= f.text_field(:default_value, :size => 10) %></p> |
|
|||
41 | <%= calendar_for('custom_field_default_value') %> |
|
|||
42 | <% when 'user', 'version' %> |
|
|||
43 | <% else %> |
|
|||
44 | <p><%= f.text_field(:default_value) %></p> |
|
|||
45 | <% end %> |
|
|||
46 |
|
19 | |||
47 | <%= call_hook(:view_custom_fields_form_upper_box, :custom_field => @custom_field, :form => f) %> |
|
20 | <%= call_hook(:view_custom_fields_form_upper_box, :custom_field => @custom_field, :form => f) %> | |
48 | </div> |
|
21 | </div> | |
|
22 | <p><%= submit_tag l(:button_save) %></p> | |||
|
23 | </div> | |||
49 |
|
24 | |||
|
25 | <div class="splitcontentright"> | |||
50 | <div class="box tabular"> |
|
26 | <div class="box tabular"> | |
51 | <% case @custom_field.class.name |
|
27 | <% case @custom_field.class.name | |
52 | when "IssueCustomField" %> |
|
28 | when "IssueCustomField" %> | |
53 | <p><%= f.check_box :is_required %></p> |
|
29 | <p><%= f.check_box :is_required %></p> | |
54 | <p><%= f.check_box :is_for_all %></p> |
|
30 | <p><%= f.check_box :is_for_all, :data => {:disables => '#custom_field_project_ids input'} %></p> | |
55 | <p><%= f.check_box :is_filter %></p> |
|
31 | <p><%= f.check_box :is_filter %></p> | |
|
32 | <% if @custom_field.format.searchable_supported %> | |||
56 | <p><%= f.check_box :searchable %></p> |
|
33 | <p><%= f.check_box :searchable %></p> | |
|
34 | <% end %> | |||
57 | <p> |
|
35 | <p> | |
58 | <label><%= l(:field_visible) %></label> |
|
36 | <label><%= l(:field_visible) %></label> | |
59 | <label class="block"> |
|
37 | <label class="block"> | |
60 |
<%= radio_button_tag 'custom_field[visible]', 1, @custom_field.visible?, :id => 'custom_field_visible_on' |
|
38 | <%= radio_button_tag 'custom_field[visible]', 1, @custom_field.visible?, :id => 'custom_field_visible_on', | |
|
39 | :data => {:disables => '.custom_field_role input'} %> | |||
61 | <%= l(:label_visibility_public) %> |
|
40 | <%= l(:label_visibility_public) %> | |
62 | </label> |
|
41 | </label> | |
63 | <label class="block"> |
|
42 | <label class="block"> | |
64 |
<%= radio_button_tag 'custom_field[visible]', 0, !@custom_field.visible?, :id => 'custom_field_visible_off' |
|
43 | <%= radio_button_tag 'custom_field[visible]', 0, !@custom_field.visible?, :id => 'custom_field_visible_off', | |
|
44 | :data => {:enables => '.custom_field_role input'} %> | |||
65 | <%= l(:label_visibility_roles) %>: |
|
45 | <%= l(:label_visibility_roles) %>: | |
66 | </label> |
|
46 | </label> | |
67 | <% Role.givable.sorted.each do |role| %> |
|
47 | <% Role.givable.sorted.each do |role| %> | |
68 | <label class="block custom_field_role" style="padding-left:2em;"> |
|
48 | <label class="block custom_field_role" style="padding-left:2em;"> | |
69 | <%= check_box_tag 'custom_field[role_ids][]', role.id, @custom_field.roles.include?(role) %> |
|
49 | <%= check_box_tag 'custom_field[role_ids][]', role.id, @custom_field.roles.include?(role), :id => nil %> | |
70 | <%= role.name %> |
|
50 | <%= role.name %> | |
71 | </label> |
|
51 | </label> | |
72 | <% end %> |
|
52 | <% end %> | |
@@ -82,7 +62,9 when "IssueCustomField" %> | |||||
82 | <% when "ProjectCustomField" %> |
|
62 | <% when "ProjectCustomField" %> | |
83 | <p><%= f.check_box :is_required %></p> |
|
63 | <p><%= f.check_box :is_required %></p> | |
84 | <p><%= f.check_box :visible %></p> |
|
64 | <p><%= f.check_box :visible %></p> | |
|
65 | <% if @custom_field.format.searchable_supported %> | |||
85 | <p><%= f.check_box :searchable %></p> |
|
66 | <p><%= f.check_box :searchable %></p> | |
|
67 | <% end %> | |||
86 | <p><%= f.check_box :is_filter %></p> |
|
68 | <p><%= f.check_box :is_filter %></p> | |
87 |
|
69 | |||
88 | <% when "VersionCustomField" %> |
|
70 | <% when "VersionCustomField" %> | |
@@ -103,12 +85,9 when "IssueCustomField" %> | |||||
103 | <% end %> |
|
85 | <% end %> | |
104 | <%= call_hook(:"view_custom_fields_form_#{@custom_field.type.to_s.underscore}", :custom_field => @custom_field, :form => f) %> |
|
86 | <%= call_hook(:"view_custom_fields_form_#{@custom_field.type.to_s.underscore}", :custom_field => @custom_field, :form => f) %> | |
105 | </div> |
|
87 | </div> | |
106 | <%= submit_tag l(:button_save) %> |
|
|||
107 |
|
88 | |||
108 | <% if @custom_field.is_a?(IssueCustomField) %> |
|
89 | <% if @custom_field.is_a?(IssueCustomField) %> | |
109 | </div> |
|
90 | <fieldset class="box" id="custom_field_tracker_ids"><legend><%=l(:label_tracker_plural)%></legend> | |
110 | <div class="splitcontentright"> |
|
|||
111 | <fieldset class="box"><legend><%=l(:label_tracker_plural)%></legend> |
|
|||
112 | <% Tracker.sorted.all.each do |tracker| %> |
|
91 | <% Tracker.sorted.all.each do |tracker| %> | |
113 | <%= check_box_tag "custom_field[tracker_ids][]", |
|
92 | <%= check_box_tag "custom_field[tracker_ids][]", | |
114 | tracker.id, |
|
93 | tracker.id, | |
@@ -119,6 +98,7 when "IssueCustomField" %> | |||||
119 | </label> |
|
98 | </label> | |
120 | <% end %> |
|
99 | <% end %> | |
121 | <%= hidden_field_tag "custom_field[tracker_ids][]", '' %> |
|
100 | <%= hidden_field_tag "custom_field[tracker_ids][]", '' %> | |
|
101 | <p><%= check_all_links 'custom_field_tracker_ids' %></p> | |||
122 | </fieldset> |
|
102 | </fieldset> | |
123 |
|
103 | |||
124 | <fieldset class="box" id="custom_field_project_ids"><legend><%= l(:label_project_plural) %></legend> |
|
104 | <fieldset class="box" id="custom_field_project_ids"><legend><%= l(:label_project_plural) %></legend> | |
@@ -128,20 +108,7 when "IssueCustomField" %> | |||||
128 | <%= hidden_field_tag('custom_field[project_ids][]', '', :id => nil) %> |
|
108 | <%= hidden_field_tag('custom_field[project_ids][]', '', :id => nil) %> | |
129 | <p><%= check_all_links 'custom_field_project_ids' %></p> |
|
109 | <p><%= check_all_links 'custom_field_project_ids' %></p> | |
130 | </fieldset> |
|
110 | </fieldset> | |
131 | </div> |
|
|||
132 | <% end %> |
|
111 | <% end %> | |
|
112 | </div> | |||
133 |
|
113 | |||
134 | <% include_calendar_headers_tags %> |
|
114 | <% include_calendar_headers_tags %> | |
135 |
|
||||
136 | <%= javascript_tag do %> |
|
|||
137 | function toggleCustomFieldRoles(){ |
|
|||
138 | var checked = $("#custom_field_visible_on").is(':checked'); |
|
|||
139 | $('.custom_field_role input').attr('disabled', checked); |
|
|||
140 | } |
|
|||
141 | $("#custom_field_visible_on, #custom_field_visible_off").change(toggleCustomFieldRoles); |
|
|||
142 | $(document).ready(toggleCustomFieldRoles); |
|
|||
143 |
|
||||
144 | $("#custom_field_is_for_all").change(function(){ |
|
|||
145 | $("#custom_field_project_ids input").attr("disabled", $(this).is(":checked")); |
|
|||
146 | }).trigger('change'); |
|
|||
147 | <% end %> |
|
@@ -14,7 +14,7 | |||||
14 | <% (@custom_fields_by_type[tab[:name]] || []).sort.each do |custom_field| -%> |
|
14 | <% (@custom_fields_by_type[tab[:name]] || []).sort.each do |custom_field| -%> | |
15 | <tr class="<%= cycle("odd", "even") %>"> |
|
15 | <tr class="<%= cycle("odd", "even") %>"> | |
16 | <td class="name"><%= link_to h(custom_field.name), edit_custom_field_path(custom_field) %></td> |
|
16 | <td class="name"><%= link_to h(custom_field.name), edit_custom_field_path(custom_field) %></td> | |
17 |
<td><%= l( |
|
17 | <td><%= l(custom_field.format.label) %></td> | |
18 | <td><%= checked_image custom_field.is_required? %></td> |
|
18 | <td><%= checked_image custom_field.is_required? %></td> | |
19 | <% if tab[:name] == 'IssueCustomField' %> |
|
19 | <% if tab[:name] == 'IssueCustomField' %> | |
20 | <td><%= checked_image custom_field.is_for_all? %></td> |
|
20 | <td><%= checked_image custom_field.is_for_all? %></td> |
@@ -12,7 +12,8 $('#custom_field_field_format').change(function(){ | |||||
12 | $.ajax({ |
|
12 | $.ajax({ | |
13 | url: '<%= new_custom_field_path(:format => 'js') %>', |
|
13 | url: '<%= new_custom_field_path(:format => 'js') %>', | |
14 | type: 'get', |
|
14 | type: 'get', | |
15 | data: $('#custom_field_form').serialize() |
|
15 | data: $('#custom_field_form').serialize(), | |
|
16 | complete: toggleDisabledInit | |||
16 | }); |
|
17 | }); | |
17 | }); |
|
18 | }); | |
18 | <% end %> |
|
19 | <% end %> |
@@ -93,7 +93,7 | |||||
93 | <% @custom_fields.each do |custom_field| %> |
|
93 | <% @custom_fields.each do |custom_field| %> | |
94 | <p> |
|
94 | <p> | |
95 | <label><%= h(custom_field.name) %></label> |
|
95 | <label><%= h(custom_field.name) %></label> | |
96 |
<%= custom_field_tag_for_bulk_edit('issue', custom_field, @ |
|
96 | <%= custom_field_tag_for_bulk_edit('issue', custom_field, @issues, @issue_params[:custom_field_values][custom_field.id.to_s]) %> | |
97 | </p> |
|
97 | </p> | |
98 | <% end %> |
|
98 | <% end %> | |
99 |
|
99 |
@@ -39,7 +39,7 | |||||
39 | </p> |
|
39 | </p> | |
40 |
|
40 | |||
41 | <% @custom_fields.each do |custom_field| %> |
|
41 | <% @custom_fields.each do |custom_field| %> | |
42 |
<p><label><%= h(custom_field.name) %></label> <%= custom_field_tag_for_bulk_edit('time_entry', custom_field, @ |
|
42 | <p><label><%= h(custom_field.name) %></label> <%= custom_field_tag_for_bulk_edit('time_entry', custom_field, @time_entries) %></p> | |
43 | <% end %> |
|
43 | <% end %> | |
44 |
|
44 | |||
45 | <%= call_hook(:view_time_entries_bulk_edit_details_bottom, { :time_entries => @time_entries }) %> |
|
45 | <%= call_hook(:view_time_entries_bulk_edit_details_bottom, { :time_entries => @time_entries }) %> |
@@ -99,6 +99,7 module Redmine | |||||
99 | cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil) |
|
99 | cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil) | |
100 | x.value = cv.value |
|
100 | x.value = cv.value | |
101 | end |
|
101 | end | |
|
102 | x.value_was = x.value.dup if x.value | |||
102 | x |
|
103 | x | |
103 | end |
|
104 | end | |
104 | end |
|
105 | end |
@@ -30,7 +30,6 require 'redmine/activity' | |||||
30 | require 'redmine/activity/fetcher' |
|
30 | require 'redmine/activity/fetcher' | |
31 | require 'redmine/ciphering' |
|
31 | require 'redmine/ciphering' | |
32 | require 'redmine/codeset_util' |
|
32 | require 'redmine/codeset_util' | |
33 | require 'redmine/custom_field_format' |
|
|||
34 | require 'redmine/i18n' |
|
33 | require 'redmine/i18n' | |
35 | require 'redmine/menu_manager' |
|
34 | require 'redmine/menu_manager' | |
36 | require 'redmine/notifiable' |
|
35 | require 'redmine/notifiable' | |
@@ -73,18 +72,6 Redmine::Scm::Base.add "Bazaar" | |||||
73 | Redmine::Scm::Base.add "Git" |
|
72 | Redmine::Scm::Base.add "Git" | |
74 | Redmine::Scm::Base.add "Filesystem" |
|
73 | Redmine::Scm::Base.add "Filesystem" | |
75 |
|
74 | |||
76 | Redmine::CustomFieldFormat.map do |fields| |
|
|||
77 | fields.register 'string' |
|
|||
78 | fields.register 'text' |
|
|||
79 | fields.register 'int', :label => :label_integer |
|
|||
80 | fields.register 'float' |
|
|||
81 | fields.register 'list' |
|
|||
82 | fields.register 'date' |
|
|||
83 | fields.register 'bool', :label => :label_boolean |
|
|||
84 | fields.register 'user', :only => %w(Issue TimeEntry Version Project), :edit_as => 'list' |
|
|||
85 | fields.register 'version', :only => %w(Issue TimeEntry Version Project), :edit_as => 'list' |
|
|||
86 | end |
|
|||
87 |
|
||||
88 | # Permissions |
|
75 | # Permissions | |
89 | Redmine::AccessControl.map do |map| |
|
76 | Redmine::AccessControl.map do |map| | |
90 | map.permission :view_project, {:projects => [:show], :activities => [:index]}, :public => true, :read => true |
|
77 | map.permission :view_project, {:projects => [:show], :activities => [:index]}, :public => true, :read => true |
@@ -257,7 +257,7 module Redmine | |||||
257 | query.inline_columns.collect do |column| |
|
257 | query.inline_columns.collect do |column| | |
258 | s = if column.is_a?(QueryCustomFieldColumn) |
|
258 | s = if column.is_a?(QueryCustomFieldColumn) | |
259 | cv = issue.visible_custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id} |
|
259 | cv = issue.visible_custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id} | |
260 | show_value(cv) |
|
260 | show_value(cv, false) | |
261 | else |
|
261 | else | |
262 | value = issue.send(column.name) |
|
262 | value = issue.send(column.name) | |
263 | if column.name == :subject |
|
263 | if column.name == :subject | |
@@ -573,7 +573,7 module Redmine | |||||
573 |
|
573 | |||
574 | half = (issue.visible_custom_field_values.size / 2.0).ceil |
|
574 | half = (issue.visible_custom_field_values.size / 2.0).ceil | |
575 | issue.visible_custom_field_values.each_with_index do |custom_value, i| |
|
575 | issue.visible_custom_field_values.each_with_index do |custom_value, i| | |
576 | (i < half ? left : right) << [custom_value.custom_field.name, show_value(custom_value)] |
|
576 | (i < half ? left : right) << [custom_value.custom_field.name, show_value(custom_value, false)] | |
577 | end |
|
577 | end | |
578 |
|
578 | |||
579 | rows = left.size > right.size ? left.size : right.size |
|
579 | rows = left.size > right.size ? left.size : right.size |
@@ -138,9 +138,10 module Redmine | |||||
138 |
|
138 | |||
139 | # Add list and boolean custom fields as available criteria |
|
139 | # Add list and boolean custom fields as available criteria | |
140 | custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf| |
|
140 | custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
141 |
@available_criteria["cf_#{cf.id}"] = {:sql => |
|
141 | @available_criteria["cf_#{cf.id}"] = {:sql => cf.group_statement, | |
142 | :joins => cf.join_for_order_statement, |
|
142 | :joins => cf.join_for_order_statement, | |
143 | :format => cf.field_format, |
|
143 | :format => cf.field_format, | |
|
144 | :custom_field => cf, | |||
144 | :label => cf.name} |
|
145 | :label => cf.name} | |
145 | end |
|
146 | end | |
146 |
|
147 |
@@ -20,7 +20,7 require 'action_view/helpers/form_helper' | |||||
20 | class Redmine::Views::LabelledFormBuilder < ActionView::Helpers::FormBuilder |
|
20 | class Redmine::Views::LabelledFormBuilder < ActionView::Helpers::FormBuilder | |
21 | include Redmine::I18n |
|
21 | include Redmine::I18n | |
22 |
|
22 | |||
23 | (field_helpers.map(&:to_s) - %w(radio_button hidden_field fields_for) + |
|
23 | (field_helpers.map(&:to_s) - %w(radio_button hidden_field fields_for check_box) + | |
24 | %w(date_select)).each do |selector| |
|
24 | %w(date_select)).each do |selector| | |
25 | src = <<-END_SRC |
|
25 | src = <<-END_SRC | |
26 | def #{selector}(field, options = {}) |
|
26 | def #{selector}(field, options = {}) | |
@@ -30,6 +30,10 class Redmine::Views::LabelledFormBuilder < ActionView::Helpers::FormBuilder | |||||
30 | class_eval src, __FILE__, __LINE__ |
|
30 | class_eval src, __FILE__, __LINE__ | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
|
33 | def check_box(field, options={}, checked_value="1", unchecked_value="0") | |||
|
34 | label_for_field(field, options) + super(field, options.except(:label), checked_value, unchecked_value).html_safe | |||
|
35 | end | |||
|
36 | ||||
33 | def select(field, choices, options = {}, html_options = {}) |
|
37 | def select(field, choices, options = {}, html_options = {}) | |
34 | label_for_field(field, options) + super(field, choices, options, html_options.except(:label)).html_safe |
|
38 | label_for_field(field, options) + super(field, choices, options, html_options.except(:label)).html_safe | |
35 | end |
|
39 | end |
@@ -588,6 +588,20 function blockEventPropagation(event) { | |||||
588 | event.preventDefault(); |
|
588 | event.preventDefault(); | |
589 | } |
|
589 | } | |
590 |
|
590 | |||
|
591 | function toggleDisabledOnChange() { | |||
|
592 | var checked = $(this).is(':checked'); | |||
|
593 | $($(this).data('disables')).attr('disabled', checked); | |||
|
594 | $($(this).data('enables')).attr('disabled', !checked); | |||
|
595 | } | |||
|
596 | function toggleDisabledInit() { | |||
|
597 | $('input[data-disables], input[data-enables]').each(toggleDisabledOnChange); | |||
|
598 | } | |||
|
599 | $(document).ready(function(){ | |||
|
600 | $('#content').on('change', 'input[data-disables], input[data-enables]', toggleDisabledOnChange); | |||
|
601 | toggleDisabledInit(); | |||
|
602 | }); | |||
|
603 | ||||
591 | $(document).ready(setupAjaxIndicator); |
|
604 | $(document).ready(setupAjaxIndicator); | |
592 | $(document).ready(hideOnLoad); |
|
605 | $(document).ready(hideOnLoad); | |
593 | $(document).ready(addFormObserversForDoubleSubmit); |
|
606 | $(document).ready(addFormObserversForDoubleSubmit); | |
|
607 |
@@ -457,8 +457,8 table.fields_permissions select {font-size:90%} | |||||
457 | table.fields_permissions td.readonly {background:#ddd;} |
|
457 | table.fields_permissions td.readonly {background:#ddd;} | |
458 | table.fields_permissions td.required {background:#d88;} |
|
458 | table.fields_permissions td.required {background:#d88;} | |
459 |
|
459 | |||
460 |
textarea#custom_field_possible_values {width: 9 |
|
460 | textarea#custom_field_possible_values {width: 95%; resize:vertical} | |
461 |
textarea#custom_field_default_value {width: 9 |
|
461 | textarea#custom_field_default_value {width: 95%; resize:vertical} | |
462 |
|
462 | |||
463 | input#content_comments {width: 99%} |
|
463 | input#content_comments {width: 99%} | |
464 |
|
464 | |||
@@ -475,6 +475,10 p.pagination {margin-top:8px; font-size: 90%} | |||||
475 |
|
475 | |||
476 | html>body .tabular p {overflow:hidden;} |
|
476 | html>body .tabular p {overflow:hidden;} | |
477 |
|
477 | |||
|
478 | .tabular input, .tabular select {max-width:95%} | |||
|
479 | .tabular textarea {width:95%; resize:vertical;} | |||
|
480 | .tabular span[title] {border-bottom:1px dotted #aaa;} | |||
|
481 | ||||
478 | .tabular label{ |
|
482 | .tabular label{ | |
479 | font-weight: bold; |
|
483 | font-weight: bold; | |
480 | float: left; |
|
484 | float: left; | |
@@ -532,6 +536,27 fieldset#notified_events .parent { padding-left: 20px; } | |||||
532 | span.required {color: #bb0000;} |
|
536 | span.required {color: #bb0000;} | |
533 | .summary {font-style: italic;} |
|
537 | .summary {font-style: italic;} | |
534 |
|
538 | |||
|
539 | .check_box_group { | |||
|
540 | display:block; | |||
|
541 | width:95%; | |||
|
542 | max-height:300px; | |||
|
543 | overflow-y:auto; | |||
|
544 | padding:2px 4px 4px 2px; | |||
|
545 | background:#fff; | |||
|
546 | border:1px solid #9EB1C2; | |||
|
547 | border-radius:2px | |||
|
548 | } | |||
|
549 | .check_box_group label { | |||
|
550 | font-weight: normal; | |||
|
551 | margin-left: 0px !important; | |||
|
552 | text-align: left; | |||
|
553 | float: none; | |||
|
554 | display: block; | |||
|
555 | width: auto; | |||
|
556 | } | |||
|
557 | .check_box_group.bool_cf {border:0; background:inherit;} | |||
|
558 | .check_box_group.bool_cf label {display: inline;} | |||
|
559 | ||||
535 | #attachments_fields input.description {margin-left:4px; width:340px;} |
|
560 | #attachments_fields input.description {margin-left:4px; width:340px;} | |
536 | #attachments_fields span {display:block; white-space:nowrap;} |
|
561 | #attachments_fields span {display:block; white-space:nowrap;} | |
537 | #attachments_fields input.filename {border:0; height:1.8em; width:250px; color:#555; background-color:inherit; background:url(../images/attachment.png) no-repeat 1px 50%; padding-left:18px;} |
|
562 | #attachments_fields input.filename {border:0; height:1.8em; width:250px; color:#555; background-color:inherit; background:url(../images/attachment.png) no-repeat 1px 50%; padding-left:18px;} | |
@@ -560,7 +585,9 a.atom { background: url(../images/feed.png) no-repeat 1px 50%; padding: 2px 0px | |||||
560 | em.info {font-style:normal;font-size:90%;color:#888;display:block;} |
|
585 | em.info {font-style:normal;font-size:90%;color:#888;display:block;} | |
561 | em.info.error {padding-left:20px; background:url(../images/exclamation.png) no-repeat 0 50%;} |
|
586 | em.info.error {padding-left:20px; background:url(../images/exclamation.png) no-repeat 0 50%;} | |
562 |
|
587 | |||
563 |
textarea.text_cf {width:9 |
|
588 | textarea.text_cf {width:95%; resize:vertical;} | |
|
589 | input.string_cf, input.link_cf {width:95%;} | |||
|
590 | select.bool_cf {width:auto !important;} | |||
564 |
|
591 | |||
565 | #tab-content-modules fieldset p {margin:3px 0 4px 0;} |
|
592 | #tab-content-modules fieldset p {margin:3px 0 4px 0;} | |
566 |
|
593 |
@@ -30,19 +30,28 class CustomFieldsControllerTest < ActionController::TestCase | |||||
30 | assert_template 'index' |
|
30 | assert_template 'index' | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | def test_new |
|
33 | def test_new_should_work_for_each_customized_class_and_format | |
34 | custom_field_classes.each do |klass| |
|
34 | custom_field_classes.each do |klass| | |
35 | get :new, :type => klass.name |
|
35 | Redmine::FieldFormat.available_formats.each do |format_name| | |
36 | assert_response :success |
|
36 | get :new, :type => klass.name, :custom_field => {:field_format => format_name} | |
37 |
assert_ |
|
37 | assert_response :success | |
38 | assert_kind_of klass, assigns(:custom_field) |
|
38 | assert_template 'new' | |
39 | assert_select 'form#custom_field_form' do |
|
39 | assert_kind_of klass, assigns(:custom_field) | |
40 | assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' |
|
40 | assert_equal format_name, assigns(:custom_field).format.name | |
41 | assert_select 'input[type=hidden][name=type][value=?]', klass.name |
|
41 | assert_select 'form#custom_field_form' do | |
|
42 | assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' | |||
|
43 | assert_select 'input[type=hidden][name=type][value=?]', klass.name | |||
|
44 | end | |||
42 | end |
|
45 | end | |
43 | end |
|
46 | end | |
44 | end |
|
47 | end | |
45 |
|
48 | |||
|
49 | def test_new_should_have_string_default_format | |||
|
50 | get :new, :type => 'IssueCustomField' | |||
|
51 | assert_response :success | |||
|
52 | assert_equal 'string', assigns(:custom_field).format.name | |||
|
53 | end | |||
|
54 | ||||
46 | def test_new_issue_custom_field |
|
55 | def test_new_issue_custom_field | |
47 | get :new, :type => 'IssueCustomField' |
|
56 | get :new, :type => 'IssueCustomField' | |
48 | assert_response :success |
|
57 | assert_response :success | |
@@ -83,7 +92,9 class CustomFieldsControllerTest < ActionController::TestCase | |||||
83 | def test_default_value_should_be_a_checkbox_for_bool_custom_field |
|
92 | def test_default_value_should_be_a_checkbox_for_bool_custom_field | |
84 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'} |
|
93 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'} | |
85 | assert_response :success |
|
94 | assert_response :success | |
86 |
assert_select ' |
|
95 | assert_select 'select[name=?]', 'custom_field[default_value]' do | |
|
96 | assert_select 'option', 3 | |||
|
97 | end | |||
87 | end |
|
98 | end | |
88 |
|
99 | |||
89 | def test_default_value_should_not_be_present_for_user_custom_field |
|
100 | def test_default_value_should_not_be_present_for_user_custom_field |
@@ -1361,8 +1361,10 class IssuesControllerTest < ActionController::TestCase | |||||
1361 | get :show, :id => 1 |
|
1361 | get :show, :id => 1 | |
1362 | assert_response :success |
|
1362 | assert_response :success | |
1363 |
|
1363 | |||
1364 | # TODO: should display links |
|
1364 | assert_select "td.cf_#{field.id}", :text => 'Dave Lopper, John Smith' do | |
1365 |
assert_select ' |
|
1365 | assert_select 'a', :text => 'Dave Lopper' | |
|
1366 | assert_select 'a', :text => 'John Smith' | |||
|
1367 | end | |||
1366 | end |
|
1368 | end | |
1367 |
|
1369 | |||
1368 | def test_show_should_display_private_notes_with_permission_only |
|
1370 | def test_show_should_display_private_notes_with_permission_only |
@@ -63,12 +63,12 class CustomFieldTest < ActiveSupport::TestCase | |||||
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | def test_field_format_validation_should_accept_formats_added_at_runtime |
|
65 | def test_field_format_validation_should_accept_formats_added_at_runtime | |
66 | Redmine::CustomFieldFormat.register 'foobar' |
|
66 | Redmine::FieldFormat.add 'foobar', Class.new(Redmine::FieldFormat::Base) | |
67 |
|
67 | |||
68 | field = CustomField.new(:name => 'Some Custom Field', :field_format => 'foobar') |
|
68 | field = CustomField.new(:name => 'Some Custom Field', :field_format => 'foobar') | |
69 | assert field.valid?, 'field should be valid' |
|
69 | assert field.valid?, 'field should be valid' | |
70 | ensure |
|
70 | ensure | |
71 |
Redmine:: |
|
71 | Redmine::FieldFormat.delete 'foobar' | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def test_should_not_change_field_format_of_existing_custom_field |
|
74 | def test_should_not_change_field_format_of_existing_custom_field | |
@@ -293,4 +293,18 class CustomFieldTest < ActiveSupport::TestCase | |||||
293 |
|
293 | |||
294 | assert_equal [fields[0]], CustomField.visible(User.anonymous).order("id").to_a |
|
294 | assert_equal [fields[0]], CustomField.visible(User.anonymous).order("id").to_a | |
295 | end |
|
295 | end | |
|
296 | ||||
|
297 | def test_float_cast_blank_value_should_return_nil | |||
|
298 | field = CustomField.new(:field_format => 'float') | |||
|
299 | assert_equal nil, field.cast_value(nil) | |||
|
300 | assert_equal nil, field.cast_value('') | |||
|
301 | end | |||
|
302 | ||||
|
303 | def test_float_cast_valid_value_should_return_float | |||
|
304 | field = CustomField.new(:field_format => 'float') | |||
|
305 | assert_equal 12.0, field.cast_value('12') | |||
|
306 | assert_equal 12.5, field.cast_value('12.5') | |||
|
307 | assert_equal 12.5, field.cast_value('+12.5') | |||
|
308 | assert_equal -12.5, field.cast_value('-12.5') | |||
|
309 | end | |||
296 | end |
|
310 | end |
@@ -24,23 +24,6 class CustomFieldUserFormatTest < ActiveSupport::TestCase | |||||
24 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user') |
|
24 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user') | |
25 | end |
|
25 | end | |
26 |
|
26 | |||
27 | def test_possible_values_with_no_arguments |
|
|||
28 | assert_equal [], @field.possible_values |
|
|||
29 | assert_equal [], @field.possible_values(nil) |
|
|||
30 | end |
|
|||
31 |
|
||||
32 | def test_possible_values_with_project_resource |
|
|||
33 | project = Project.find(1) |
|
|||
34 | possible_values = @field.possible_values(project.issues.first) |
|
|||
35 | assert possible_values.any? |
|
|||
36 | assert_equal project.users.sort.collect(&:id).map(&:to_s), possible_values |
|
|||
37 | end |
|
|||
38 |
|
||||
39 | def test_possible_values_with_nil_project_resource |
|
|||
40 | project = Project.find(1) |
|
|||
41 | assert_equal [], @field.possible_values(Issue.new) |
|
|||
42 | end |
|
|||
43 |
|
||||
44 | def test_possible_values_options_with_no_arguments |
|
27 | def test_possible_values_options_with_no_arguments | |
45 | assert_equal [], @field.possible_values_options |
|
28 | assert_equal [], @field.possible_values_options | |
46 | assert_equal [], @field.possible_values_options(nil) |
|
29 | assert_equal [], @field.possible_values_options(nil) |
@@ -24,22 +24,6 class CustomFieldVersionFormatTest < ActiveSupport::TestCase | |||||
24 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'version') |
|
24 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'version') | |
25 | end |
|
25 | end | |
26 |
|
26 | |||
27 | def test_possible_values_with_no_arguments |
|
|||
28 | assert_equal [], @field.possible_values |
|
|||
29 | assert_equal [], @field.possible_values(nil) |
|
|||
30 | end |
|
|||
31 |
|
||||
32 | def test_possible_values_with_project_resource |
|
|||
33 | project = Project.find(1) |
|
|||
34 | possible_values = @field.possible_values(project.issues.first) |
|
|||
35 | assert possible_values.any? |
|
|||
36 | assert_equal project.shared_versions.sort.collect(&:id).map(&:to_s), possible_values |
|
|||
37 | end |
|
|||
38 |
|
||||
39 | def test_possible_values_with_nil_project_resource |
|
|||
40 | assert_equal [], @field.possible_values(Issue.new) |
|
|||
41 | end |
|
|||
42 |
|
||||
43 | def test_possible_values_options_with_no_arguments |
|
27 | def test_possible_values_options_with_no_arguments | |
44 | assert_equal [], @field.possible_values_options |
|
28 | assert_equal [], @field.possible_values_options | |
45 | assert_equal [], @field.possible_values_options(nil) |
|
29 | assert_equal [], @field.possible_values_options(nil) |
@@ -18,14 +18,15 | |||||
18 | require File.expand_path('../../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class CustomFieldsHelperTest < ActionView::TestCase |
|
20 | class CustomFieldsHelperTest < ActionView::TestCase | |
|
21 | include ApplicationHelper | |||
21 | include CustomFieldsHelper |
|
22 | include CustomFieldsHelper | |
22 | include Redmine::I18n |
|
23 | include Redmine::I18n | |
23 | include ERB::Util |
|
24 | include ERB::Util | |
24 |
|
25 | |||
25 | def test_format_boolean_value |
|
26 | def test_format_boolean_value | |
26 | I18n.locale = 'en' |
|
27 | I18n.locale = 'en' | |
27 | assert_equal 'Yes', format_value('1', 'bool') |
|
28 | assert_equal 'Yes', format_value('1', CustomField.new(:field_format => 'bool')) | |
28 | assert_equal 'No', format_value('0', 'bool') |
|
29 | assert_equal 'No', format_value('0', CustomField.new(:field_format => 'bool')) | |
29 | end |
|
30 | end | |
30 |
|
31 | |||
31 | def test_unknow_field_format_should_be_edited_as_string |
|
32 | def test_unknow_field_format_should_be_edited_as_string |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now