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