##// END OF EJS Templates
Removed hardcoded formats for right-click edit....
Jean-Philippe Lang -
r12127:ebb8e8612254
parent child
Show More
@@ -1,86 +1,84
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class ContextMenusController < ApplicationController
18 class ContextMenusController < ApplicationController
19 helper :watchers
19 helper :watchers
20 helper :issues
20 helper :issues
21
21
22 before_filter :find_issues, :only => :issues
22 before_filter :find_issues, :only => :issues
23
23
24 def issues
24 def issues
25 if (@issues.size == 1)
25 if (@issues.size == 1)
26 @issue = @issues.first
26 @issue = @issues.first
27 end
27 end
28 @issue_ids = @issues.map(&:id).sort
28 @issue_ids = @issues.map(&:id).sort
29
29
30 @allowed_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
30 @allowed_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
31
31
32 @can = {:edit => User.current.allowed_to?(:edit_issues, @projects),
32 @can = {:edit => User.current.allowed_to?(:edit_issues, @projects),
33 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
33 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
34 :update => (User.current.allowed_to?(:edit_issues, @projects) || (User.current.allowed_to?(:change_status, @projects) && !@allowed_statuses.blank?)),
34 :update => (User.current.allowed_to?(:edit_issues, @projects) || (User.current.allowed_to?(:change_status, @projects) && !@allowed_statuses.blank?)),
35 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
35 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
36 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
36 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
37 :delete => User.current.allowed_to?(:delete_issues, @projects)
37 :delete => User.current.allowed_to?(:delete_issues, @projects)
38 }
38 }
39 if @project
39 if @project
40 if @issue
40 if @issue
41 @assignables = @issue.assignable_users
41 @assignables = @issue.assignable_users
42 else
42 else
43 @assignables = @project.assignable_users
43 @assignables = @project.assignable_users
44 end
44 end
45 @trackers = @project.trackers
45 @trackers = @project.trackers
46 else
46 else
47 #when multiple projects, we only keep the intersection of each set
47 #when multiple projects, we only keep the intersection of each set
48 @assignables = @projects.map(&:assignable_users).reduce(:&)
48 @assignables = @projects.map(&:assignable_users).reduce(:&)
49 @trackers = @projects.map(&:trackers).reduce(:&)
49 @trackers = @projects.map(&:trackers).reduce(:&)
50 end
50 end
51 @versions = @projects.map {|p| p.shared_versions.open}.reduce(:&)
51 @versions = @projects.map {|p| p.shared_versions.open}.reduce(:&)
52
52
53 @priorities = IssuePriority.active.reverse
53 @priorities = IssuePriority.active.reverse
54 @back = back_url
54 @back = back_url
55
55
56 @options_by_custom_field = {}
56 @options_by_custom_field = {}
57 if @can[:edit]
57 if @can[:edit]
58 custom_fields = @issues.map(&:available_custom_fields).reduce(:&).select do |f|
58 custom_fields = @issues.map(&:available_custom_fields).reduce(:&).reject(&:multiple?)
59 %w(bool list user version).include?(f.field_format) && !f.multiple?
60 end
61 custom_fields.each do |field|
59 custom_fields.each do |field|
62 values = field.possible_values_options(@projects)
60 values = field.possible_values_options(@projects)
63 if values.any?
61 if values.present?
64 @options_by_custom_field[field] = values
62 @options_by_custom_field[field] = values
65 end
63 end
66 end
64 end
67 end
65 end
68
66
69 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
67 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
70 render :layout => false
68 render :layout => false
71 end
69 end
72
70
73 def time_entries
71 def time_entries
74 @time_entries = TimeEntry.where(:id => params[:ids]).preload(:project).to_a
72 @time_entries = TimeEntry.where(:id => params[:ids]).preload(:project).to_a
75 (render_404; return) unless @time_entries.present?
73 (render_404; return) unless @time_entries.present?
76
74
77 @projects = @time_entries.collect(&:project).compact.uniq
75 @projects = @time_entries.collect(&:project).compact.uniq
78 @project = @projects.first if @projects.size == 1
76 @project = @projects.first if @projects.size == 1
79 @activities = TimeEntryActivity.shared.active
77 @activities = TimeEntryActivity.shared.active
80 @can = {:edit => User.current.allowed_to?(:edit_time_entries, @projects),
78 @can = {:edit => User.current.allowed_to?(:edit_time_entries, @projects),
81 :delete => User.current.allowed_to?(:edit_time_entries, @projects)
79 :delete => User.current.allowed_to?(:edit_time_entries, @projects)
82 }
80 }
83 @back = back_url
81 @back = back_url
84 render :layout => false
82 render :layout => false
85 end
83 end
86 end
84 end
@@ -1,646 +1,650
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module Redmine
18 module Redmine
19 module FieldFormat
19 module FieldFormat
20 def self.add(name, klass)
20 def self.add(name, klass)
21 all[name.to_s] = klass.instance
21 all[name.to_s] = klass.instance
22 end
22 end
23
23
24 def self.delete(name)
24 def self.delete(name)
25 all.delete(name.to_s)
25 all.delete(name.to_s)
26 end
26 end
27
27
28 def self.all
28 def self.all
29 @formats ||= Hash.new(Base.instance)
29 @formats ||= Hash.new(Base.instance)
30 end
30 end
31
31
32 def self.available_formats
32 def self.available_formats
33 all.keys
33 all.keys
34 end
34 end
35
35
36 def self.find(name)
36 def self.find(name)
37 all[name.to_s]
37 all[name.to_s]
38 end
38 end
39
39
40 # Return an array of custom field formats which can be used in select_tag
40 # Return an array of custom field formats which can be used in select_tag
41 def self.as_select(class_name=nil)
41 def self.as_select(class_name=nil)
42 formats = all.values
42 formats = all.values
43 formats.select! do |format|
43 formats.select! do |format|
44 format.class.customized_class_names.nil? || format.class.customized_class_names.include?(class_name)
44 format.class.customized_class_names.nil? || format.class.customized_class_names.include?(class_name)
45 end
45 end
46 formats.map {|format| [::I18n.t(format.label), format.name] }.sort_by(&:first)
46 formats.map {|format| [::I18n.t(format.label), format.name] }.sort_by(&:first)
47 end
47 end
48
48
49 class Base
49 class Base
50 include Singleton
50 include Singleton
51 include Redmine::I18n
51 include Redmine::I18n
52 include ERB::Util
52 include ERB::Util
53
53
54 class_attribute :format_name
54 class_attribute :format_name
55 self.format_name = nil
55 self.format_name = nil
56
56
57 # Set this to true if the format supports multiple values
57 # Set this to true if the format supports multiple values
58 class_attribute :multiple_supported
58 class_attribute :multiple_supported
59 self.multiple_supported = false
59 self.multiple_supported = false
60
60
61 # Set this to true if the format supports textual search on custom values
61 # Set this to true if the format supports textual search on custom values
62 class_attribute :searchable_supported
62 class_attribute :searchable_supported
63 self.searchable_supported = false
63 self.searchable_supported = false
64
64
65 # Restricts the classes that the custom field can be added to
65 # Restricts the classes that the custom field can be added to
66 # Set to nil for no restrictions
66 # Set to nil for no restrictions
67 class_attribute :customized_class_names
67 class_attribute :customized_class_names
68 self.customized_class_names = nil
68 self.customized_class_names = nil
69
69
70 # Name of the partial for editing the custom field
70 # Name of the partial for editing the custom field
71 class_attribute :form_partial
71 class_attribute :form_partial
72 self.form_partial = nil
72 self.form_partial = nil
73
73
74 def self.add(name)
74 def self.add(name)
75 self.format_name = name
75 self.format_name = name
76 Redmine::FieldFormat.add(name, self)
76 Redmine::FieldFormat.add(name, self)
77 end
77 end
78 private_class_method :add
78 private_class_method :add
79
79
80 def self.field_attributes(*args)
80 def self.field_attributes(*args)
81 CustomField.store_accessor :format_store, *args
81 CustomField.store_accessor :format_store, *args
82 end
82 end
83
83
84 def name
84 def name
85 self.class.format_name
85 self.class.format_name
86 end
86 end
87
87
88 def label
88 def label
89 "label_#{name}"
89 "label_#{name}"
90 end
90 end
91
91
92 def cast_custom_value(custom_value)
92 def cast_custom_value(custom_value)
93 cast_value(custom_value.custom_field, custom_value.value, custom_value.customized)
93 cast_value(custom_value.custom_field, custom_value.value, custom_value.customized)
94 end
94 end
95
95
96 def cast_value(custom_field, value, customized=nil)
96 def cast_value(custom_field, value, customized=nil)
97 if value.blank?
97 if value.blank?
98 nil
98 nil
99 elsif value.is_a?(Array)
99 elsif value.is_a?(Array)
100 value.map do |v|
100 value.map do |v|
101 cast_single_value(custom_field, v, customized)
101 cast_single_value(custom_field, v, customized)
102 end.sort
102 end.sort
103 else
103 else
104 cast_single_value(custom_field, value, customized)
104 cast_single_value(custom_field, value, customized)
105 end
105 end
106 end
106 end
107
107
108 def cast_single_value(custom_field, value, customized=nil)
108 def cast_single_value(custom_field, value, customized=nil)
109 value.to_s
109 value.to_s
110 end
110 end
111
111
112 def target_class
112 def target_class
113 nil
113 nil
114 end
114 end
115
115
116 def possible_custom_value_options(custom_value)
116 def possible_custom_value_options(custom_value)
117 possible_values_options(custom_value.custom_field, custom_value.customized)
117 possible_values_options(custom_value.custom_field, custom_value.customized)
118 end
118 end
119
119
120 def possible_values_options(custom_field, object=nil)
120 def possible_values_options(custom_field, object=nil)
121 custom_field.possible_values
121 []
122 end
122 end
123
123
124 # Returns the validation errors for custom_field
124 # Returns the validation errors for custom_field
125 # Should return an empty array if custom_field is valid
125 # Should return an empty array if custom_field is valid
126 def validate_custom_field(custom_field)
126 def validate_custom_field(custom_field)
127 []
127 []
128 end
128 end
129
129
130 # Returns the validation error messages for custom_value
130 # Returns the validation error messages for custom_value
131 # Should return an empty array if custom_value is valid
131 # Should return an empty array if custom_value is valid
132 def validate_custom_value(custom_value)
132 def validate_custom_value(custom_value)
133 errors = Array.wrap(custom_value.value).reject(&:blank?).map do |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)
134 validate_single_value(custom_value.custom_field, value, custom_value.customized)
135 end
135 end
136 errors.flatten.uniq
136 errors.flatten.uniq
137 end
137 end
138
138
139 def validate_single_value(custom_field, value, customized=nil)
139 def validate_single_value(custom_field, value, customized=nil)
140 []
140 []
141 end
141 end
142
142
143 def formatted_custom_value(view, custom_value, html=false)
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)
144 formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html)
145 end
145 end
146
146
147 def formatted_value(view, custom_field, value, customized=nil, html=false)
147 def formatted_value(view, custom_field, value, customized=nil, html=false)
148 cast_value(custom_field, value, customized)
148 cast_value(custom_field, value, customized)
149 end
149 end
150
150
151 def edit_tag(view, tag_id, tag_name, custom_value, options={})
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))
152 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id))
153 end
153 end
154
154
155 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
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)) +
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)
157 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
158 end
158 end
159
159
160 def bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
160 def bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
161 if custom_field.is_required?
161 if custom_field.is_required?
162 ''.html_safe
162 ''.html_safe
163 else
163 else
164 view.content_tag('label',
164 view.content_tag('label',
165 view.check_box_tag(tag_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{tag_id}"}) + l(:button_clear),
165 view.check_box_tag(tag_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{tag_id}"}) + l(:button_clear),
166 :class => 'inline'
166 :class => 'inline'
167 )
167 )
168 end
168 end
169 end
169 end
170 protected :bulk_clear_tag
170 protected :bulk_clear_tag
171
171
172 def query_filter_options(custom_field, query)
172 def query_filter_options(custom_field, query)
173 {:type => :string}
173 {:type => :string}
174 end
174 end
175
175
176 def before_custom_field_save(custom_field)
176 def before_custom_field_save(custom_field)
177 end
177 end
178
178
179 # Returns a ORDER BY clause that can used to sort customized
179 # Returns a ORDER BY clause that can used to sort customized
180 # objects by their value of the custom field.
180 # objects by their value of the custom field.
181 # Returns nil if the custom field can not be used for sorting.
181 # Returns nil if the custom field can not be used for sorting.
182 def order_statement(custom_field)
182 def order_statement(custom_field)
183 # COALESCE is here to make sure that blank and NULL values are sorted equally
183 # COALESCE is here to make sure that blank and NULL values are sorted equally
184 "COALESCE(#{join_alias custom_field}.value, '')"
184 "COALESCE(#{join_alias custom_field}.value, '')"
185 end
185 end
186
186
187 # Returns a GROUP BY clause that can used to group by custom value
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.
188 # Returns nil if the custom field can not be used for grouping.
189 def group_statement(custom_field)
189 def group_statement(custom_field)
190 nil
190 nil
191 end
191 end
192
192
193 # Returns a JOIN clause that is added to the query when sorting by custom values
193 # Returns a JOIN clause that is added to the query when sorting by custom values
194 def join_for_order_statement(custom_field)
194 def join_for_order_statement(custom_field)
195 alias_name = join_alias(custom_field)
195 alias_name = join_alias(custom_field)
196
196
197 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
197 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
198 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.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" +
199 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
200 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
200 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
201 " AND (#{custom_field.visibility_by_project_condition})" +
201 " AND (#{custom_field.visibility_by_project_condition})" +
202 " AND #{alias_name}.value <> ''" +
202 " AND #{alias_name}.value <> ''" +
203 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
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" +
204 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
205 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
205 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
206 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)"
206 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)"
207 end
207 end
208
208
209 def join_alias(custom_field)
209 def join_alias(custom_field)
210 "cf_#{custom_field.id}"
210 "cf_#{custom_field.id}"
211 end
211 end
212 protected :join_alias
212 protected :join_alias
213 end
213 end
214
214
215 class Unbounded < Base
215 class Unbounded < Base
216 def validate_single_value(custom_field, value, customized=nil)
216 def validate_single_value(custom_field, value, customized=nil)
217 errs = super
217 errs = super
218 if value.present?
218 if value.present?
219 unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
219 unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
220 errs << ::I18n.t('activerecord.errors.messages.invalid')
220 errs << ::I18n.t('activerecord.errors.messages.invalid')
221 end
221 end
222 if custom_field.min_length > 0 and value.length < custom_field.min_length
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)
223 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => custom_field.min_length)
224 end
224 end
225 if custom_field.max_length > 0 and value.length > custom_field.max_length
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)
226 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => custom_field.max_length)
227 end
227 end
228 end
228 end
229 errs
229 errs
230 end
230 end
231 end
231 end
232
232
233 class StringFormat < Unbounded
233 class StringFormat < Unbounded
234 add 'string'
234 add 'string'
235 self.searchable_supported = true
235 self.searchable_supported = true
236 self.form_partial = 'custom_fields/formats/string'
236 self.form_partial = 'custom_fields/formats/string'
237 field_attributes :text_formatting
237 field_attributes :text_formatting
238
238
239 def formatted_value(view, custom_field, value, customized=nil, html=false)
239 def formatted_value(view, custom_field, value, customized=nil, html=false)
240 if html && custom_field.text_formatting == 'full'
240 if html && custom_field.text_formatting == 'full'
241 view.textilizable(value, :object => customized)
241 view.textilizable(value, :object => customized)
242 else
242 else
243 value.to_s
243 value.to_s
244 end
244 end
245 end
245 end
246 end
246 end
247
247
248 class TextFormat < Unbounded
248 class TextFormat < Unbounded
249 add 'text'
249 add 'text'
250 self.searchable_supported = true
250 self.searchable_supported = true
251 self.form_partial = 'custom_fields/formats/text'
251 self.form_partial = 'custom_fields/formats/text'
252
252
253 def formatted_value(view, custom_field, value, customized=nil, html=false)
253 def formatted_value(view, custom_field, value, customized=nil, html=false)
254 if html
254 if html
255 if custom_field.text_formatting == 'full'
255 if custom_field.text_formatting == 'full'
256 view.textilizable(value, :object => customized)
256 view.textilizable(value, :object => customized)
257 else
257 else
258 view.simple_format(html_escape(value))
258 view.simple_format(html_escape(value))
259 end
259 end
260 else
260 else
261 value.to_s
261 value.to_s
262 end
262 end
263 end
263 end
264
264
265 def edit_tag(view, tag_id, tag_name, custom_value, options={})
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))
266 view.text_area_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :rows => 3))
267 end
267 end
268
268
269 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
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)) +
270 view.text_area_tag(tag_name, value, options.merge(:id => tag_id, :rows => 3)) +
271 '<br />'.html_safe +
271 '<br />'.html_safe +
272 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
272 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
273 end
273 end
274
274
275 def query_filter_options(custom_field, query)
275 def query_filter_options(custom_field, query)
276 {:type => :text}
276 {:type => :text}
277 end
277 end
278 end
278 end
279
279
280 class LinkFormat < StringFormat
280 class LinkFormat < StringFormat
281 add 'link'
281 add 'link'
282 self.searchable_supported = false
282 self.searchable_supported = false
283 self.form_partial = 'custom_fields/formats/link'
283 self.form_partial = 'custom_fields/formats/link'
284 field_attributes :url_pattern
284 field_attributes :url_pattern
285
285
286 def formatted_value(view, custom_field, value, customized=nil, html=false)
286 def formatted_value(view, custom_field, value, customized=nil, html=false)
287 if html
287 if html
288 if custom_field.url_pattern.present?
288 if custom_field.url_pattern.present?
289 url = custom_field.url_pattern.to_s.dup
289 url = custom_field.url_pattern.to_s.dup
290 url.gsub!('%value%') {value.to_s}
290 url.gsub!('%value%') {value.to_s}
291 url.gsub!('%id%') {customized.id.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}
292 url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
293 if custom_field.regexp.present?
293 if custom_field.regexp.present?
294 url.gsub!(%r{%m(\d+)%}) do
294 url.gsub!(%r{%m(\d+)%}) do
295 m = $1.to_i
295 m = $1.to_i
296 matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
296 matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
297 matches[m].to_s if matches
297 matches[m].to_s if matches
298 end
298 end
299 end
299 end
300 else
300 else
301 url = value.to_s
301 url = value.to_s
302 unless url =~ %r{\A[a-z]+://}i
302 unless url =~ %r{\A[a-z]+://}i
303 # no protocol found, use http by default
303 # no protocol found, use http by default
304 url = "http://" + url
304 url = "http://" + url
305 end
305 end
306 end
306 end
307 view.link_to value.to_s, url
307 view.link_to value.to_s, url
308 else
308 else
309 value.to_s
309 value.to_s
310 end
310 end
311 end
311 end
312 end
312 end
313
313
314 class Numeric < Unbounded
314 class Numeric < Unbounded
315 self.form_partial = 'custom_fields/formats/numeric'
315 self.form_partial = 'custom_fields/formats/numeric'
316
316
317 def order_statement(custom_field)
317 def order_statement(custom_field)
318 # Make the database cast values into numeric
318 # Make the database cast values into numeric
319 # Postgresql will raise an error if a value can not be casted!
319 # Postgresql will raise an error if a value can not be casted!
320 # CustomValue validations should ensure that it doesn't occur
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))"
321 "CAST(CASE #{join_alias custom_field}.value WHEN '' THEN '0' ELSE #{join_alias custom_field}.value END AS decimal(30,3))"
322 end
322 end
323 end
323 end
324
324
325 class IntFormat < Numeric
325 class IntFormat < Numeric
326 add 'int'
326 add 'int'
327
327
328 def label
328 def label
329 "label_integer"
329 "label_integer"
330 end
330 end
331
331
332 def cast_single_value(custom_field, value, customized=nil)
332 def cast_single_value(custom_field, value, customized=nil)
333 value.to_i
333 value.to_i
334 end
334 end
335
335
336 def validate_single_value(custom_field, value, customized=nil)
336 def validate_single_value(custom_field, value, customized=nil)
337 errs = super
337 errs = super
338 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
338 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
339 errs
339 errs
340 end
340 end
341
341
342 def query_filter_options(custom_field, query)
342 def query_filter_options(custom_field, query)
343 {:type => :integer}
343 {:type => :integer}
344 end
344 end
345
345
346 def group_statement(custom_field)
346 def group_statement(custom_field)
347 order_statement(custom_field)
347 order_statement(custom_field)
348 end
348 end
349 end
349 end
350
350
351 class FloatFormat < Numeric
351 class FloatFormat < Numeric
352 add 'float'
352 add 'float'
353
353
354 def cast_single_value(custom_field, value, customized=nil)
354 def cast_single_value(custom_field, value, customized=nil)
355 value.to_f
355 value.to_f
356 end
356 end
357
357
358 def validate_single_value(custom_field, value, customized=nil)
358 def validate_single_value(custom_field, value, customized=nil)
359 errs = super
359 errs = super
360 errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
360 errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
361 errs
361 errs
362 end
362 end
363
363
364 def query_filter_options(custom_field, query)
364 def query_filter_options(custom_field, query)
365 {:type => :float}
365 {:type => :float}
366 end
366 end
367 end
367 end
368
368
369 class DateFormat < Unbounded
369 class DateFormat < Unbounded
370 add 'date'
370 add 'date'
371 self.form_partial = 'custom_fields/formats/date'
371 self.form_partial = 'custom_fields/formats/date'
372
372
373 def cast_single_value(custom_field, value, customized=nil)
373 def cast_single_value(custom_field, value, customized=nil)
374 value.to_date rescue nil
374 value.to_date rescue nil
375 end
375 end
376
376
377 def validate_single_value(custom_field, value, customized=nil)
377 def validate_single_value(custom_field, value, customized=nil)
378 if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)
378 if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)
379 []
379 []
380 else
380 else
381 [::I18n.t('activerecord.errors.messages.not_a_date')]
381 [::I18n.t('activerecord.errors.messages.not_a_date')]
382 end
382 end
383 end
383 end
384
384
385 def edit_tag(view, tag_id, tag_name, custom_value, options={})
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)) +
386 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10)) +
387 view.calendar_for(tag_id)
387 view.calendar_for(tag_id)
388 end
388 end
389
389
390 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
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)) +
391 view.text_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10)) +
392 view.calendar_for(tag_id) +
392 view.calendar_for(tag_id) +
393 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
393 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
394 end
394 end
395
395
396 def query_filter_options(custom_field, query)
396 def query_filter_options(custom_field, query)
397 {:type => :date}
397 {:type => :date}
398 end
398 end
399
399
400 def group_statement(custom_field)
400 def group_statement(custom_field)
401 order_statement(custom_field)
401 order_statement(custom_field)
402 end
402 end
403 end
403 end
404
404
405 class List < Base
405 class List < Base
406 self.multiple_supported = true
406 self.multiple_supported = true
407 field_attributes :edit_tag_style
407 field_attributes :edit_tag_style
408
408
409 def edit_tag(view, tag_id, tag_name, custom_value, options={})
409 def edit_tag(view, tag_id, tag_name, custom_value, options={})
410 if custom_value.custom_field.edit_tag_style == 'check_box'
410 if custom_value.custom_field.edit_tag_style == 'check_box'
411 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
411 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
412 else
412 else
413 select_edit_tag(view, tag_id, tag_name, custom_value, options)
413 select_edit_tag(view, tag_id, tag_name, custom_value, options)
414 end
414 end
415 end
415 end
416
416
417 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
417 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
418 opts = []
418 opts = []
419 opts << [l(:label_no_change_option), ''] unless custom_field.multiple?
419 opts << [l(:label_no_change_option), ''] unless custom_field.multiple?
420 opts << [l(:label_none), '__none__'] unless custom_field.is_required?
420 opts << [l(:label_none), '__none__'] unless custom_field.is_required?
421 opts += possible_values_options(custom_field, objects)
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?))
422 view.select_tag(tag_name, view.options_for_select(opts, value), options.merge(:multiple => custom_field.multiple?))
423 end
423 end
424
424
425 def query_filter_options(custom_field, query)
425 def query_filter_options(custom_field, query)
426 {:type => :list_optional, :values => possible_values_options(custom_field, query.project)}
426 {:type => :list_optional, :values => possible_values_options(custom_field, query.project)}
427 end
427 end
428
428
429 protected
429 protected
430
430
431 # Renders the edit tag as a select tag
431 # Renders the edit tag as a select tag
432 def select_edit_tag(view, tag_id, tag_name, custom_value, options={})
432 def select_edit_tag(view, tag_id, tag_name, custom_value, options={})
433 blank_option = ''.html_safe
433 blank_option = ''.html_safe
434 unless custom_value.custom_field.multiple?
434 unless custom_value.custom_field.multiple?
435 if custom_value.custom_field.is_required?
435 if custom_value.custom_field.is_required?
436 unless custom_value.custom_field.default_value.present?
436 unless custom_value.custom_field.default_value.present?
437 blank_option = view.content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
437 blank_option = view.content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
438 end
438 end
439 else
439 else
440 blank_option = view.content_tag('option', '&nbsp;'.html_safe, :value => '')
440 blank_option = view.content_tag('option', '&nbsp;'.html_safe, :value => '')
441 end
441 end
442 end
442 end
443 options_tags = blank_option + view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
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?))
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?
445 if custom_value.custom_field.multiple?
446 s << view.hidden_field_tag(tag_name, '')
446 s << view.hidden_field_tag(tag_name, '')
447 end
447 end
448 s
448 s
449 end
449 end
450
450
451 # Renders the edit tag as check box or radio tags
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={})
452 def check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
453 opts = []
453 opts = []
454 unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required?
454 unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required?
455 opts << ["(#{l(:label_none)})", '']
455 opts << ["(#{l(:label_none)})", '']
456 end
456 end
457 opts += possible_custom_value_options(custom_value)
457 opts += possible_custom_value_options(custom_value)
458 s = ''.html_safe
458 s = ''.html_safe
459 tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag
459 tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag
460 opts.each do |label, value|
460 opts.each do |label, value|
461 value ||= label
461 value ||= label
462 checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value
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)
463 tag = view.send(tag_method, tag_name, value, checked, :id => tag_id)
464 # set the id on the first tag only
464 # set the id on the first tag only
465 tag_id = nil
465 tag_id = nil
466 s << view.content_tag('label', tag + ' ' + label)
466 s << view.content_tag('label', tag + ' ' + label)
467 end
467 end
468 css = "#{options[:class]} check_box_group"
468 css = "#{options[:class]} check_box_group"
469 view.content_tag('span', s, options.merge(:class => css))
469 view.content_tag('span', s, options.merge(:class => css))
470 end
470 end
471 end
471 end
472
472
473 class ListFormat < List
473 class ListFormat < List
474 add 'list'
474 add 'list'
475 self.searchable_supported = true
475 self.searchable_supported = true
476 self.form_partial = 'custom_fields/formats/list'
476 self.form_partial = 'custom_fields/formats/list'
477
477
478 def possible_custom_value_options(custom_value)
478 def possible_custom_value_options(custom_value)
479 options = super
479 options = possible_values_options(custom_value.custom_field)
480 missing = [custom_value.value].flatten.reject(&:blank?) - options
480 missing = [custom_value.value].flatten.reject(&:blank?) - options
481 if missing.any?
481 if missing.any?
482 options += missing
482 options += missing
483 end
483 end
484 options
484 options
485 end
485 end
486
486
487 def possible_values_options(custom_field, object=nil)
488 custom_field.possible_values
489 end
490
487 def validate_custom_field(custom_field)
491 def validate_custom_field(custom_field)
488 errors = []
492 errors = []
489 errors << [:possible_values, :blank] if custom_field.possible_values.blank?
493 errors << [:possible_values, :blank] if custom_field.possible_values.blank?
490 errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array
494 errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array
491 errors
495 errors
492 end
496 end
493
497
494 def validate_custom_value(custom_value)
498 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
499 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?
500 if invalid_values.select(&:present?).any?
497 [::I18n.t('activerecord.errors.messages.inclusion')]
501 [::I18n.t('activerecord.errors.messages.inclusion')]
498 else
502 else
499 []
503 []
500 end
504 end
501 end
505 end
502
506
503 def group_statement(custom_field)
507 def group_statement(custom_field)
504 order_statement(custom_field)
508 order_statement(custom_field)
505 end
509 end
506 end
510 end
507
511
508 class BoolFormat < List
512 class BoolFormat < List
509 add 'bool'
513 add 'bool'
510 self.multiple_supported = false
514 self.multiple_supported = false
511 self.form_partial = 'custom_fields/formats/bool'
515 self.form_partial = 'custom_fields/formats/bool'
512
516
513 def label
517 def label
514 "label_boolean"
518 "label_boolean"
515 end
519 end
516
520
517 def cast_single_value(custom_field, value, customized=nil)
521 def cast_single_value(custom_field, value, customized=nil)
518 value == '1' ? true : false
522 value == '1' ? true : false
519 end
523 end
520
524
521 def possible_values_options(custom_field, object=nil)
525 def possible_values_options(custom_field, object=nil)
522 [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
526 [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
523 end
527 end
524
528
525 def group_statement(custom_field)
529 def group_statement(custom_field)
526 order_statement(custom_field)
530 order_statement(custom_field)
527 end
531 end
528 end
532 end
529
533
530 class RecordList < List
534 class RecordList < List
531 self.customized_class_names = %w(Issue TimeEntry Version Project)
535 self.customized_class_names = %w(Issue TimeEntry Version Project)
532
536
533 def cast_single_value(custom_field, value, customized=nil)
537 def cast_single_value(custom_field, value, customized=nil)
534 target_class.find_by_id(value.to_i) if value.present?
538 target_class.find_by_id(value.to_i) if value.present?
535 end
539 end
536
540
537 def target_class
541 def target_class
538 @target_class ||= self.class.name[/^(.*::)?(.+)Format$/, 2].constantize rescue nil
542 @target_class ||= self.class.name[/^(.*::)?(.+)Format$/, 2].constantize rescue nil
539 end
543 end
540
544
541 def possible_custom_value_options(custom_value)
545 def possible_custom_value_options(custom_value)
542 options = possible_values_options(custom_value.custom_field, custom_value.customized)
546 options = possible_values_options(custom_value.custom_field, custom_value.customized)
543 missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last)
547 missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last)
544 if missing.any?
548 if missing.any?
545 options += target_class.find_all_by_id(missing.map(&:to_i)).map {|o| [o.to_s, o.id.to_s]}
549 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)
550 options.sort_by!(&:first)
547 end
551 end
548 options
552 options
549 end
553 end
550
554
551 def order_statement(custom_field)
555 def order_statement(custom_field)
552 if target_class.respond_to?(:fields_for_order_statement)
556 if target_class.respond_to?(:fields_for_order_statement)
553 target_class.fields_for_order_statement(value_join_alias(custom_field))
557 target_class.fields_for_order_statement(value_join_alias(custom_field))
554 end
558 end
555 end
559 end
556
560
557 def group_statement(custom_field)
561 def group_statement(custom_field)
558 "COALESCE(#{join_alias custom_field}.value, '')"
562 "COALESCE(#{join_alias custom_field}.value, '')"
559 end
563 end
560
564
561 def join_for_order_statement(custom_field)
565 def join_for_order_statement(custom_field)
562 alias_name = join_alias(custom_field)
566 alias_name = join_alias(custom_field)
563
567
564 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
568 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
565 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
569 " 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" +
570 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
567 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
571 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
568 " AND (#{custom_field.visibility_by_project_condition})" +
572 " AND (#{custom_field.visibility_by_project_condition})" +
569 " AND #{alias_name}.value <> ''" +
573 " AND #{alias_name}.value <> ''" +
570 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
574 " 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" +
575 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
572 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
576 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
573 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)" +
577 " 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}" +
578 " 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"
579 " 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
580 end
577
581
578 def value_join_alias(custom_field)
582 def value_join_alias(custom_field)
579 join_alias(custom_field) + "_" + custom_field.field_format
583 join_alias(custom_field) + "_" + custom_field.field_format
580 end
584 end
581 protected :value_join_alias
585 protected :value_join_alias
582 end
586 end
583
587
584 class UserFormat < RecordList
588 class UserFormat < RecordList
585 add 'user'
589 add 'user'
586 self.form_partial = 'custom_fields/formats/user'
590 self.form_partial = 'custom_fields/formats/user'
587 field_attributes :user_role
591 field_attributes :user_role
588
592
589 def possible_values_options(custom_field, object=nil)
593 def possible_values_options(custom_field, object=nil)
590 if object.is_a?(Array)
594 if object.is_a?(Array)
591 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
595 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
592 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
596 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
593 elsif object.respond_to?(:project) && object.project
597 elsif object.respond_to?(:project) && object.project
594 scope = object.project.users
598 scope = object.project.users
595 if custom_field.user_role.is_a?(Array)
599 if custom_field.user_role.is_a?(Array)
596 role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i)
600 role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i)
597 if role_ids.any?
601 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)
602 scope = scope.where("#{Member.table_name}.id IN (SELECT DISTINCT member_id FROM #{MemberRole.table_name} WHERE role_id IN (?))", role_ids)
599 end
603 end
600 end
604 end
601 scope.sorted.collect {|u| [u.to_s, u.id.to_s]}
605 scope.sorted.collect {|u| [u.to_s, u.id.to_s]}
602 else
606 else
603 []
607 []
604 end
608 end
605 end
609 end
606
610
607 def before_custom_field_save(custom_field)
611 def before_custom_field_save(custom_field)
608 super
612 super
609 if custom_field.user_role.is_a?(Array)
613 if custom_field.user_role.is_a?(Array)
610 custom_field.user_role.map!(&:to_s).reject!(&:blank?)
614 custom_field.user_role.map!(&:to_s).reject!(&:blank?)
611 end
615 end
612 end
616 end
613 end
617 end
614
618
615 class VersionFormat < RecordList
619 class VersionFormat < RecordList
616 add 'version'
620 add 'version'
617 self.form_partial = 'custom_fields/formats/version'
621 self.form_partial = 'custom_fields/formats/version'
618 field_attributes :version_status
622 field_attributes :version_status
619
623
620 def possible_values_options(custom_field, object=nil)
624 def possible_values_options(custom_field, object=nil)
621 if object.is_a?(Array)
625 if object.is_a?(Array)
622 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
626 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
623 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
627 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
624 elsif object.respond_to?(:project) && object.project
628 elsif object.respond_to?(:project) && object.project
625 scope = object.project.shared_versions
629 scope = object.project.shared_versions
626 if custom_field.version_status.is_a?(Array)
630 if custom_field.version_status.is_a?(Array)
627 statuses = custom_field.version_status.map(&:to_s).reject(&:blank?)
631 statuses = custom_field.version_status.map(&:to_s).reject(&:blank?)
628 if statuses.any?
632 if statuses.any?
629 scope = scope.where(:status => statuses.map(&:to_s))
633 scope = scope.where(:status => statuses.map(&:to_s))
630 end
634 end
631 end
635 end
632 scope.sort.collect {|u| [u.to_s, u.id.to_s]}
636 scope.sort.collect {|u| [u.to_s, u.id.to_s]}
633 else
637 else
634 []
638 []
635 end
639 end
636 end
640 end
637
641
638 def before_custom_field_save(custom_field)
642 def before_custom_field_save(custom_field)
639 super
643 super
640 if custom_field.version_status.is_a?(Array)
644 if custom_field.version_status.is_a?(Array)
641 custom_field.version_status.map!(&:to_s).reject!(&:blank?)
645 custom_field.version_status.map!(&:to_s).reject!(&:blank?)
642 end
646 end
643 end
647 end
644 end
648 end
645 end
649 end
646 end
650 end
General Comments 0
You need to be logged in to leave comments. Login now