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