##// END OF EJS Templates
Fixed that non-empty blank strings as custom field values are not properly validated (#16169)....
Jean-Philippe Lang -
r12663:4c7a76785d9f
parent child
Show More
@@ -1,283 +1,281
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class CustomField < ActiveRecord::Base
19 19 include Redmine::SubclassFactory
20 20
21 21 has_many :custom_values, :dependent => :delete_all
22 22 has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "custom_field_id"
23 23 acts_as_list :scope => 'type = \'#{self.class}\''
24 24 serialize :possible_values
25 25 store :format_store
26 26
27 27 validates_presence_of :name, :field_format
28 28 validates_uniqueness_of :name, :scope => :type
29 29 validates_length_of :name, :maximum => 30
30 30 validates_inclusion_of :field_format, :in => Proc.new { Redmine::FieldFormat.available_formats }
31 31 validate :validate_custom_field
32 32
33 33 before_validation :set_searchable
34 34 before_save do |field|
35 35 field.format.before_custom_field_save(field)
36 36 end
37 37 after_save :handle_multiplicity_change
38 38 after_save do |field|
39 39 if field.visible_changed? && field.visible
40 40 field.roles.clear
41 41 end
42 42 end
43 43
44 44 scope :sorted, lambda { order("#{table_name}.position ASC") }
45 45 scope :visible, lambda {|*args|
46 46 user = args.shift || User.current
47 47 if user.admin?
48 48 # nop
49 49 elsif user.memberships.any?
50 50 where("#{table_name}.visible = ? OR #{table_name}.id IN (SELECT DISTINCT cfr.custom_field_id FROM #{Member.table_name} m" +
51 51 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
52 52 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
53 53 " WHERE m.user_id = ?)",
54 54 true, user.id)
55 55 else
56 56 where(:visible => true)
57 57 end
58 58 }
59 59
60 60 def visible_by?(project, user=User.current)
61 61 visible? || user.admin?
62 62 end
63 63
64 64 def format
65 65 @format ||= Redmine::FieldFormat.find(field_format)
66 66 end
67 67
68 68 def field_format=(arg)
69 69 # cannot change format of a saved custom field
70 70 if new_record?
71 71 @format = nil
72 72 super
73 73 end
74 74 end
75 75
76 76 def set_searchable
77 77 # make sure these fields are not searchable
78 78 self.searchable = false unless format.class.searchable_supported
79 79 # make sure only these fields can have multiple values
80 80 self.multiple = false unless format.class.multiple_supported
81 81 true
82 82 end
83 83
84 84 def validate_custom_field
85 85 format.validate_custom_field(self).each do |attribute, message|
86 86 errors.add attribute, message
87 87 end
88 88
89 89 if regexp.present?
90 90 begin
91 91 Regexp.new(regexp)
92 92 rescue
93 93 errors.add(:regexp, :invalid)
94 94 end
95 95 end
96 96
97 97 if default_value.present?
98 98 validate_field_value(default_value).each do |message|
99 99 errors.add :default_value, message
100 100 end
101 101 end
102 102 end
103 103
104 104 def possible_custom_value_options(custom_value)
105 105 format.possible_custom_value_options(custom_value)
106 106 end
107 107
108 108 def possible_values_options(object=nil)
109 109 if object.is_a?(Array)
110 110 object.map {|o| format.possible_values_options(self, o)}.reduce(:&) || []
111 111 else
112 112 format.possible_values_options(self, object) || []
113 113 end
114 114 end
115 115
116 116 def possible_values
117 117 values = super()
118 118 if values.is_a?(Array)
119 119 values.each do |value|
120 120 value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
121 121 end
122 122 values
123 123 else
124 124 []
125 125 end
126 126 end
127 127
128 128 # Makes possible_values accept a multiline string
129 129 def possible_values=(arg)
130 130 if arg.is_a?(Array)
131 131 super(arg.compact.collect(&:strip).select {|v| !v.blank?})
132 132 else
133 133 self.possible_values = arg.to_s.split(/[\n\r]+/)
134 134 end
135 135 end
136 136
137 137 def cast_value(value)
138 138 format.cast_value(self, value)
139 139 end
140 140
141 141 def value_from_keyword(keyword, customized)
142 142 possible_values_options = possible_values_options(customized)
143 143 if possible_values_options.present?
144 144 keyword = keyword.to_s.downcase
145 145 if v = possible_values_options.detect {|text, id| text.downcase == keyword}
146 146 if v.is_a?(Array)
147 147 v.last
148 148 else
149 149 v
150 150 end
151 151 end
152 152 else
153 153 keyword
154 154 end
155 155 end
156 156
157 157 # Returns a ORDER BY clause that can used to sort customized
158 158 # objects by their value of the custom field.
159 159 # Returns nil if the custom field can not be used for sorting.
160 160 def order_statement
161 161 return nil if multiple?
162 162 format.order_statement(self)
163 163 end
164 164
165 165 # Returns a GROUP BY clause that can used to group by custom value
166 166 # Returns nil if the custom field can not be used for grouping.
167 167 def group_statement
168 168 return nil if multiple?
169 169 format.group_statement(self)
170 170 end
171 171
172 172 def join_for_order_statement
173 173 format.join_for_order_statement(self)
174 174 end
175 175
176 176 def visibility_by_project_condition(project_key=nil, user=User.current, id_column=nil)
177 177 if visible? || user.admin?
178 178 "1=1"
179 179 elsif user.anonymous?
180 180 "1=0"
181 181 else
182 182 project_key ||= "#{self.class.customized_class.table_name}.project_id"
183 183 id_column ||= id
184 184 "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
185 185 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
186 186 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
187 187 " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id_column})"
188 188 end
189 189 end
190 190
191 191 def self.visibility_condition
192 192 if user.admin?
193 193 "1=1"
194 194 elsif user.anonymous?
195 195 "#{table_name}.visible"
196 196 else
197 197 "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
198 198 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
199 199 " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
200 200 " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})"
201 201 end
202 202 end
203 203
204 204 def <=>(field)
205 205 position <=> field.position
206 206 end
207 207
208 208 # Returns the class that values represent
209 209 def value_class
210 210 format.target_class if format.respond_to?(:target_class)
211 211 end
212 212
213 213 def self.customized_class
214 214 self.name =~ /^(.+)CustomField$/
215 215 $1.constantize rescue nil
216 216 end
217 217
218 218 # to move in project_custom_field
219 219 def self.for_all
220 220 where(:is_for_all => true).order('position').all
221 221 end
222 222
223 223 def type_name
224 224 nil
225 225 end
226 226
227 227 # Returns the error messages for the given value
228 228 # or an empty array if value is a valid value for the custom field
229 229 def validate_custom_value(custom_value)
230 230 value = custom_value.value
231 231 errs = []
232 232 if value.is_a?(Array)
233 233 if !multiple?
234 234 errs << ::I18n.t('activerecord.errors.messages.invalid')
235 235 end
236 236 if is_required? && value.detect(&:present?).nil?
237 237 errs << ::I18n.t('activerecord.errors.messages.blank')
238 238 end
239 239 else
240 240 if is_required? && value.blank?
241 241 errs << ::I18n.t('activerecord.errors.messages.blank')
242 242 end
243 243 end
244 if custom_value.value.present?
245 errs += format.validate_custom_value(custom_value)
246 end
244 errs += format.validate_custom_value(custom_value)
247 245 errs
248 246 end
249 247
250 248 # Returns the error messages for the default custom field value
251 249 def validate_field_value(value)
252 250 validate_custom_value(CustomValue.new(:custom_field => self, :value => value))
253 251 end
254 252
255 253 # Returns true if value is a valid value for the custom field
256 254 def valid_field_value?(value)
257 255 validate_field_value(value).empty?
258 256 end
259 257
260 258 def format_in?(*args)
261 259 args.include?(field_format)
262 260 end
263 261
264 262 protected
265 263
266 264 # Removes multiple values for the custom field after setting the multiple attribute to false
267 265 # We kepp the value with the highest id for each customized object
268 266 def handle_multiplicity_change
269 267 if !new_record? && multiple_was && !multiple
270 268 ids = custom_values.
271 269 where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" +
272 270 " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" +
273 271 " AND cve.id > #{CustomValue.table_name}.id)").
274 272 pluck(:id)
275 273
276 274 if ids.any?
277 275 custom_values.where(:id => ids).delete_all
278 276 end
279 277 end
280 278 end
281 279 end
282 280
283 281 require_dependency 'redmine/field_format'
@@ -1,683 +1,684
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module FieldFormat
20 20 def self.add(name, klass)
21 21 all[name.to_s] = klass.instance
22 22 end
23 23
24 24 def self.delete(name)
25 25 all.delete(name.to_s)
26 26 end
27 27
28 28 def self.all
29 29 @formats ||= Hash.new(Base.instance)
30 30 end
31 31
32 32 def self.available_formats
33 33 all.keys
34 34 end
35 35
36 36 def self.find(name)
37 37 all[name.to_s]
38 38 end
39 39
40 40 # Return an array of custom field formats which can be used in select_tag
41 41 def self.as_select(class_name=nil)
42 42 formats = all.values.select do |format|
43 43 format.class.customized_class_names.nil? || format.class.customized_class_names.include?(class_name)
44 44 end
45 45 formats.map {|format| [::I18n.t(format.label), format.name] }.sort_by(&:first)
46 46 end
47 47
48 48 class Base
49 49 include Singleton
50 50 include Redmine::I18n
51 51 include ERB::Util
52 52
53 53 class_attribute :format_name
54 54 self.format_name = nil
55 55
56 56 # Set this to true if the format supports multiple values
57 57 class_attribute :multiple_supported
58 58 self.multiple_supported = false
59 59
60 60 # Set this to true if the format supports textual search on custom values
61 61 class_attribute :searchable_supported
62 62 self.searchable_supported = false
63 63
64 64 # Restricts the classes that the custom field can be added to
65 65 # Set to nil for no restrictions
66 66 class_attribute :customized_class_names
67 67 self.customized_class_names = nil
68 68
69 69 # Name of the partial for editing the custom field
70 70 class_attribute :form_partial
71 71 self.form_partial = nil
72 72
73 73 def self.add(name)
74 74 self.format_name = name
75 75 Redmine::FieldFormat.add(name, self)
76 76 end
77 77 private_class_method :add
78 78
79 79 def self.field_attributes(*args)
80 80 CustomField.store_accessor :format_store, *args
81 81 end
82 82
83 83 field_attributes :url_pattern
84 84
85 85 def name
86 86 self.class.format_name
87 87 end
88 88
89 89 def label
90 90 "label_#{name}"
91 91 end
92 92
93 93 def cast_custom_value(custom_value)
94 94 cast_value(custom_value.custom_field, custom_value.value, custom_value.customized)
95 95 end
96 96
97 97 def cast_value(custom_field, value, customized=nil)
98 98 if value.blank?
99 99 nil
100 100 elsif value.is_a?(Array)
101 101 value.map do |v|
102 102 cast_single_value(custom_field, v, customized)
103 103 end.sort
104 104 else
105 105 cast_single_value(custom_field, value, customized)
106 106 end
107 107 end
108 108
109 109 def cast_single_value(custom_field, value, customized=nil)
110 110 value.to_s
111 111 end
112 112
113 113 def target_class
114 114 nil
115 115 end
116 116
117 117 def possible_custom_value_options(custom_value)
118 118 possible_values_options(custom_value.custom_field, custom_value.customized)
119 119 end
120 120
121 121 def possible_values_options(custom_field, object=nil)
122 122 []
123 123 end
124 124
125 125 # Returns the validation errors for custom_field
126 126 # Should return an empty array if custom_field is valid
127 127 def validate_custom_field(custom_field)
128 128 []
129 129 end
130 130
131 131 # Returns the validation error messages for custom_value
132 132 # Should return an empty array if custom_value is valid
133 133 def validate_custom_value(custom_value)
134 errors = Array.wrap(custom_value.value).reject(&:blank?).map do |value|
134 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
135 errors = values.map do |value|
135 136 validate_single_value(custom_value.custom_field, value, custom_value.customized)
136 137 end
137 138 errors.flatten.uniq
138 139 end
139 140
140 141 def validate_single_value(custom_field, value, customized=nil)
141 142 []
142 143 end
143 144
144 145 def formatted_custom_value(view, custom_value, html=false)
145 146 formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html)
146 147 end
147 148
148 149 def formatted_value(view, custom_field, value, customized=nil, html=false)
149 150 casted = cast_value(custom_field, value, customized)
150 151 if custom_field.url_pattern.present?
151 152 texts_and_urls = Array.wrap(casted).map do |single_value|
152 153 text = view.format_object(single_value, false).to_s
153 154 url = url_from_pattern(custom_field, single_value, customized)
154 155 [text, url]
155 156 end
156 157 links = texts_and_urls.sort_by(&:first).map {|text, url| view.link_to text, url}
157 158 links.join(', ').html_safe
158 159 else
159 160 casted
160 161 end
161 162 end
162 163
163 164 # Returns an URL generated with the custom field URL pattern
164 165 # and variables substitution:
165 166 # %value% => the custom field value
166 167 # %id% => id of the customized object
167 168 # %project_id% => id of the project of the customized object if defined
168 169 # %project_identifier% => identifier of the project of the customized object if defined
169 170 # %m1%, %m2%... => capture groups matches of the custom field regexp if defined
170 171 def url_from_pattern(custom_field, value, customized)
171 172 url = custom_field.url_pattern.to_s.dup
172 173 url.gsub!('%value%') {value.to_s}
173 174 url.gsub!('%id%') {customized.id.to_s}
174 175 url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
175 176 url.gsub!('%project_identifier%') {(customized.respond_to?(:project) ? customized.project.try(:identifier) : nil).to_s}
176 177 if custom_field.regexp.present?
177 178 url.gsub!(%r{%m(\d+)%}) do
178 179 m = $1.to_i
179 180 if matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
180 181 matches[m].to_s
181 182 end
182 183 end
183 184 end
184 185 url
185 186 end
186 187 protected :url_from_pattern
187 188
188 189 def edit_tag(view, tag_id, tag_name, custom_value, options={})
189 190 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id))
190 191 end
191 192
192 193 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
193 194 view.text_field_tag(tag_name, value, options.merge(:id => tag_id)) +
194 195 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
195 196 end
196 197
197 198 def bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
198 199 if custom_field.is_required?
199 200 ''.html_safe
200 201 else
201 202 view.content_tag('label',
202 203 view.check_box_tag(tag_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{tag_id}"}) + l(:button_clear),
203 204 :class => 'inline'
204 205 )
205 206 end
206 207 end
207 208 protected :bulk_clear_tag
208 209
209 210 def query_filter_options(custom_field, query)
210 211 {:type => :string}
211 212 end
212 213
213 214 def before_custom_field_save(custom_field)
214 215 end
215 216
216 217 # Returns a ORDER BY clause that can used to sort customized
217 218 # objects by their value of the custom field.
218 219 # Returns nil if the custom field can not be used for sorting.
219 220 def order_statement(custom_field)
220 221 # COALESCE is here to make sure that blank and NULL values are sorted equally
221 222 "COALESCE(#{join_alias custom_field}.value, '')"
222 223 end
223 224
224 225 # Returns a GROUP BY clause that can used to group by custom value
225 226 # Returns nil if the custom field can not be used for grouping.
226 227 def group_statement(custom_field)
227 228 nil
228 229 end
229 230
230 231 # Returns a JOIN clause that is added to the query when sorting by custom values
231 232 def join_for_order_statement(custom_field)
232 233 alias_name = join_alias(custom_field)
233 234
234 235 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
235 236 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
236 237 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
237 238 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
238 239 " AND (#{custom_field.visibility_by_project_condition})" +
239 240 " AND #{alias_name}.value <> ''" +
240 241 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
241 242 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
242 243 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
243 244 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)"
244 245 end
245 246
246 247 def join_alias(custom_field)
247 248 "cf_#{custom_field.id}"
248 249 end
249 250 protected :join_alias
250 251 end
251 252
252 253 class Unbounded < Base
253 254 def validate_single_value(custom_field, value, customized=nil)
254 255 errs = super
255 if value.present?
256 unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
257 errs << ::I18n.t('activerecord.errors.messages.invalid')
258 end
259 if custom_field.min_length && value.length < custom_field.min_length
260 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => custom_field.min_length)
261 end
262 if custom_field.max_length && custom_field.max_length > 0 && value.length > custom_field.max_length
263 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => custom_field.max_length)
264 end
256 value = value.to_s
257 unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
258 errs << ::I18n.t('activerecord.errors.messages.invalid')
259 end
260 if custom_field.min_length && value.length < custom_field.min_length
261 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => custom_field.min_length)
262 end
263 if custom_field.max_length && custom_field.max_length > 0 && value.length > custom_field.max_length
264 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => custom_field.max_length)
265 265 end
266 266 errs
267 267 end
268 268 end
269 269
270 270 class StringFormat < Unbounded
271 271 add 'string'
272 272 self.searchable_supported = true
273 273 self.form_partial = 'custom_fields/formats/string'
274 274 field_attributes :text_formatting
275 275
276 276 def formatted_value(view, custom_field, value, customized=nil, html=false)
277 277 if html
278 278 if custom_field.url_pattern.present?
279 279 super
280 280 elsif custom_field.text_formatting == 'full'
281 281 view.textilizable(value, :object => customized)
282 282 else
283 283 value.to_s
284 284 end
285 285 else
286 286 value.to_s
287 287 end
288 288 end
289 289 end
290 290
291 291 class TextFormat < Unbounded
292 292 add 'text'
293 293 self.searchable_supported = true
294 294 self.form_partial = 'custom_fields/formats/text'
295 295
296 296 def formatted_value(view, custom_field, value, customized=nil, html=false)
297 297 if html
298 298 if custom_field.text_formatting == 'full'
299 299 view.textilizable(value, :object => customized)
300 300 else
301 301 view.simple_format(html_escape(value))
302 302 end
303 303 else
304 304 value.to_s
305 305 end
306 306 end
307 307
308 308 def edit_tag(view, tag_id, tag_name, custom_value, options={})
309 309 view.text_area_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :rows => 3))
310 310 end
311 311
312 312 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
313 313 view.text_area_tag(tag_name, value, options.merge(:id => tag_id, :rows => 3)) +
314 314 '<br />'.html_safe +
315 315 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
316 316 end
317 317
318 318 def query_filter_options(custom_field, query)
319 319 {:type => :text}
320 320 end
321 321 end
322 322
323 323 class LinkFormat < StringFormat
324 324 add 'link'
325 325 self.searchable_supported = false
326 326 self.form_partial = 'custom_fields/formats/link'
327 327
328 328 def formatted_value(view, custom_field, value, customized=nil, html=false)
329 329 if html
330 330 if custom_field.url_pattern.present?
331 331 url = url_from_pattern(custom_field, value, customized)
332 332 else
333 333 url = value.to_s
334 334 unless url =~ %r{\A[a-z]+://}i
335 335 # no protocol found, use http by default
336 336 url = "http://" + url
337 337 end
338 338 end
339 339 view.link_to value.to_s, url
340 340 else
341 341 value.to_s
342 342 end
343 343 end
344 344 end
345 345
346 346 class Numeric < Unbounded
347 347 self.form_partial = 'custom_fields/formats/numeric'
348 348
349 349 def order_statement(custom_field)
350 350 # Make the database cast values into numeric
351 351 # Postgresql will raise an error if a value can not be casted!
352 352 # CustomValue validations should ensure that it doesn't occur
353 353 "CAST(CASE #{join_alias custom_field}.value WHEN '' THEN '0' ELSE #{join_alias custom_field}.value END AS decimal(30,3))"
354 354 end
355 355 end
356 356
357 357 class IntFormat < Numeric
358 358 add 'int'
359 359
360 360 def label
361 361 "label_integer"
362 362 end
363 363
364 364 def cast_single_value(custom_field, value, customized=nil)
365 365 value.to_i
366 366 end
367 367
368 368 def validate_single_value(custom_field, value, customized=nil)
369 369 errs = super
370 370 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
371 371 errs
372 372 end
373 373
374 374 def query_filter_options(custom_field, query)
375 375 {:type => :integer}
376 376 end
377 377
378 378 def group_statement(custom_field)
379 379 order_statement(custom_field)
380 380 end
381 381 end
382 382
383 383 class FloatFormat < Numeric
384 384 add 'float'
385 385
386 386 def cast_single_value(custom_field, value, customized=nil)
387 387 value.to_f
388 388 end
389 389
390 390 def validate_single_value(custom_field, value, customized=nil)
391 391 errs = super
392 392 errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
393 393 errs
394 394 end
395 395
396 396 def query_filter_options(custom_field, query)
397 397 {:type => :float}
398 398 end
399 399 end
400 400
401 401 class DateFormat < Unbounded
402 402 add 'date'
403 403 self.form_partial = 'custom_fields/formats/date'
404 404
405 405 def cast_single_value(custom_field, value, customized=nil)
406 406 value.to_date rescue nil
407 407 end
408 408
409 409 def validate_single_value(custom_field, value, customized=nil)
410 410 if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)
411 411 []
412 412 else
413 413 [::I18n.t('activerecord.errors.messages.not_a_date')]
414 414 end
415 415 end
416 416
417 417 def edit_tag(view, tag_id, tag_name, custom_value, options={})
418 418 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10)) +
419 419 view.calendar_for(tag_id)
420 420 end
421 421
422 422 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
423 423 view.text_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10)) +
424 424 view.calendar_for(tag_id) +
425 425 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
426 426 end
427 427
428 428 def query_filter_options(custom_field, query)
429 429 {:type => :date}
430 430 end
431 431
432 432 def group_statement(custom_field)
433 433 order_statement(custom_field)
434 434 end
435 435 end
436 436
437 437 class List < Base
438 438 self.multiple_supported = true
439 439 field_attributes :edit_tag_style
440 440
441 441 def edit_tag(view, tag_id, tag_name, custom_value, options={})
442 442 if custom_value.custom_field.edit_tag_style == 'check_box'
443 443 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
444 444 else
445 445 select_edit_tag(view, tag_id, tag_name, custom_value, options)
446 446 end
447 447 end
448 448
449 449 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
450 450 opts = []
451 451 opts << [l(:label_no_change_option), ''] unless custom_field.multiple?
452 452 opts << [l(:label_none), '__none__'] unless custom_field.is_required?
453 453 opts += possible_values_options(custom_field, objects)
454 454 view.select_tag(tag_name, view.options_for_select(opts, value), options.merge(:multiple => custom_field.multiple?))
455 455 end
456 456
457 457 def query_filter_options(custom_field, query)
458 458 {:type => :list_optional, :values => possible_values_options(custom_field, query.project)}
459 459 end
460 460
461 461 protected
462 462
463 463 # Renders the edit tag as a select tag
464 464 def select_edit_tag(view, tag_id, tag_name, custom_value, options={})
465 465 blank_option = ''.html_safe
466 466 unless custom_value.custom_field.multiple?
467 467 if custom_value.custom_field.is_required?
468 468 unless custom_value.custom_field.default_value.present?
469 469 blank_option = view.content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
470 470 end
471 471 else
472 472 blank_option = view.content_tag('option', '&nbsp;'.html_safe, :value => '')
473 473 end
474 474 end
475 475 options_tags = blank_option + view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
476 476 s = view.select_tag(tag_name, options_tags, options.merge(:id => tag_id, :multiple => custom_value.custom_field.multiple?))
477 477 if custom_value.custom_field.multiple?
478 478 s << view.hidden_field_tag(tag_name, '')
479 479 end
480 480 s
481 481 end
482 482
483 483 # Renders the edit tag as check box or radio tags
484 484 def check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
485 485 opts = []
486 486 unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required?
487 487 opts << ["(#{l(:label_none)})", '']
488 488 end
489 489 opts += possible_custom_value_options(custom_value)
490 490 s = ''.html_safe
491 491 tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag
492 492 opts.each do |label, value|
493 493 value ||= label
494 494 checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value
495 495 tag = view.send(tag_method, tag_name, value, checked, :id => tag_id)
496 496 # set the id on the first tag only
497 497 tag_id = nil
498 498 s << view.content_tag('label', tag + ' ' + label)
499 499 end
500 500 css = "#{options[:class]} check_box_group"
501 501 view.content_tag('span', s, options.merge(:class => css))
502 502 end
503 503 end
504 504
505 505 class ListFormat < List
506 506 add 'list'
507 507 self.searchable_supported = true
508 508 self.form_partial = 'custom_fields/formats/list'
509 509
510 510 def possible_custom_value_options(custom_value)
511 511 options = possible_values_options(custom_value.custom_field)
512 512 missing = [custom_value.value].flatten.reject(&:blank?) - options
513 513 if missing.any?
514 514 options += missing
515 515 end
516 516 options
517 517 end
518 518
519 519 def possible_values_options(custom_field, object=nil)
520 520 custom_field.possible_values
521 521 end
522 522
523 523 def validate_custom_field(custom_field)
524 524 errors = []
525 525 errors << [:possible_values, :blank] if custom_field.possible_values.blank?
526 526 errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array
527 527 errors
528 528 end
529 529
530 530 def validate_custom_value(custom_value)
531 invalid_values = Array.wrap(custom_value.value) - Array.wrap(custom_value.value_was) - custom_value.custom_field.possible_values
532 if invalid_values.select(&:present?).any?
531 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
532 invalid_values = values - Array.wrap(custom_value.value_was) - custom_value.custom_field.possible_values
533 if invalid_values.any?
533 534 [::I18n.t('activerecord.errors.messages.inclusion')]
534 535 else
535 536 []
536 537 end
537 538 end
538 539
539 540 def group_statement(custom_field)
540 541 order_statement(custom_field)
541 542 end
542 543 end
543 544
544 545 class BoolFormat < List
545 546 add 'bool'
546 547 self.multiple_supported = false
547 548 self.form_partial = 'custom_fields/formats/bool'
548 549
549 550 def label
550 551 "label_boolean"
551 552 end
552 553
553 554 def cast_single_value(custom_field, value, customized=nil)
554 555 value == '1' ? true : false
555 556 end
556 557
557 558 def possible_values_options(custom_field, object=nil)
558 559 [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
559 560 end
560 561
561 562 def group_statement(custom_field)
562 563 order_statement(custom_field)
563 564 end
564 565 end
565 566
566 567 class RecordList < List
567 568 self.customized_class_names = %w(Issue TimeEntry Version Project)
568 569
569 570 def cast_single_value(custom_field, value, customized=nil)
570 571 target_class.find_by_id(value.to_i) if value.present?
571 572 end
572 573
573 574 def target_class
574 575 @target_class ||= self.class.name[/^(.*::)?(.+)Format$/, 2].constantize rescue nil
575 576 end
576 577
577 578 def possible_custom_value_options(custom_value)
578 579 options = possible_values_options(custom_value.custom_field, custom_value.customized)
579 580 missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last)
580 581 if missing.any?
581 582 options += target_class.where(:id => missing.map(&:to_i)).map {|o| [o.to_s, o.id.to_s]}
582 583 #TODO: use #sort_by! when ruby1.8 support is dropped
583 584 options = options.sort_by(&:first)
584 585 end
585 586 options
586 587 end
587 588
588 589 def order_statement(custom_field)
589 590 if target_class.respond_to?(:fields_for_order_statement)
590 591 target_class.fields_for_order_statement(value_join_alias(custom_field))
591 592 end
592 593 end
593 594
594 595 def group_statement(custom_field)
595 596 "COALESCE(#{join_alias custom_field}.value, '')"
596 597 end
597 598
598 599 def join_for_order_statement(custom_field)
599 600 alias_name = join_alias(custom_field)
600 601
601 602 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
602 603 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
603 604 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
604 605 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
605 606 " AND (#{custom_field.visibility_by_project_condition})" +
606 607 " AND #{alias_name}.value <> ''" +
607 608 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
608 609 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
609 610 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
610 611 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)" +
611 612 " LEFT OUTER JOIN #{target_class.table_name} #{value_join_alias custom_field}" +
612 613 " ON CAST(CASE #{alias_name}.value WHEN '' THEN '0' ELSE #{alias_name}.value END AS decimal(30,0)) = #{value_join_alias custom_field}.id"
613 614 end
614 615
615 616 def value_join_alias(custom_field)
616 617 join_alias(custom_field) + "_" + custom_field.field_format
617 618 end
618 619 protected :value_join_alias
619 620 end
620 621
621 622 class UserFormat < RecordList
622 623 add 'user'
623 624 self.form_partial = 'custom_fields/formats/user'
624 625 field_attributes :user_role
625 626
626 627 def possible_values_options(custom_field, object=nil)
627 628 if object.is_a?(Array)
628 629 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
629 630 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
630 631 elsif object.respond_to?(:project) && object.project
631 632 scope = object.project.users
632 633 if custom_field.user_role.is_a?(Array)
633 634 role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i)
634 635 if role_ids.any?
635 636 scope = scope.where("#{Member.table_name}.id IN (SELECT DISTINCT member_id FROM #{MemberRole.table_name} WHERE role_id IN (?))", role_ids)
636 637 end
637 638 end
638 639 scope.sorted.collect {|u| [u.to_s, u.id.to_s]}
639 640 else
640 641 []
641 642 end
642 643 end
643 644
644 645 def before_custom_field_save(custom_field)
645 646 super
646 647 if custom_field.user_role.is_a?(Array)
647 648 custom_field.user_role.map!(&:to_s).reject!(&:blank?)
648 649 end
649 650 end
650 651 end
651 652
652 653 class VersionFormat < RecordList
653 654 add 'version'
654 655 self.form_partial = 'custom_fields/formats/version'
655 656 field_attributes :version_status
656 657
657 658 def possible_values_options(custom_field, object=nil)
658 659 if object.is_a?(Array)
659 660 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
660 661 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
661 662 elsif object.respond_to?(:project) && object.project
662 663 scope = object.project.shared_versions
663 664 if custom_field.version_status.is_a?(Array)
664 665 statuses = custom_field.version_status.map(&:to_s).reject(&:blank?)
665 666 if statuses.any?
666 667 scope = scope.where(:status => statuses.map(&:to_s))
667 668 end
668 669 end
669 670 scope.sort.collect {|u| [u.to_s, u.id.to_s]}
670 671 else
671 672 []
672 673 end
673 674 end
674 675
675 676 def before_custom_field_save(custom_field)
676 677 super
677 678 if custom_field.version_status.is_a?(Array)
678 679 custom_field.version_status.map!(&:to_s).reject!(&:blank?)
679 680 end
680 681 end
681 682 end
682 683 end
683 684 end
@@ -1,310 +1,318
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class CustomFieldTest < ActiveSupport::TestCase
21 21 fixtures :custom_fields
22 22
23 23 def test_create
24 24 field = UserCustomField.new(:name => 'Money money money', :field_format => 'float')
25 25 assert field.save
26 26 end
27 27
28 28 def test_before_validation
29 29 field = CustomField.new(:name => 'test_before_validation', :field_format => 'int')
30 30 field.searchable = true
31 31 assert field.save
32 32 assert_equal false, field.searchable
33 33 field.searchable = true
34 34 assert field.save
35 35 assert_equal false, field.searchable
36 36 end
37 37
38 38 def test_regexp_validation
39 39 field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9')
40 40 assert !field.save
41 41 assert_include I18n.t('activerecord.errors.messages.invalid'),
42 42 field.errors[:regexp]
43 43 field.regexp = '[a-z0-9]'
44 44 assert field.save
45 45 end
46 46
47 47 def test_default_value_should_be_validated
48 48 field = CustomField.new(:name => 'Test', :field_format => 'int')
49 49 field.default_value = 'abc'
50 50 assert !field.valid?
51 51 field.default_value = '6'
52 52 assert field.valid?
53 53 end
54 54
55 55 def test_default_value_should_not_be_validated_when_blank
56 56 field = CustomField.new(:name => 'Test', :field_format => 'list', :possible_values => ['a', 'b'], :is_required => true, :default_value => '')
57 57 assert field.valid?
58 58 end
59 59
60 60 def test_field_format_should_be_validated
61 61 field = CustomField.new(:name => 'Test', :field_format => 'foo')
62 62 assert !field.valid?
63 63 end
64 64
65 65 def test_field_format_validation_should_accept_formats_added_at_runtime
66 66 Redmine::FieldFormat.add 'foobar', Class.new(Redmine::FieldFormat::Base)
67 67
68 68 field = CustomField.new(:name => 'Some Custom Field', :field_format => 'foobar')
69 69 assert field.valid?, 'field should be valid'
70 70 ensure
71 71 Redmine::FieldFormat.delete 'foobar'
72 72 end
73 73
74 74 def test_should_not_change_field_format_of_existing_custom_field
75 75 field = CustomField.find(1)
76 76 field.field_format = 'int'
77 77 assert_equal 'list', field.field_format
78 78 end
79 79
80 80 def test_possible_values_should_accept_an_array
81 81 field = CustomField.new
82 82 field.possible_values = ["One value", ""]
83 83 assert_equal ["One value"], field.possible_values
84 84 end
85 85
86 86 def test_possible_values_should_accept_a_string
87 87 field = CustomField.new
88 88 field.possible_values = "One value"
89 89 assert_equal ["One value"], field.possible_values
90 90 end
91 91
92 92 def test_possible_values_should_accept_a_multiline_string
93 93 field = CustomField.new
94 94 field.possible_values = "One value\nAnd another one \r\n \n"
95 95 assert_equal ["One value", "And another one"], field.possible_values
96 96 end
97 97
98 98 if "string".respond_to?(:encoding)
99 99 def test_possible_values_stored_as_binary_should_be_utf8_encoded
100 100 field = CustomField.find(11)
101 101 assert_kind_of Array, field.possible_values
102 102 assert field.possible_values.size > 0
103 103 field.possible_values.each do |value|
104 104 assert_equal "UTF-8", value.encoding.name
105 105 end
106 106 end
107 107 end
108 108
109 109 def test_destroy
110 110 field = CustomField.find(1)
111 111 assert field.destroy
112 112 end
113 113
114 114 def test_new_subclass_instance_should_return_an_instance
115 115 f = CustomField.new_subclass_instance('IssueCustomField')
116 116 assert_kind_of IssueCustomField, f
117 117 end
118 118
119 119 def test_new_subclass_instance_should_set_attributes
120 120 f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test')
121 121 assert_kind_of IssueCustomField, f
122 122 assert_equal 'Test', f.name
123 123 end
124 124
125 125 def test_new_subclass_instance_with_invalid_class_name_should_return_nil
126 126 assert_nil CustomField.new_subclass_instance('WrongClassName')
127 127 end
128 128
129 129 def test_new_subclass_instance_with_non_subclass_name_should_return_nil
130 130 assert_nil CustomField.new_subclass_instance('Project')
131 131 end
132 132
133 133 def test_string_field_validation_with_blank_value
134 134 f = CustomField.new(:field_format => 'string')
135 135
136 136 assert f.valid_field_value?(nil)
137 137 assert f.valid_field_value?('')
138 138
139 139 f.is_required = true
140 140 assert !f.valid_field_value?(nil)
141 141 assert !f.valid_field_value?('')
142 142 end
143 143
144 144 def test_string_field_validation_with_min_and_max_lengths
145 145 f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5)
146 146
147 147 assert f.valid_field_value?(nil)
148 148 assert f.valid_field_value?('')
149 assert !f.valid_field_value?(' ')
149 150 assert f.valid_field_value?('a' * 2)
150 151 assert !f.valid_field_value?('a')
151 152 assert !f.valid_field_value?('a' * 6)
152 153 end
153 154
154 155 def test_string_field_validation_with_regexp
155 156 f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$')
156 157
157 158 assert f.valid_field_value?(nil)
158 159 assert f.valid_field_value?('')
160 assert !f.valid_field_value?(' ')
159 161 assert f.valid_field_value?('ABC')
160 162 assert !f.valid_field_value?('abc')
161 163 end
162 164
163 165 def test_date_field_validation
164 166 f = CustomField.new(:field_format => 'date')
165 167
166 168 assert f.valid_field_value?(nil)
167 169 assert f.valid_field_value?('')
170 assert !f.valid_field_value?(' ')
168 171 assert f.valid_field_value?('1975-07-14')
169 172 assert !f.valid_field_value?('1975-07-33')
170 173 assert !f.valid_field_value?('abc')
171 174 end
172 175
173 176 def test_list_field_validation
174 177 f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2'])
175 178
176 179 assert f.valid_field_value?(nil)
177 180 assert f.valid_field_value?('')
181 assert !f.valid_field_value?(' ')
178 182 assert f.valid_field_value?('value2')
179 183 assert !f.valid_field_value?('abc')
180 184 end
181 185
182 186 def test_int_field_validation
183 187 f = CustomField.new(:field_format => 'int')
184 188
185 189 assert f.valid_field_value?(nil)
186 190 assert f.valid_field_value?('')
191 assert !f.valid_field_value?(' ')
187 192 assert f.valid_field_value?('123')
188 193 assert f.valid_field_value?('+123')
189 194 assert f.valid_field_value?('-123')
190 195 assert !f.valid_field_value?('6abc')
191 196 end
192 197
193 198 def test_float_field_validation
194 199 f = CustomField.new(:field_format => 'float')
195 200
196 201 assert f.valid_field_value?(nil)
197 202 assert f.valid_field_value?('')
203 assert !f.valid_field_value?(' ')
198 204 assert f.valid_field_value?('11.2')
199 205 assert f.valid_field_value?('-6.250')
200 206 assert f.valid_field_value?('5')
201 207 assert !f.valid_field_value?('6abc')
202 208 end
203 209
204 210 def test_multi_field_validation
205 211 f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2'])
206 212
207 213 assert f.valid_field_value?(nil)
208 214 assert f.valid_field_value?('')
215 assert !f.valid_field_value?(' ')
209 216 assert f.valid_field_value?([])
210 217 assert f.valid_field_value?([nil])
211 218 assert f.valid_field_value?([''])
219 assert !f.valid_field_value?([' '])
212 220
213 221 assert f.valid_field_value?('value2')
214 222 assert !f.valid_field_value?('abc')
215 223
216 224 assert f.valid_field_value?(['value2'])
217 225 assert !f.valid_field_value?(['abc'])
218 226
219 227 assert f.valid_field_value?(['', 'value2'])
220 228 assert !f.valid_field_value?(['', 'abc'])
221 229
222 230 assert f.valid_field_value?(['value1', 'value2'])
223 231 assert !f.valid_field_value?(['value1', 'abc'])
224 232 end
225 233
226 234 def test_changing_multiple_to_false_should_delete_multiple_values
227 235 field = ProjectCustomField.create!(:name => 'field', :field_format => 'list', :multiple => 'true', :possible_values => ['field1', 'field2'])
228 236 other = ProjectCustomField.create!(:name => 'other', :field_format => 'list', :multiple => 'true', :possible_values => ['other1', 'other2'])
229 237
230 238 item_with_multiple_values = Project.generate!(:custom_field_values => {field.id => ['field1', 'field2'], other.id => ['other1', 'other2']})
231 239 item_with_single_values = Project.generate!(:custom_field_values => {field.id => ['field1'], other.id => ['other2']})
232 240
233 241 assert_difference 'CustomValue.count', -1 do
234 242 field.multiple = false
235 243 field.save!
236 244 end
237 245
238 246 item_with_multiple_values = Project.find(item_with_multiple_values.id)
239 247 assert_kind_of String, item_with_multiple_values.custom_field_value(field)
240 248 assert_kind_of Array, item_with_multiple_values.custom_field_value(other)
241 249 assert_equal 2, item_with_multiple_values.custom_field_value(other).size
242 250 end
243 251
244 252 def test_value_class_should_return_the_class_used_for_fields_values
245 253 assert_equal User, CustomField.new(:field_format => 'user').value_class
246 254 assert_equal Version, CustomField.new(:field_format => 'version').value_class
247 255 end
248 256
249 257 def test_value_class_should_return_nil_for_other_fields
250 258 assert_nil CustomField.new(:field_format => 'text').value_class
251 259 assert_nil CustomField.new.value_class
252 260 end
253 261
254 262 def test_value_from_keyword_for_list_custom_field
255 263 field = CustomField.find(1)
256 264 assert_equal 'PostgreSQL', field.value_from_keyword('postgresql', Issue.find(1))
257 265 end
258 266
259 267 def test_visibile_scope_with_admin_should_return_all_custom_fields
260 268 CustomField.delete_all
261 269 fields = [
262 270 CustomField.generate!(:visible => true),
263 271 CustomField.generate!(:visible => false),
264 272 CustomField.generate!(:visible => false, :role_ids => [1, 3]),
265 273 CustomField.generate!(:visible => false, :role_ids => [1, 2]),
266 274 ]
267 275
268 276 assert_equal 4, CustomField.visible(User.find(1)).count
269 277 end
270 278
271 279 def test_visibile_scope_with_non_admin_user_should_return_visible_custom_fields
272 280 CustomField.delete_all
273 281 fields = [
274 282 CustomField.generate!(:visible => true),
275 283 CustomField.generate!(:visible => false),
276 284 CustomField.generate!(:visible => false, :role_ids => [1, 3]),
277 285 CustomField.generate!(:visible => false, :role_ids => [1, 2]),
278 286 ]
279 287 user = User.generate!
280 288 User.add_to_project(user, Project.first, Role.find(3))
281 289
282 290 assert_equal [fields[0], fields[2]], CustomField.visible(user).order("id").to_a
283 291 end
284 292
285 293 def test_visibile_scope_with_anonymous_user_should_return_visible_custom_fields
286 294 CustomField.delete_all
287 295 fields = [
288 296 CustomField.generate!(:visible => true),
289 297 CustomField.generate!(:visible => false),
290 298 CustomField.generate!(:visible => false, :role_ids => [1, 3]),
291 299 CustomField.generate!(:visible => false, :role_ids => [1, 2]),
292 300 ]
293 301
294 302 assert_equal [fields[0]], CustomField.visible(User.anonymous).order("id").to_a
295 303 end
296 304
297 305 def test_float_cast_blank_value_should_return_nil
298 306 field = CustomField.new(:field_format => 'float')
299 307 assert_equal nil, field.cast_value(nil)
300 308 assert_equal nil, field.cast_value('')
301 309 end
302 310
303 311 def test_float_cast_valid_value_should_return_float
304 312 field = CustomField.new(:field_format => 'float')
305 313 assert_equal 12.0, field.cast_value('12')
306 314 assert_equal 12.5, field.cast_value('12.5')
307 315 assert_equal 12.5, field.cast_value('+12.5')
308 316 assert_equal -12.5, field.cast_value('-12.5')
309 317 end
310 318 end
General Comments 0
You need to be logged in to leave comments. Login now