##// END OF EJS Templates
Fixed: can not save numeric, date and boolean custom fields (broken by r994)....
Jean-Philippe Lang -
r983:777edc13c137
parent child
Show More
@@ -1,69 +1,70
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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 has_many :custom_values, :dependent => :delete_all
20 20 acts_as_list :scope => 'type = \'#{self.class}\''
21 21 serialize :possible_values
22 22
23 23 FIELD_FORMATS = { "string" => { :name => :label_string, :order => 1 },
24 24 "text" => { :name => :label_text, :order => 2 },
25 25 "int" => { :name => :label_integer, :order => 3 },
26 26 "float" => { :name => :label_float, :order => 4 },
27 27 "list" => { :name => :label_list, :order => 5 },
28 28 "date" => { :name => :label_date, :order => 6 },
29 29 "bool" => { :name => :label_boolean, :order => 7 }
30 30 }.freeze
31 31
32 32 validates_presence_of :name, :field_format
33 33 validates_uniqueness_of :name
34 34 validates_length_of :name, :maximum => 30
35 35 validates_format_of :name, :with => /^[\w\s\'\-]*$/i
36 36 validates_inclusion_of :field_format, :in => FIELD_FORMATS.keys
37 37
38 38 def initialize(attributes = nil)
39 39 super
40 40 self.possible_values ||= []
41 41 end
42 42
43 43 def before_validation
44 44 # remove empty values
45 45 self.possible_values = self.possible_values.collect{|v| v unless v.empty?}.compact
46 46 # make sure these fields are not searchable
47 47 self.searchable = false if %w(int float date bool).include?(field_format)
48 true
48 49 end
49 50
50 51 def validate
51 52 if self.field_format == "list"
52 53 errors.add(:possible_values, :activerecord_error_blank) if self.possible_values.nil? || self.possible_values.empty?
53 54 errors.add(:possible_values, :activerecord_error_invalid) unless self.possible_values.is_a? Array
54 55 end
55 56 end
56 57
57 58 def <=>(field)
58 59 position <=> field.position
59 60 end
60 61
61 62 # to move in project_custom_field
62 63 def self.for_all
63 64 find(:all, :conditions => ["is_for_all=?", true])
64 65 end
65 66
66 67 def type_name
67 68 nil
68 69 end
69 70 end
General Comments 0
You need to be logged in to leave comments. Login now