@@ -1,388 +1,388 | |||||
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 CustomField < ActiveRecord::Base |
|
18 | class CustomField < ActiveRecord::Base | |
19 | include Redmine::SubclassFactory |
|
19 | include Redmine::SubclassFactory | |
20 |
|
20 | |||
21 | has_many :custom_values, :dependent => :delete_all |
|
21 | has_many :custom_values, :dependent => :delete_all | |
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 |
|
25 | |||
26 | validates_presence_of :name, :field_format |
|
26 | validates_presence_of :name, :field_format | |
27 | validates_uniqueness_of :name, :scope => :type |
|
27 | validates_uniqueness_of :name, :scope => :type | |
28 | validates_length_of :name, :maximum => 30 |
|
28 | validates_length_of :name, :maximum => 30 | |
29 | validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats |
|
29 | validates_inclusion_of :field_format, :in => Proc.new { Redmine::CustomFieldFormat.available_formats } | |
30 | validate :validate_custom_field |
|
30 | validate :validate_custom_field | |
31 |
|
31 | |||
32 | before_validation :set_searchable |
|
32 | before_validation :set_searchable | |
33 | after_save :handle_multiplicity_change |
|
33 | after_save :handle_multiplicity_change | |
34 | after_save do |field| |
|
34 | after_save do |field| | |
35 | if field.visible_changed? && field.visible |
|
35 | if field.visible_changed? && field.visible | |
36 | field.roles.clear |
|
36 | field.roles.clear | |
37 | end |
|
37 | end | |
38 | end |
|
38 | end | |
39 |
|
39 | |||
40 | scope :sorted, lambda { order("#{table_name}.position ASC") } |
|
40 | scope :sorted, lambda { order("#{table_name}.position ASC") } | |
41 | scope :visible, lambda {|*args| |
|
41 | scope :visible, lambda {|*args| | |
42 | user = args.shift || User.current |
|
42 | user = args.shift || User.current | |
43 | if user.admin? |
|
43 | if user.admin? | |
44 | # nop |
|
44 | # nop | |
45 | elsif user.memberships.any? |
|
45 | elsif user.memberships.any? | |
46 | where("#{table_name}.visible = ? OR #{table_name}.id IN (SELECT DISTINCT cfr.custom_field_id FROM #{Member.table_name} m" + |
|
46 | where("#{table_name}.visible = ? OR #{table_name}.id IN (SELECT DISTINCT cfr.custom_field_id FROM #{Member.table_name} m" + | |
47 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + |
|
47 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + | |
48 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + |
|
48 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + | |
49 | " WHERE m.user_id = ?)", |
|
49 | " WHERE m.user_id = ?)", | |
50 | true, user.id) |
|
50 | true, user.id) | |
51 | else |
|
51 | else | |
52 | where(:visible => true) |
|
52 | where(:visible => true) | |
53 | end |
|
53 | end | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | def visible_by?(project, user=User.current) |
|
56 | def visible_by?(project, user=User.current) | |
57 | visible? || user.admin? |
|
57 | visible? || user.admin? | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def field_format=(arg) |
|
60 | def field_format=(arg) | |
61 | # cannot change format of a saved custom field |
|
61 | # cannot change format of a saved custom field | |
62 | super if new_record? |
|
62 | super if new_record? | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | def set_searchable |
|
65 | def set_searchable | |
66 | # make sure these fields are not searchable |
|
66 | # make sure these fields are not searchable | |
67 | self.searchable = false if %w(int float date bool).include?(field_format) |
|
67 | self.searchable = false if %w(int float date bool).include?(field_format) | |
68 | # make sure only these fields can have multiple values |
|
68 | # make sure only these fields can have multiple values | |
69 | self.multiple = false unless %w(list user version).include?(field_format) |
|
69 | self.multiple = false unless %w(list user version).include?(field_format) | |
70 | true |
|
70 | true | |
71 | end |
|
71 | end | |
72 |
|
72 | |||
73 | def validate_custom_field |
|
73 | def validate_custom_field | |
74 | if self.field_format == "list" |
|
74 | if self.field_format == "list" | |
75 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? |
|
75 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? | |
76 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array |
|
76 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | if regexp.present? |
|
79 | if regexp.present? | |
80 | begin |
|
80 | begin | |
81 | Regexp.new(regexp) |
|
81 | Regexp.new(regexp) | |
82 | rescue |
|
82 | rescue | |
83 | errors.add(:regexp, :invalid) |
|
83 | errors.add(:regexp, :invalid) | |
84 | end |
|
84 | end | |
85 | end |
|
85 | end | |
86 |
|
86 | |||
87 | if default_value.present? && !valid_field_value?(default_value) |
|
87 | if default_value.present? && !valid_field_value?(default_value) | |
88 | errors.add(:default_value, :invalid) |
|
88 | errors.add(:default_value, :invalid) | |
89 | end |
|
89 | end | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | def possible_values_options(obj=nil) |
|
92 | def possible_values_options(obj=nil) | |
93 | case field_format |
|
93 | case field_format | |
94 | when 'user', 'version' |
|
94 | when 'user', 'version' | |
95 | if obj.respond_to?(:project) && obj.project |
|
95 | if obj.respond_to?(:project) && obj.project | |
96 | case field_format |
|
96 | case field_format | |
97 | when 'user' |
|
97 | when 'user' | |
98 | obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} |
|
98 | obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} | |
99 | when 'version' |
|
99 | when 'version' | |
100 | obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} |
|
100 | obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} | |
101 | end |
|
101 | end | |
102 | elsif obj.is_a?(Array) |
|
102 | elsif obj.is_a?(Array) | |
103 | obj.collect {|o| possible_values_options(o)}.reduce(:&) |
|
103 | obj.collect {|o| possible_values_options(o)}.reduce(:&) | |
104 | else |
|
104 | else | |
105 | [] |
|
105 | [] | |
106 | end |
|
106 | end | |
107 | when 'bool' |
|
107 | when 'bool' | |
108 | [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']] |
|
108 | [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']] | |
109 | else |
|
109 | else | |
110 | possible_values || [] |
|
110 | possible_values || [] | |
111 | end |
|
111 | end | |
112 | end |
|
112 | end | |
113 |
|
113 | |||
114 | def possible_values(obj=nil) |
|
114 | def possible_values(obj=nil) | |
115 | case field_format |
|
115 | case field_format | |
116 | when 'user', 'version' |
|
116 | when 'user', 'version' | |
117 | possible_values_options(obj).collect(&:last) |
|
117 | possible_values_options(obj).collect(&:last) | |
118 | when 'bool' |
|
118 | when 'bool' | |
119 | ['1', '0'] |
|
119 | ['1', '0'] | |
120 | else |
|
120 | else | |
121 | values = super() |
|
121 | values = super() | |
122 | if values.is_a?(Array) |
|
122 | if values.is_a?(Array) | |
123 | values.each do |value| |
|
123 | values.each do |value| | |
124 | value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) |
|
124 | value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) | |
125 | end |
|
125 | end | |
126 | values |
|
126 | values | |
127 | else |
|
127 | else | |
128 | [] |
|
128 | [] | |
129 | end |
|
129 | end | |
130 | end |
|
130 | end | |
131 | end |
|
131 | end | |
132 |
|
132 | |||
133 | # Makes possible_values accept a multiline string |
|
133 | # Makes possible_values accept a multiline string | |
134 | def possible_values=(arg) |
|
134 | def possible_values=(arg) | |
135 | if arg.is_a?(Array) |
|
135 | if arg.is_a?(Array) | |
136 | super(arg.compact.collect(&:strip).select {|v| !v.blank?}) |
|
136 | super(arg.compact.collect(&:strip).select {|v| !v.blank?}) | |
137 | else |
|
137 | else | |
138 | self.possible_values = arg.to_s.split(/[\n\r]+/) |
|
138 | self.possible_values = arg.to_s.split(/[\n\r]+/) | |
139 | end |
|
139 | end | |
140 | end |
|
140 | end | |
141 |
|
141 | |||
142 | def cast_value(value) |
|
142 | def cast_value(value) | |
143 | casted = nil |
|
143 | casted = nil | |
144 | unless value.blank? |
|
144 | unless value.blank? | |
145 | case field_format |
|
145 | case field_format | |
146 | when 'string', 'text', 'list' |
|
146 | when 'string', 'text', 'list' | |
147 | casted = value |
|
147 | casted = value | |
148 | when 'date' |
|
148 | when 'date' | |
149 | casted = begin; value.to_date; rescue; nil end |
|
149 | casted = begin; value.to_date; rescue; nil end | |
150 | when 'bool' |
|
150 | when 'bool' | |
151 | casted = (value == '1' ? true : false) |
|
151 | casted = (value == '1' ? true : false) | |
152 | when 'int' |
|
152 | when 'int' | |
153 | casted = value.to_i |
|
153 | casted = value.to_i | |
154 | when 'float' |
|
154 | when 'float' | |
155 | casted = value.to_f |
|
155 | casted = value.to_f | |
156 | when 'user', 'version' |
|
156 | when 'user', 'version' | |
157 | casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) |
|
157 | casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) | |
158 | end |
|
158 | end | |
159 | end |
|
159 | end | |
160 | casted |
|
160 | casted | |
161 | end |
|
161 | end | |
162 |
|
162 | |||
163 | def value_from_keyword(keyword, customized) |
|
163 | def value_from_keyword(keyword, customized) | |
164 | possible_values_options = possible_values_options(customized) |
|
164 | possible_values_options = possible_values_options(customized) | |
165 | if possible_values_options.present? |
|
165 | if possible_values_options.present? | |
166 | keyword = keyword.to_s.downcase |
|
166 | keyword = keyword.to_s.downcase | |
167 | if v = possible_values_options.detect {|text, id| text.downcase == keyword} |
|
167 | if v = possible_values_options.detect {|text, id| text.downcase == keyword} | |
168 | if v.is_a?(Array) |
|
168 | if v.is_a?(Array) | |
169 | v.last |
|
169 | v.last | |
170 | else |
|
170 | else | |
171 | v |
|
171 | v | |
172 | end |
|
172 | end | |
173 | end |
|
173 | end | |
174 | else |
|
174 | else | |
175 | keyword |
|
175 | keyword | |
176 | end |
|
176 | end | |
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 |
|
182 | def order_statement | |
183 | return nil if multiple? |
|
183 | return nil if multiple? | |
184 | case field_format |
|
184 | case field_format | |
185 | when 'string', 'text', 'list', 'date', 'bool' |
|
185 | when 'string', 'text', 'list', 'date', 'bool' | |
186 | # COALESCE is here to make sure that blank and NULL values are sorted equally |
|
186 | # COALESCE is here to make sure that blank and NULL values are sorted equally | |
187 | "COALESCE(#{join_alias}.value, '')" |
|
187 | "COALESCE(#{join_alias}.value, '')" | |
188 | when 'int', 'float' |
|
188 | when 'int', 'float' | |
189 | # Make the database cast values into numeric |
|
189 | # Make the database cast values into numeric | |
190 | # Postgresql will raise an error if a value can not be casted! |
|
190 | # Postgresql will raise an error if a value can not be casted! | |
191 | # CustomValue validations should ensure that it doesn't occur |
|
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))" |
|
192 | "CAST(CASE #{join_alias}.value WHEN '' THEN '0' ELSE #{join_alias}.value END AS decimal(30,3))" | |
193 | when 'user', 'version' |
|
193 | when 'user', 'version' | |
194 | value_class.fields_for_order_statement(value_join_alias) |
|
194 | value_class.fields_for_order_statement(value_join_alias) | |
195 | else |
|
195 | else | |
196 | nil |
|
196 | nil | |
197 | end |
|
197 | end | |
198 | end |
|
198 | end | |
199 |
|
199 | |||
200 | # Returns a GROUP BY clause that can used to group by custom value |
|
200 | # 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. |
|
201 | # Returns nil if the custom field can not be used for grouping. | |
202 | def group_statement |
|
202 | def group_statement | |
203 | return nil if multiple? |
|
203 | return nil if multiple? | |
204 | case field_format |
|
204 | case field_format | |
205 | when 'list', 'date', 'bool', 'int' |
|
205 | when 'list', 'date', 'bool', 'int' | |
206 | order_statement |
|
206 | order_statement | |
207 | when 'user', 'version' |
|
207 | when 'user', 'version' | |
208 | "COALESCE(#{join_alias}.value, '')" |
|
208 | "COALESCE(#{join_alias}.value, '')" | |
209 | else |
|
209 | else | |
210 | nil |
|
210 | nil | |
211 | end |
|
211 | end | |
212 | end |
|
212 | end | |
213 |
|
213 | |||
214 | def join_for_order_statement |
|
214 | def join_for_order_statement | |
215 | case field_format |
|
215 | case field_format | |
216 | when 'user', 'version' |
|
216 | when 'user', 'version' | |
217 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + |
|
217 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + | |
218 | " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + |
|
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" + |
|
219 | " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + | |
220 | " AND #{join_alias}.custom_field_id = #{id}" + |
|
220 | " AND #{join_alias}.custom_field_id = #{id}" + | |
221 | " AND (#{visibility_by_project_condition})" + |
|
221 | " AND (#{visibility_by_project_condition})" + | |
222 | " AND #{join_alias}.value <> ''" + |
|
222 | " AND #{join_alias}.value <> ''" + | |
223 | " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + |
|
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" + |
|
224 | " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + | |
225 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + |
|
225 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + | |
226 | " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_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}" + |
|
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" |
|
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' |
|
229 | when 'int', 'float' | |
230 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + |
|
230 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + | |
231 | " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + |
|
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" + |
|
232 | " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + | |
233 | " AND #{join_alias}.custom_field_id = #{id}" + |
|
233 | " AND #{join_alias}.custom_field_id = #{id}" + | |
234 | " AND (#{visibility_by_project_condition})" + |
|
234 | " AND (#{visibility_by_project_condition})" + | |
235 | " AND #{join_alias}.value <> ''" + |
|
235 | " AND #{join_alias}.value <> ''" + | |
236 | " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + |
|
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" + |
|
237 | " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + | |
238 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + |
|
238 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + | |
239 | " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" |
|
239 | " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" | |
240 | when 'string', 'text', 'list', 'date', 'bool' |
|
240 | when 'string', 'text', 'list', 'date', 'bool' | |
241 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + |
|
241 | "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + | |
242 | " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + |
|
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" + |
|
243 | " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + | |
244 | " AND #{join_alias}.custom_field_id = #{id}" + |
|
244 | " AND #{join_alias}.custom_field_id = #{id}" + | |
245 | " AND (#{visibility_by_project_condition})" + |
|
245 | " AND (#{visibility_by_project_condition})" + | |
246 | " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + |
|
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" + |
|
247 | " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + | |
248 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + |
|
248 | " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + | |
249 | " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" |
|
249 | " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" | |
250 | else |
|
250 | else | |
251 | nil |
|
251 | nil | |
252 | end |
|
252 | end | |
253 | end |
|
253 | end | |
254 |
|
254 | |||
255 | def join_alias |
|
255 | def join_alias | |
256 | "cf_#{id}" |
|
256 | "cf_#{id}" | |
257 | end |
|
257 | end | |
258 |
|
258 | |||
259 | def value_join_alias |
|
259 | def value_join_alias | |
260 | join_alias + "_" + field_format |
|
260 | join_alias + "_" + field_format | |
261 | end |
|
261 | end | |
262 |
|
262 | |||
263 | def visibility_by_project_condition(project_key=nil, user=User.current) |
|
263 | def visibility_by_project_condition(project_key=nil, user=User.current) | |
264 | if visible? || user.admin? |
|
264 | if visible? || user.admin? | |
265 | "1=1" |
|
265 | "1=1" | |
266 | elsif user.anonymous? |
|
266 | elsif user.anonymous? | |
267 | "1=0" |
|
267 | "1=0" | |
268 | else |
|
268 | else | |
269 | project_key ||= "#{self.class.customized_class.table_name}.project_id" |
|
269 | project_key ||= "#{self.class.customized_class.table_name}.project_id" | |
270 | "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" + |
|
270 | "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" + | |
271 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + |
|
271 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + | |
272 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + |
|
272 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + | |
273 | " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})" |
|
273 | " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})" | |
274 | end |
|
274 | end | |
275 | end |
|
275 | end | |
276 |
|
276 | |||
277 | def self.visibility_condition |
|
277 | def self.visibility_condition | |
278 | if user.admin? |
|
278 | if user.admin? | |
279 | "1=1" |
|
279 | "1=1" | |
280 | elsif user.anonymous? |
|
280 | elsif user.anonymous? | |
281 | "#{table_name}.visible" |
|
281 | "#{table_name}.visible" | |
282 | else |
|
282 | else | |
283 | "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" + |
|
283 | "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" + | |
284 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + |
|
284 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" + | |
285 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + |
|
285 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" + | |
286 | " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})" |
|
286 | " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})" | |
287 | end |
|
287 | end | |
288 | end |
|
288 | end | |
289 |
|
289 | |||
290 | def <=>(field) |
|
290 | def <=>(field) | |
291 | position <=> field.position |
|
291 | position <=> field.position | |
292 | end |
|
292 | end | |
293 |
|
293 | |||
294 | # Returns the class that values represent |
|
294 | # Returns the class that values represent | |
295 | def value_class |
|
295 | def value_class | |
296 | case field_format |
|
296 | case field_format | |
297 | when 'user', 'version' |
|
297 | when 'user', 'version' | |
298 | field_format.classify.constantize |
|
298 | field_format.classify.constantize | |
299 | else |
|
299 | else | |
300 | nil |
|
300 | nil | |
301 | end |
|
301 | end | |
302 | end |
|
302 | end | |
303 |
|
303 | |||
304 | def self.customized_class |
|
304 | def self.customized_class | |
305 | self.name =~ /^(.+)CustomField$/ |
|
305 | self.name =~ /^(.+)CustomField$/ | |
306 | $1.constantize rescue nil |
|
306 | $1.constantize rescue nil | |
307 | end |
|
307 | end | |
308 |
|
308 | |||
309 | # to move in project_custom_field |
|
309 | # to move in project_custom_field | |
310 | def self.for_all |
|
310 | def self.for_all | |
311 | where(:is_for_all => true).order('position').all |
|
311 | where(:is_for_all => true).order('position').all | |
312 | end |
|
312 | end | |
313 |
|
313 | |||
314 | def type_name |
|
314 | def type_name | |
315 | nil |
|
315 | nil | |
316 | end |
|
316 | end | |
317 |
|
317 | |||
318 | # Returns the error messages for the given value |
|
318 | # Returns the error messages for the given value | |
319 | # or an empty array if value is a valid value for the custom field |
|
319 | # or an empty array if value is a valid value for the custom field | |
320 | def validate_field_value(value) |
|
320 | def validate_field_value(value) | |
321 | errs = [] |
|
321 | errs = [] | |
322 | if value.is_a?(Array) |
|
322 | if value.is_a?(Array) | |
323 | if !multiple? |
|
323 | if !multiple? | |
324 | errs << ::I18n.t('activerecord.errors.messages.invalid') |
|
324 | errs << ::I18n.t('activerecord.errors.messages.invalid') | |
325 | end |
|
325 | end | |
326 | if is_required? && value.detect(&:present?).nil? |
|
326 | if is_required? && value.detect(&:present?).nil? | |
327 | errs << ::I18n.t('activerecord.errors.messages.blank') |
|
327 | errs << ::I18n.t('activerecord.errors.messages.blank') | |
328 | end |
|
328 | end | |
329 | value.each {|v| errs += validate_field_value_format(v)} |
|
329 | value.each {|v| errs += validate_field_value_format(v)} | |
330 | else |
|
330 | else | |
331 | if is_required? && value.blank? |
|
331 | if is_required? && value.blank? | |
332 | errs << ::I18n.t('activerecord.errors.messages.blank') |
|
332 | errs << ::I18n.t('activerecord.errors.messages.blank') | |
333 | end |
|
333 | end | |
334 | errs += validate_field_value_format(value) |
|
334 | errs += validate_field_value_format(value) | |
335 | end |
|
335 | end | |
336 | errs |
|
336 | errs | |
337 | end |
|
337 | end | |
338 |
|
338 | |||
339 | # Returns true if value is a valid value for the custom field |
|
339 | # Returns true if value is a valid value for the custom field | |
340 | def valid_field_value?(value) |
|
340 | def valid_field_value?(value) | |
341 | validate_field_value(value).empty? |
|
341 | validate_field_value(value).empty? | |
342 | end |
|
342 | end | |
343 |
|
343 | |||
344 | def format_in?(*args) |
|
344 | def format_in?(*args) | |
345 | args.include?(field_format) |
|
345 | args.include?(field_format) | |
346 | end |
|
346 | end | |
347 |
|
347 | |||
348 | protected |
|
348 | protected | |
349 |
|
349 | |||
350 | # Returns the error message for the given value regarding its format |
|
350 | # Returns the error message for the given value regarding its format | |
351 | def validate_field_value_format(value) |
|
351 | def validate_field_value_format(value) | |
352 | errs = [] |
|
352 | errs = [] | |
353 | if value.present? |
|
353 | if value.present? | |
354 | errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp) |
|
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 |
|
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 |
|
356 | errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length | |
357 |
|
357 | |||
358 | # Format specific validations |
|
358 | # Format specific validations | |
359 | case field_format |
|
359 | case field_format | |
360 | when 'int' |
|
360 | when 'int' | |
361 | errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/ |
|
361 | errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/ | |
362 | when 'float' |
|
362 | when 'float' | |
363 | begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end |
|
363 | begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end | |
364 | when 'date' |
|
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 |
|
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' |
|
366 | when 'list' | |
367 | errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value) |
|
367 | errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value) | |
368 | end |
|
368 | end | |
369 | end |
|
369 | end | |
370 | errs |
|
370 | errs | |
371 | end |
|
371 | end | |
372 |
|
372 | |||
373 | # Removes multiple values for the custom field after setting the multiple attribute to false |
|
373 | # 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 |
|
374 | # We kepp the value with the highest id for each customized object | |
375 | def handle_multiplicity_change |
|
375 | def handle_multiplicity_change | |
376 | if !new_record? && multiple_was && !multiple |
|
376 | if !new_record? && multiple_was && !multiple | |
377 | ids = custom_values. |
|
377 | ids = custom_values. | |
378 | where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" + |
|
378 | where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" + | |
379 | " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" + |
|
379 | " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" + | |
380 | " AND cve.id > #{CustomValue.table_name}.id)"). |
|
380 | " AND cve.id > #{CustomValue.table_name}.id)"). | |
381 | pluck(:id) |
|
381 | pluck(:id) | |
382 |
|
382 | |||
383 | if ids.any? |
|
383 | if ids.any? | |
384 | custom_values.where(:id => ids).delete_all |
|
384 | custom_values.where(:id => ids).delete_all | |
385 | end |
|
385 | end | |
386 | end |
|
386 | end | |
387 | end |
|
387 | end | |
388 | end |
|
388 | end |
@@ -1,108 +1,115 | |||||
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 | class CustomFieldFormat |
|
19 | class CustomFieldFormat | |
20 | include Redmine::I18n |
|
20 | include Redmine::I18n | |
21 |
|
21 | |||
22 | cattr_accessor :available |
|
22 | cattr_accessor :available | |
23 | @@available = {} |
|
23 | @@available = {} | |
24 |
|
24 | |||
25 | attr_accessor :name, :order, :label, :edit_as, :class_names |
|
25 | attr_accessor :name, :order, :label, :edit_as, :class_names | |
26 |
|
26 | |||
27 | def initialize(name, options={}) |
|
27 | def initialize(name, options={}) | |
28 | self.name = name |
|
28 | self.name = name | |
29 | self.label = options[:label] || "label_#{name}".to_sym |
|
29 | self.label = options[:label] || "label_#{name}".to_sym | |
30 | self.order = options[:order] || self.class.available_formats.size |
|
30 | self.order = options[:order] || self.class.available_formats.size | |
31 | self.edit_as = options[:edit_as] || name |
|
31 | self.edit_as = options[:edit_as] || name | |
32 | self.class_names = options[:only] |
|
32 | self.class_names = options[:only] | |
33 | end |
|
33 | end | |
34 |
|
34 | |||
35 | def format(value) |
|
35 | def format(value) | |
36 | send "format_as_#{name}", value |
|
36 | send "format_as_#{name}", value | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def format_as_date(value) |
|
39 | def format_as_date(value) | |
40 | begin; format_date(value.to_date); rescue; value end |
|
40 | begin; format_date(value.to_date); rescue; value end | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | def format_as_bool(value) |
|
43 | def format_as_bool(value) | |
44 | l(value == "1" ? :general_text_Yes : :general_text_No) |
|
44 | l(value == "1" ? :general_text_Yes : :general_text_No) | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | ['string','text','int','float','list'].each do |name| |
|
47 | ['string','text','int','float','list'].each do |name| | |
48 | define_method("format_as_#{name}") {|value| |
|
48 | define_method("format_as_#{name}") {|value| | |
49 | return value |
|
49 | return value | |
50 | } |
|
50 | } | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | ['user', 'version'].each do |name| |
|
53 | ['user', 'version'].each do |name| | |
54 | define_method("format_as_#{name}") {|value| |
|
54 | define_method("format_as_#{name}") {|value| | |
55 | return value.blank? ? "" : name.classify.constantize.find_by_id(value.to_i).to_s |
|
55 | return value.blank? ? "" : name.classify.constantize.find_by_id(value.to_i).to_s | |
56 | } |
|
56 | } | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | class << self |
|
59 | class << self | |
60 | def map(&block) |
|
60 | def map(&block) | |
61 | yield self |
|
61 | yield self | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | # Registers a custom field format |
|
64 | # Registers a custom field format | |
65 | def register(*args) |
|
65 | def register(*args) | |
66 | custom_field_format = args.first |
|
66 | custom_field_format = args.first | |
67 | unless custom_field_format.is_a?(Redmine::CustomFieldFormat) |
|
67 | unless custom_field_format.is_a?(Redmine::CustomFieldFormat) | |
68 | custom_field_format = Redmine::CustomFieldFormat.new(*args) |
|
68 | custom_field_format = Redmine::CustomFieldFormat.new(*args) | |
69 | end |
|
69 | end | |
70 | @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name) |
|
70 | @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name) | |
71 | end |
|
71 | end | |
72 |
|
72 | |||
|
73 | def delete(format) | |||
|
74 | if format.is_a?(Redmine::CustomFieldFormat) | |||
|
75 | format = format.name | |||
|
76 | end | |||
|
77 | @@available.delete(format) | |||
|
78 | end | |||
|
79 | ||||
73 | def available_formats |
|
80 | def available_formats | |
74 | @@available.keys |
|
81 | @@available.keys | |
75 | end |
|
82 | end | |
76 |
|
83 | |||
77 | def find_by_name(name) |
|
84 | def find_by_name(name) | |
78 | @@available[name.to_s] |
|
85 | @@available[name.to_s] | |
79 | end |
|
86 | end | |
80 |
|
87 | |||
81 | def label_for(name) |
|
88 | def label_for(name) | |
82 | format = @@available[name.to_s] |
|
89 | format = @@available[name.to_s] | |
83 | format.label if format |
|
90 | format.label if format | |
84 | end |
|
91 | end | |
85 |
|
92 | |||
86 | # Return an array of custom field formats which can be used in select_tag |
|
93 | # Return an array of custom field formats which can be used in select_tag | |
87 | def as_select(class_name=nil) |
|
94 | def as_select(class_name=nil) | |
88 | fields = @@available.values |
|
95 | fields = @@available.values | |
89 | fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)} |
|
96 | fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)} | |
90 | fields.sort {|a,b| |
|
97 | fields.sort {|a,b| | |
91 | a.order <=> b.order |
|
98 | a.order <=> b.order | |
92 | }.collect {|custom_field_format| |
|
99 | }.collect {|custom_field_format| | |
93 | [ l(custom_field_format.label), custom_field_format.name ] |
|
100 | [ l(custom_field_format.label), custom_field_format.name ] | |
94 | } |
|
101 | } | |
95 | end |
|
102 | end | |
96 |
|
103 | |||
97 | def format_value(value, field_format) |
|
104 | def format_value(value, field_format) | |
98 | return "" unless value && !value.empty? |
|
105 | return "" unless value && !value.empty? | |
99 |
|
106 | |||
100 | if format_type = find_by_name(field_format) |
|
107 | if format_type = find_by_name(field_format) | |
101 | format_type.format(value) |
|
108 | format_type.format(value) | |
102 | else |
|
109 | else | |
103 | value |
|
110 | value | |
104 | end |
|
111 | end | |
105 | end |
|
112 | end | |
106 | end |
|
113 | end | |
107 | end |
|
114 | end | |
108 | end |
|
115 | end |
@@ -1,282 +1,296 | |||||
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 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class CustomFieldTest < ActiveSupport::TestCase |
|
20 | class CustomFieldTest < ActiveSupport::TestCase | |
21 | fixtures :custom_fields |
|
21 | fixtures :custom_fields | |
22 |
|
22 | |||
23 | def test_create |
|
23 | def test_create | |
24 | field = UserCustomField.new(:name => 'Money money money', :field_format => 'float') |
|
24 | field = UserCustomField.new(:name => 'Money money money', :field_format => 'float') | |
25 | assert field.save |
|
25 | assert field.save | |
26 | end |
|
26 | end | |
27 |
|
27 | |||
28 | def test_before_validation |
|
28 | def test_before_validation | |
29 | field = CustomField.new(:name => 'test_before_validation', :field_format => 'int') |
|
29 | field = CustomField.new(:name => 'test_before_validation', :field_format => 'int') | |
30 | field.searchable = true |
|
30 | field.searchable = true | |
31 | assert field.save |
|
31 | assert field.save | |
32 | assert_equal false, field.searchable |
|
32 | assert_equal false, field.searchable | |
33 | field.searchable = true |
|
33 | field.searchable = true | |
34 | assert field.save |
|
34 | assert field.save | |
35 | assert_equal false, field.searchable |
|
35 | assert_equal false, field.searchable | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def test_regexp_validation |
|
38 | def test_regexp_validation | |
39 | field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9') |
|
39 | field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9') | |
40 | assert !field.save |
|
40 | assert !field.save | |
41 | assert_include I18n.t('activerecord.errors.messages.invalid'), |
|
41 | assert_include I18n.t('activerecord.errors.messages.invalid'), | |
42 | field.errors[:regexp] |
|
42 | field.errors[:regexp] | |
43 | field.regexp = '[a-z0-9]' |
|
43 | field.regexp = '[a-z0-9]' | |
44 | assert field.save |
|
44 | assert field.save | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | def test_default_value_should_be_validated |
|
47 | def test_default_value_should_be_validated | |
48 | field = CustomField.new(:name => 'Test', :field_format => 'int') |
|
48 | field = CustomField.new(:name => 'Test', :field_format => 'int') | |
49 | field.default_value = 'abc' |
|
49 | field.default_value = 'abc' | |
50 | assert !field.valid? |
|
50 | assert !field.valid? | |
51 | field.default_value = '6' |
|
51 | field.default_value = '6' | |
52 | assert field.valid? |
|
52 | assert field.valid? | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | def test_default_value_should_not_be_validated_when_blank |
|
55 | def test_default_value_should_not_be_validated_when_blank | |
56 | field = CustomField.new(:name => 'Test', :field_format => 'list', :possible_values => ['a', 'b'], :is_required => true, :default_value => '') |
|
56 | field = CustomField.new(:name => 'Test', :field_format => 'list', :possible_values => ['a', 'b'], :is_required => true, :default_value => '') | |
57 | assert field.valid? |
|
57 | assert field.valid? | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
|
60 | def test_field_format_should_be_validated | |||
|
61 | field = CustomField.new(:name => 'Test', :field_format => 'foo') | |||
|
62 | assert !field.valid? | |||
|
63 | end | |||
|
64 | ||||
|
65 | def test_field_format_validation_should_accept_formats_added_at_runtime | |||
|
66 | Redmine::CustomFieldFormat.register 'foobar' | |||
|
67 | ||||
|
68 | field = CustomField.new(:name => 'Some Custom Field', :field_format => 'foobar') | |||
|
69 | assert field.valid?, 'field should be valid' | |||
|
70 | ensure | |||
|
71 | Redmine::CustomFieldFormat.delete 'foobar' | |||
|
72 | end | |||
|
73 | ||||
60 | def test_should_not_change_field_format_of_existing_custom_field |
|
74 | def test_should_not_change_field_format_of_existing_custom_field | |
61 | field = CustomField.find(1) |
|
75 | field = CustomField.find(1) | |
62 | field.field_format = 'int' |
|
76 | field.field_format = 'int' | |
63 | assert_equal 'list', field.field_format |
|
77 | assert_equal 'list', field.field_format | |
64 | end |
|
78 | end | |
65 |
|
79 | |||
66 | def test_possible_values_should_accept_an_array |
|
80 | def test_possible_values_should_accept_an_array | |
67 | field = CustomField.new |
|
81 | field = CustomField.new | |
68 | field.possible_values = ["One value", ""] |
|
82 | field.possible_values = ["One value", ""] | |
69 | assert_equal ["One value"], field.possible_values |
|
83 | assert_equal ["One value"], field.possible_values | |
70 | end |
|
84 | end | |
71 |
|
85 | |||
72 | def test_possible_values_should_accept_a_string |
|
86 | def test_possible_values_should_accept_a_string | |
73 | field = CustomField.new |
|
87 | field = CustomField.new | |
74 | field.possible_values = "One value" |
|
88 | field.possible_values = "One value" | |
75 | assert_equal ["One value"], field.possible_values |
|
89 | assert_equal ["One value"], field.possible_values | |
76 | end |
|
90 | end | |
77 |
|
91 | |||
78 | def test_possible_values_should_accept_a_multiline_string |
|
92 | def test_possible_values_should_accept_a_multiline_string | |
79 | field = CustomField.new |
|
93 | field = CustomField.new | |
80 | field.possible_values = "One value\nAnd another one \r\n \n" |
|
94 | field.possible_values = "One value\nAnd another one \r\n \n" | |
81 | assert_equal ["One value", "And another one"], field.possible_values |
|
95 | assert_equal ["One value", "And another one"], field.possible_values | |
82 | end |
|
96 | end | |
83 |
|
97 | |||
84 | if "string".respond_to?(:encoding) |
|
98 | if "string".respond_to?(:encoding) | |
85 | def test_possible_values_stored_as_binary_should_be_utf8_encoded |
|
99 | def test_possible_values_stored_as_binary_should_be_utf8_encoded | |
86 | field = CustomField.find(11) |
|
100 | field = CustomField.find(11) | |
87 | assert_kind_of Array, field.possible_values |
|
101 | assert_kind_of Array, field.possible_values | |
88 | assert field.possible_values.size > 0 |
|
102 | assert field.possible_values.size > 0 | |
89 | field.possible_values.each do |value| |
|
103 | field.possible_values.each do |value| | |
90 | assert_equal "UTF-8", value.encoding.name |
|
104 | assert_equal "UTF-8", value.encoding.name | |
91 | end |
|
105 | end | |
92 | end |
|
106 | end | |
93 | end |
|
107 | end | |
94 |
|
108 | |||
95 | def test_destroy |
|
109 | def test_destroy | |
96 | field = CustomField.find(1) |
|
110 | field = CustomField.find(1) | |
97 | assert field.destroy |
|
111 | assert field.destroy | |
98 | end |
|
112 | end | |
99 |
|
113 | |||
100 | def test_new_subclass_instance_should_return_an_instance |
|
114 | def test_new_subclass_instance_should_return_an_instance | |
101 | f = CustomField.new_subclass_instance('IssueCustomField') |
|
115 | f = CustomField.new_subclass_instance('IssueCustomField') | |
102 | assert_kind_of IssueCustomField, f |
|
116 | assert_kind_of IssueCustomField, f | |
103 | end |
|
117 | end | |
104 |
|
118 | |||
105 | def test_new_subclass_instance_should_set_attributes |
|
119 | def test_new_subclass_instance_should_set_attributes | |
106 | f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test') |
|
120 | f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test') | |
107 | assert_kind_of IssueCustomField, f |
|
121 | assert_kind_of IssueCustomField, f | |
108 | assert_equal 'Test', f.name |
|
122 | assert_equal 'Test', f.name | |
109 | end |
|
123 | end | |
110 |
|
124 | |||
111 | def test_new_subclass_instance_with_invalid_class_name_should_return_nil |
|
125 | def test_new_subclass_instance_with_invalid_class_name_should_return_nil | |
112 | assert_nil CustomField.new_subclass_instance('WrongClassName') |
|
126 | assert_nil CustomField.new_subclass_instance('WrongClassName') | |
113 | end |
|
127 | end | |
114 |
|
128 | |||
115 | def test_new_subclass_instance_with_non_subclass_name_should_return_nil |
|
129 | def test_new_subclass_instance_with_non_subclass_name_should_return_nil | |
116 | assert_nil CustomField.new_subclass_instance('Project') |
|
130 | assert_nil CustomField.new_subclass_instance('Project') | |
117 | end |
|
131 | end | |
118 |
|
132 | |||
119 | def test_string_field_validation_with_blank_value |
|
133 | def test_string_field_validation_with_blank_value | |
120 | f = CustomField.new(:field_format => 'string') |
|
134 | f = CustomField.new(:field_format => 'string') | |
121 |
|
135 | |||
122 | assert f.valid_field_value?(nil) |
|
136 | assert f.valid_field_value?(nil) | |
123 | assert f.valid_field_value?('') |
|
137 | assert f.valid_field_value?('') | |
124 |
|
138 | |||
125 | f.is_required = true |
|
139 | f.is_required = true | |
126 | assert !f.valid_field_value?(nil) |
|
140 | assert !f.valid_field_value?(nil) | |
127 | assert !f.valid_field_value?('') |
|
141 | assert !f.valid_field_value?('') | |
128 | end |
|
142 | end | |
129 |
|
143 | |||
130 | def test_string_field_validation_with_min_and_max_lengths |
|
144 | def test_string_field_validation_with_min_and_max_lengths | |
131 | f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) |
|
145 | f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) | |
132 |
|
146 | |||
133 | assert f.valid_field_value?(nil) |
|
147 | assert f.valid_field_value?(nil) | |
134 | assert f.valid_field_value?('') |
|
148 | assert f.valid_field_value?('') | |
135 | assert f.valid_field_value?('a' * 2) |
|
149 | assert f.valid_field_value?('a' * 2) | |
136 | assert !f.valid_field_value?('a') |
|
150 | assert !f.valid_field_value?('a') | |
137 | assert !f.valid_field_value?('a' * 6) |
|
151 | assert !f.valid_field_value?('a' * 6) | |
138 | end |
|
152 | end | |
139 |
|
153 | |||
140 | def test_string_field_validation_with_regexp |
|
154 | def test_string_field_validation_with_regexp | |
141 | f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') |
|
155 | f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') | |
142 |
|
156 | |||
143 | assert f.valid_field_value?(nil) |
|
157 | assert f.valid_field_value?(nil) | |
144 | assert f.valid_field_value?('') |
|
158 | assert f.valid_field_value?('') | |
145 | assert f.valid_field_value?('ABC') |
|
159 | assert f.valid_field_value?('ABC') | |
146 | assert !f.valid_field_value?('abc') |
|
160 | assert !f.valid_field_value?('abc') | |
147 | end |
|
161 | end | |
148 |
|
162 | |||
149 | def test_date_field_validation |
|
163 | def test_date_field_validation | |
150 | f = CustomField.new(:field_format => 'date') |
|
164 | f = CustomField.new(:field_format => 'date') | |
151 |
|
165 | |||
152 | assert f.valid_field_value?(nil) |
|
166 | assert f.valid_field_value?(nil) | |
153 | assert f.valid_field_value?('') |
|
167 | assert f.valid_field_value?('') | |
154 | assert f.valid_field_value?('1975-07-14') |
|
168 | assert f.valid_field_value?('1975-07-14') | |
155 | assert !f.valid_field_value?('1975-07-33') |
|
169 | assert !f.valid_field_value?('1975-07-33') | |
156 | assert !f.valid_field_value?('abc') |
|
170 | assert !f.valid_field_value?('abc') | |
157 | end |
|
171 | end | |
158 |
|
172 | |||
159 | def test_list_field_validation |
|
173 | def test_list_field_validation | |
160 | f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) |
|
174 | f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) | |
161 |
|
175 | |||
162 | assert f.valid_field_value?(nil) |
|
176 | assert f.valid_field_value?(nil) | |
163 | assert f.valid_field_value?('') |
|
177 | assert f.valid_field_value?('') | |
164 | assert f.valid_field_value?('value2') |
|
178 | assert f.valid_field_value?('value2') | |
165 | assert !f.valid_field_value?('abc') |
|
179 | assert !f.valid_field_value?('abc') | |
166 | end |
|
180 | end | |
167 |
|
181 | |||
168 | def test_int_field_validation |
|
182 | def test_int_field_validation | |
169 | f = CustomField.new(:field_format => 'int') |
|
183 | f = CustomField.new(:field_format => 'int') | |
170 |
|
184 | |||
171 | assert f.valid_field_value?(nil) |
|
185 | assert f.valid_field_value?(nil) | |
172 | assert f.valid_field_value?('') |
|
186 | assert f.valid_field_value?('') | |
173 | assert f.valid_field_value?('123') |
|
187 | assert f.valid_field_value?('123') | |
174 | assert f.valid_field_value?('+123') |
|
188 | assert f.valid_field_value?('+123') | |
175 | assert f.valid_field_value?('-123') |
|
189 | assert f.valid_field_value?('-123') | |
176 | assert !f.valid_field_value?('6abc') |
|
190 | assert !f.valid_field_value?('6abc') | |
177 | end |
|
191 | end | |
178 |
|
192 | |||
179 | def test_float_field_validation |
|
193 | def test_float_field_validation | |
180 | f = CustomField.new(:field_format => 'float') |
|
194 | f = CustomField.new(:field_format => 'float') | |
181 |
|
195 | |||
182 | assert f.valid_field_value?(nil) |
|
196 | assert f.valid_field_value?(nil) | |
183 | assert f.valid_field_value?('') |
|
197 | assert f.valid_field_value?('') | |
184 | assert f.valid_field_value?('11.2') |
|
198 | assert f.valid_field_value?('11.2') | |
185 | assert f.valid_field_value?('-6.250') |
|
199 | assert f.valid_field_value?('-6.250') | |
186 | assert f.valid_field_value?('5') |
|
200 | assert f.valid_field_value?('5') | |
187 | assert !f.valid_field_value?('6abc') |
|
201 | assert !f.valid_field_value?('6abc') | |
188 | end |
|
202 | end | |
189 |
|
203 | |||
190 | def test_multi_field_validation |
|
204 | def test_multi_field_validation | |
191 | f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2']) |
|
205 | f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2']) | |
192 |
|
206 | |||
193 | assert f.valid_field_value?(nil) |
|
207 | assert f.valid_field_value?(nil) | |
194 | assert f.valid_field_value?('') |
|
208 | assert f.valid_field_value?('') | |
195 | assert f.valid_field_value?([]) |
|
209 | assert f.valid_field_value?([]) | |
196 | assert f.valid_field_value?([nil]) |
|
210 | assert f.valid_field_value?([nil]) | |
197 | assert f.valid_field_value?(['']) |
|
211 | assert f.valid_field_value?(['']) | |
198 |
|
212 | |||
199 | assert f.valid_field_value?('value2') |
|
213 | assert f.valid_field_value?('value2') | |
200 | assert !f.valid_field_value?('abc') |
|
214 | assert !f.valid_field_value?('abc') | |
201 |
|
215 | |||
202 | assert f.valid_field_value?(['value2']) |
|
216 | assert f.valid_field_value?(['value2']) | |
203 | assert !f.valid_field_value?(['abc']) |
|
217 | assert !f.valid_field_value?(['abc']) | |
204 |
|
218 | |||
205 | assert f.valid_field_value?(['', 'value2']) |
|
219 | assert f.valid_field_value?(['', 'value2']) | |
206 | assert !f.valid_field_value?(['', 'abc']) |
|
220 | assert !f.valid_field_value?(['', 'abc']) | |
207 |
|
221 | |||
208 | assert f.valid_field_value?(['value1', 'value2']) |
|
222 | assert f.valid_field_value?(['value1', 'value2']) | |
209 | assert !f.valid_field_value?(['value1', 'abc']) |
|
223 | assert !f.valid_field_value?(['value1', 'abc']) | |
210 | end |
|
224 | end | |
211 |
|
225 | |||
212 | def test_changing_multiple_to_false_should_delete_multiple_values |
|
226 | def test_changing_multiple_to_false_should_delete_multiple_values | |
213 | field = ProjectCustomField.create!(:name => 'field', :field_format => 'list', :multiple => 'true', :possible_values => ['field1', 'field2']) |
|
227 | field = ProjectCustomField.create!(:name => 'field', :field_format => 'list', :multiple => 'true', :possible_values => ['field1', 'field2']) | |
214 | other = ProjectCustomField.create!(:name => 'other', :field_format => 'list', :multiple => 'true', :possible_values => ['other1', 'other2']) |
|
228 | other = ProjectCustomField.create!(:name => 'other', :field_format => 'list', :multiple => 'true', :possible_values => ['other1', 'other2']) | |
215 |
|
229 | |||
216 | item_with_multiple_values = Project.generate!(:custom_field_values => {field.id => ['field1', 'field2'], other.id => ['other1', 'other2']}) |
|
230 | item_with_multiple_values = Project.generate!(:custom_field_values => {field.id => ['field1', 'field2'], other.id => ['other1', 'other2']}) | |
217 | item_with_single_values = Project.generate!(:custom_field_values => {field.id => ['field1'], other.id => ['other2']}) |
|
231 | item_with_single_values = Project.generate!(:custom_field_values => {field.id => ['field1'], other.id => ['other2']}) | |
218 |
|
232 | |||
219 | assert_difference 'CustomValue.count', -1 do |
|
233 | assert_difference 'CustomValue.count', -1 do | |
220 | field.multiple = false |
|
234 | field.multiple = false | |
221 | field.save! |
|
235 | field.save! | |
222 | end |
|
236 | end | |
223 |
|
237 | |||
224 | item_with_multiple_values = Project.find(item_with_multiple_values.id) |
|
238 | item_with_multiple_values = Project.find(item_with_multiple_values.id) | |
225 | assert_kind_of String, item_with_multiple_values.custom_field_value(field) |
|
239 | assert_kind_of String, item_with_multiple_values.custom_field_value(field) | |
226 | assert_kind_of Array, item_with_multiple_values.custom_field_value(other) |
|
240 | assert_kind_of Array, item_with_multiple_values.custom_field_value(other) | |
227 | assert_equal 2, item_with_multiple_values.custom_field_value(other).size |
|
241 | assert_equal 2, item_with_multiple_values.custom_field_value(other).size | |
228 | end |
|
242 | end | |
229 |
|
243 | |||
230 | def test_value_class_should_return_the_class_used_for_fields_values |
|
244 | def test_value_class_should_return_the_class_used_for_fields_values | |
231 | assert_equal User, CustomField.new(:field_format => 'user').value_class |
|
245 | assert_equal User, CustomField.new(:field_format => 'user').value_class | |
232 | assert_equal Version, CustomField.new(:field_format => 'version').value_class |
|
246 | assert_equal Version, CustomField.new(:field_format => 'version').value_class | |
233 | end |
|
247 | end | |
234 |
|
248 | |||
235 | def test_value_class_should_return_nil_for_other_fields |
|
249 | def test_value_class_should_return_nil_for_other_fields | |
236 | assert_nil CustomField.new(:field_format => 'text').value_class |
|
250 | assert_nil CustomField.new(:field_format => 'text').value_class | |
237 | assert_nil CustomField.new.value_class |
|
251 | assert_nil CustomField.new.value_class | |
238 | end |
|
252 | end | |
239 |
|
253 | |||
240 | def test_value_from_keyword_for_list_custom_field |
|
254 | def test_value_from_keyword_for_list_custom_field | |
241 | field = CustomField.find(1) |
|
255 | field = CustomField.find(1) | |
242 | assert_equal 'PostgreSQL', field.value_from_keyword('postgresql', Issue.find(1)) |
|
256 | assert_equal 'PostgreSQL', field.value_from_keyword('postgresql', Issue.find(1)) | |
243 | end |
|
257 | end | |
244 |
|
258 | |||
245 | def test_visibile_scope_with_admin_should_return_all_custom_fields |
|
259 | def test_visibile_scope_with_admin_should_return_all_custom_fields | |
246 | CustomField.delete_all |
|
260 | CustomField.delete_all | |
247 | fields = [ |
|
261 | fields = [ | |
248 | CustomField.generate!(:visible => true), |
|
262 | CustomField.generate!(:visible => true), | |
249 | CustomField.generate!(:visible => false), |
|
263 | CustomField.generate!(:visible => false), | |
250 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), |
|
264 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), | |
251 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), |
|
265 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), | |
252 | ] |
|
266 | ] | |
253 |
|
267 | |||
254 | assert_equal 4, CustomField.visible(User.find(1)).count |
|
268 | assert_equal 4, CustomField.visible(User.find(1)).count | |
255 | end |
|
269 | end | |
256 |
|
270 | |||
257 | def test_visibile_scope_with_non_admin_user_should_return_visible_custom_fields |
|
271 | def test_visibile_scope_with_non_admin_user_should_return_visible_custom_fields | |
258 | CustomField.delete_all |
|
272 | CustomField.delete_all | |
259 | fields = [ |
|
273 | fields = [ | |
260 | CustomField.generate!(:visible => true), |
|
274 | CustomField.generate!(:visible => true), | |
261 | CustomField.generate!(:visible => false), |
|
275 | CustomField.generate!(:visible => false), | |
262 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), |
|
276 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), | |
263 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), |
|
277 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), | |
264 | ] |
|
278 | ] | |
265 | user = User.generate! |
|
279 | user = User.generate! | |
266 | User.add_to_project(user, Project.first, Role.find(3)) |
|
280 | User.add_to_project(user, Project.first, Role.find(3)) | |
267 |
|
281 | |||
268 | assert_equal [fields[0], fields[2]], CustomField.visible(user).order("id").to_a |
|
282 | assert_equal [fields[0], fields[2]], CustomField.visible(user).order("id").to_a | |
269 | end |
|
283 | end | |
270 |
|
284 | |||
271 | def test_visibile_scope_with_anonymous_user_should_return_visible_custom_fields |
|
285 | def test_visibile_scope_with_anonymous_user_should_return_visible_custom_fields | |
272 | CustomField.delete_all |
|
286 | CustomField.delete_all | |
273 | fields = [ |
|
287 | fields = [ | |
274 | CustomField.generate!(:visible => true), |
|
288 | CustomField.generate!(:visible => true), | |
275 | CustomField.generate!(:visible => false), |
|
289 | CustomField.generate!(:visible => false), | |
276 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), |
|
290 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), | |
277 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), |
|
291 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), | |
278 | ] |
|
292 | ] | |
279 |
|
293 | |||
280 | 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 | |
281 | end |
|
295 | end | |
282 | end |
|
296 | end |
General Comments 0
You need to be logged in to leave comments.
Login now