Mercurial > hg > soundsoftware-site
comparison .svn/pristine/c5/c5caa31d1ab1ccf81fe14cfaca2195b9d5a737f4.svn-base @ 909:cbb26bc654de redmine-1.3
Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author | Chris Cannam |
---|---|
date | Fri, 24 Feb 2012 19:09:32 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
908:c6c2cbd0afee | 909:cbb26bc654de |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
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 | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 class CustomValue < ActiveRecord::Base | |
19 belongs_to :custom_field | |
20 belongs_to :customized, :polymorphic => true | |
21 | |
22 validate :validate_custom_value | |
23 | |
24 def after_initialize | |
25 if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?)) | |
26 self.value ||= custom_field.default_value | |
27 end | |
28 end | |
29 | |
30 # Returns true if the boolean custom value is true | |
31 def true? | |
32 self.value == '1' | |
33 end | |
34 | |
35 def editable? | |
36 custom_field.editable? | |
37 end | |
38 | |
39 def visible? | |
40 custom_field.visible? | |
41 end | |
42 | |
43 def required? | |
44 custom_field.is_required? | |
45 end | |
46 | |
47 def to_s | |
48 value.to_s | |
49 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 |