comparison app/models/custom_value.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents cbb26bc654de
children 622f24f53b42
comparison
equal deleted inserted replaced
929:5f33065ddc4b 1115:433d4f72a19b
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang 2 # Copyright (C) 2006-2012 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.
17 17
18 class CustomValue < ActiveRecord::Base 18 class CustomValue < ActiveRecord::Base
19 belongs_to :custom_field 19 belongs_to :custom_field
20 belongs_to :customized, :polymorphic => true 20 belongs_to :customized, :polymorphic => true
21 21
22 validate :validate_custom_value 22 def initialize(attributes=nil, *args)
23 23 super
24 def after_initialize
25 if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?)) 24 if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?))
26 self.value ||= custom_field.default_value 25 self.value ||= custom_field.default_value
27 end 26 end
28 end 27 end
29 28
45 end 44 end
46 45
47 def to_s 46 def to_s
48 value.to_s 47 value.to_s
49 end 48 end
50
51 protected
52 def validate_custom_value
53 if value.blank?
54 errors.add(:value, :blank) if custom_field.is_required? and value.blank?
55 else
56 errors.add(:value, :invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
57 errors.add(:value, :too_short, :count => custom_field.min_length) if custom_field.min_length > 0 and value.length < custom_field.min_length
58 errors.add(:value, :too_long, :count => custom_field.max_length) if custom_field.max_length > 0 and value.length > custom_field.max_length
59
60 # Format specific validations
61 case custom_field.field_format
62 when 'int'
63 errors.add(:value, :not_a_number) unless value =~ /^[+-]?\d+$/
64 when 'float'
65 begin; Kernel.Float(value); rescue; errors.add(:value, :invalid) end
66 when 'date'
67 errors.add(:value, :not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
68 when 'list'
69 errors.add(:value, :inclusion) unless custom_field.possible_values.include?(value)
70 end
71 end
72 end
73 end 49 end