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