##// END OF EJS Templates
Merged r6298 from trunk (#8865)....
Jean-Philippe Lang -
r7648:f22ff1f1c2d8
parent child
Show More
@@ -1,151 +1,159
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 validates_presence_of :name, :field_format
24 24 validates_uniqueness_of :name, :scope => :type
25 25 validates_length_of :name, :maximum => 30
26 26 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
27 27
28 28 def initialize(attributes = nil)
29 29 super
30 30 self.possible_values ||= []
31 31 end
32 32
33 33 def before_validation
34 34 # make sure these fields are not searchable
35 35 self.searchable = false if %w(int float date bool).include?(field_format)
36 36 true
37 37 end
38 38
39 39 def validate
40 40 if self.field_format == "list"
41 41 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
42 42 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
43 43 end
44 44
45 if regexp.present?
46 begin
47 Regexp.new(regexp)
48 rescue
49 errors.add(:regexp, :invalid)
50 end
51 end
52
45 53 # validate default value
46 54 v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil)
47 55 v.custom_field.is_required = false
48 56 errors.add(:default_value, :invalid) unless v.valid?
49 57 end
50 58
51 59 def possible_values_options(obj=nil)
52 60 case field_format
53 61 when 'user', 'version'
54 62 if obj.respond_to?(:project) && obj.project
55 63 case field_format
56 64 when 'user'
57 65 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
58 66 when 'version'
59 67 obj.project.versions.sort.collect {|u| [u.to_s, u.id.to_s]}
60 68 end
61 69 elsif obj.is_a?(Array)
62 70 obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v}
63 71 else
64 72 []
65 73 end
66 74 else
67 75 read_attribute :possible_values
68 76 end
69 77 end
70 78
71 79 def possible_values(obj=nil)
72 80 case field_format
73 81 when 'user', 'version'
74 82 possible_values_options(obj).collect(&:last)
75 83 else
76 84 read_attribute :possible_values
77 85 end
78 86 end
79 87
80 88 # Makes possible_values accept a multiline string
81 89 def possible_values=(arg)
82 90 if arg.is_a?(Array)
83 91 write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?})
84 92 else
85 93 self.possible_values = arg.to_s.split(/[\n\r]+/)
86 94 end
87 95 end
88 96
89 97 def cast_value(value)
90 98 casted = nil
91 99 unless value.blank?
92 100 case field_format
93 101 when 'string', 'text', 'list'
94 102 casted = value
95 103 when 'date'
96 104 casted = begin; value.to_date; rescue; nil end
97 105 when 'bool'
98 106 casted = (value == '1' ? true : false)
99 107 when 'int'
100 108 casted = value.to_i
101 109 when 'float'
102 110 casted = value.to_f
103 111 when 'user', 'version'
104 112 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
105 113 end
106 114 end
107 115 casted
108 116 end
109 117
110 118 # Returns a ORDER BY clause that can used to sort customized
111 119 # objects by their value of the custom field.
112 120 # Returns false, if the custom field can not be used for sorting.
113 121 def order_statement
114 122 case field_format
115 123 when 'string', 'text', 'list', 'date', 'bool'
116 124 # COALESCE is here to make sure that blank and NULL values are sorted equally
117 125 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
118 126 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
119 127 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
120 128 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
121 129 when 'int', 'float'
122 130 # Make the database cast values into numeric
123 131 # Postgresql will raise an error if a value can not be casted!
124 132 # CustomValue validations should ensure that it doesn't occur
125 133 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
126 134 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
127 135 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
128 136 " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
129 137 else
130 138 nil
131 139 end
132 140 end
133 141
134 142 def <=>(field)
135 143 position <=> field.position
136 144 end
137 145
138 146 def self.customized_class
139 147 self.name =~ /^(.+)CustomField$/
140 148 begin; $1.constantize; rescue nil; end
141 149 end
142 150
143 151 # to move in project_custom_field
144 152 def self.for_all
145 153 find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
146 154 end
147 155
148 156 def type_name
149 157 nil
150 158 end
151 159 end
@@ -1,50 +1,59
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class CustomFieldTest < ActiveSupport::TestCase
21 21 fixtures :custom_fields
22 22
23 23 def test_create
24 24 field = UserCustomField.new(:name => 'Money money money', :field_format => 'float')
25 25 assert field.save
26 26 end
27 27
28 def test_regexp_validation
29 field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9')
30 assert !field.save
31 assert_equal I18n.t('activerecord.errors.messages.invalid'), field.errors.on(:regexp)
32
33 field.regexp = '[a-z0-9]'
34 assert field.save
35 end
36
28 37 def test_possible_values_should_accept_an_array
29 38 field = CustomField.new
30 39 field.possible_values = ["One value", ""]
31 40 assert_equal ["One value"], field.possible_values
32 41 end
33 42
34 43 def test_possible_values_should_accept_a_string
35 44 field = CustomField.new
36 45 field.possible_values = "One value"
37 46 assert_equal ["One value"], field.possible_values
38 47 end
39 48
40 49 def test_possible_values_should_accept_a_multiline_string
41 50 field = CustomField.new
42 51 field.possible_values = "One value\nAnd another one \r\n \n"
43 52 assert_equal ["One value", "And another one"], field.possible_values
44 53 end
45 54
46 55 def test_destroy
47 56 field = CustomField.find(1)
48 57 assert field.destroy
49 58 end
50 59 end
General Comments 0
You need to be logged in to leave comments. Login now