##// 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 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 validates_uniqueness_of :name
33 validates_uniqueness_of :name, :scope => :type
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 48 true
49 49 end
50 50
51 51 def validate
52 52 if self.field_format == "list"
53 53 errors.add(:possible_values, :activerecord_error_blank) if self.possible_values.nil? || self.possible_values.empty?
54 54 errors.add(:possible_values, :activerecord_error_invalid) unless self.possible_values.is_a? Array
55 55 end
56 56
57 57 # validate default value
58 58 v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil)
59 59 v.custom_field.is_required = false
60 60 errors.add(:default_value, :activerecord_error_invalid) unless v.valid?
61 61 end
62 62
63 63 def <=>(field)
64 64 position <=> field.position
65 65 end
66 66
67 67 # to move in project_custom_field
68 68 def self.for_all
69 69 find(:all, :conditions => ["is_for_all=?", true])
70 70 end
71 71
72 72 def type_name
73 73 nil
74 74 end
75 75 end
General Comments 0
You need to be logged in to leave comments. Login now