##// END OF EJS Templates
Allow same name for custom fields on different object types....
Jean-Philippe Lang -
r1729:163101902608
parent child
Show More
@@ -1,75 +1,75
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, :scope => :type
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 true
49 end
49 end
50
50
51 def validate
51 def validate
52 if self.field_format == "list"
52 if self.field_format == "list"
53 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?
54 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
55 end
55 end
56
56
57 # validate default value
57 # validate default value
58 v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil)
58 v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil)
59 v.custom_field.is_required = false
59 v.custom_field.is_required = false
60 errors.add(:default_value, :activerecord_error_invalid) unless v.valid?
60 errors.add(:default_value, :activerecord_error_invalid) unless v.valid?
61 end
61 end
62
62
63 def <=>(field)
63 def <=>(field)
64 position <=> field.position
64 position <=> field.position
65 end
65 end
66
66
67 # to move in project_custom_field
67 # to move in project_custom_field
68 def self.for_all
68 def self.for_all
69 find(:all, :conditions => ["is_for_all=?", true])
69 find(:all, :conditions => ["is_for_all=?", true])
70 end
70 end
71
71
72 def type_name
72 def type_name
73 nil
73 nil
74 end
74 end
75 end
75 end
General Comments 0
You need to be logged in to leave comments. Login now