@@ -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 => 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 |
General Comments 0
You need to be logged in to leave comments.
Login now