@@ -5,12 +5,12 | |||||
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. | |
@@ -19,7 +19,7 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 | validates_presence_of :name, :field_format |
|
23 | validates_presence_of :name, :field_format | |
24 | validates_uniqueness_of :name, :scope => :type |
|
24 | validates_uniqueness_of :name, :scope => :type | |
25 | validates_length_of :name, :maximum => 30 |
|
25 | validates_length_of :name, :maximum => 30 | |
@@ -29,19 +29,19 class CustomField < ActiveRecord::Base | |||||
29 | super |
|
29 | super | |
30 | self.possible_values ||= [] |
|
30 | self.possible_values ||= [] | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | def before_validation |
|
33 | def before_validation | |
34 | # make sure these fields are not searchable |
|
34 | # make sure these fields are not searchable | |
35 | self.searchable = false if %w(int float date bool).include?(field_format) |
|
35 | self.searchable = false if %w(int float date bool).include?(field_format) | |
36 | true |
|
36 | true | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def validate |
|
39 | def validate | |
40 | if self.field_format == "list" |
|
40 | if self.field_format == "list" | |
41 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? |
|
41 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? | |
42 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array |
|
42 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array | |
43 | end |
|
43 | end | |
44 |
|
44 | |||
45 | if regexp.present? |
|
45 | if regexp.present? | |
46 | begin |
|
46 | begin | |
47 | Regexp.new(regexp) |
|
47 | Regexp.new(regexp) | |
@@ -49,13 +49,13 class CustomField < ActiveRecord::Base | |||||
49 | errors.add(:regexp, :invalid) |
|
49 | errors.add(:regexp, :invalid) | |
50 | end |
|
50 | end | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | # validate default value |
|
53 | # validate default value | |
54 | v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil) |
|
54 | v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil) | |
55 | v.custom_field.is_required = false |
|
55 | v.custom_field.is_required = false | |
56 | errors.add(:default_value, :invalid) unless v.valid? |
|
56 | errors.add(:default_value, :invalid) unless v.valid? | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | def possible_values_options(obj=nil) |
|
59 | def possible_values_options(obj=nil) | |
60 | case field_format |
|
60 | case field_format | |
61 | when 'user', 'version' |
|
61 | when 'user', 'version' | |
@@ -75,7 +75,7 class CustomField < ActiveRecord::Base | |||||
75 | read_attribute :possible_values |
|
75 | read_attribute :possible_values | |
76 | end |
|
76 | end | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | def possible_values(obj=nil) |
|
79 | def possible_values(obj=nil) | |
80 | case field_format |
|
80 | case field_format | |
81 | when 'user', 'version' |
|
81 | when 'user', 'version' | |
@@ -84,7 +84,7 class CustomField < ActiveRecord::Base | |||||
84 | read_attribute :possible_values |
|
84 | read_attribute :possible_values | |
85 | end |
|
85 | end | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | # Makes possible_values accept a multiline string |
|
88 | # Makes possible_values accept a multiline string | |
89 | def possible_values=(arg) |
|
89 | def possible_values=(arg) | |
90 | if arg.is_a?(Array) |
|
90 | if arg.is_a?(Array) | |
@@ -93,7 +93,7 class CustomField < ActiveRecord::Base | |||||
93 | self.possible_values = arg.to_s.split(/[\n\r]+/) |
|
93 | self.possible_values = arg.to_s.split(/[\n\r]+/) | |
94 | end |
|
94 | end | |
95 | end |
|
95 | end | |
96 |
|
96 | |||
97 | def cast_value(value) |
|
97 | def cast_value(value) | |
98 | casted = nil |
|
98 | casted = nil | |
99 | unless value.blank? |
|
99 | unless value.blank? | |
@@ -114,7 +114,7 class CustomField < ActiveRecord::Base | |||||
114 | end |
|
114 | end | |
115 | casted |
|
115 | casted | |
116 | end |
|
116 | end | |
117 |
|
117 | |||
118 | # Returns a ORDER BY clause that can used to sort customized |
|
118 | # Returns a ORDER BY clause that can used to sort customized | |
119 | # objects by their value of the custom field. |
|
119 | # objects by their value of the custom field. | |
120 | # Returns false, if the custom field can not be used for sorting. |
|
120 | # Returns false, if the custom field can not be used for sorting. | |
@@ -122,7 +122,7 class CustomField < ActiveRecord::Base | |||||
122 | case field_format |
|
122 | case field_format | |
123 | when 'string', 'text', 'list', 'date', 'bool' |
|
123 | when 'string', 'text', 'list', 'date', 'bool' | |
124 | # COALESCE is here to make sure that blank and NULL values are sorted equally |
|
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 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + |
|
126 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + | |
127 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + |
|
127 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + | |
128 | " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" |
|
128 | " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" | |
@@ -130,7 +130,7 class CustomField < ActiveRecord::Base | |||||
130 | # Make the database cast values into numeric |
|
130 | # Make the database cast values into numeric | |
131 | # Postgresql will raise an error if a value can not be casted! |
|
131 | # Postgresql will raise an error if a value can not be casted! | |
132 | # CustomValue validations should ensure that it doesn't occur |
|
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 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + |
|
134 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + | |
135 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + |
|
135 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + | |
136 | " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)" |
|
136 | " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)" | |
@@ -142,17 +142,17 class CustomField < ActiveRecord::Base | |||||
142 | def <=>(field) |
|
142 | def <=>(field) | |
143 | position <=> field.position |
|
143 | position <=> field.position | |
144 | end |
|
144 | end | |
145 |
|
145 | |||
146 | def self.customized_class |
|
146 | def self.customized_class | |
147 | self.name =~ /^(.+)CustomField$/ |
|
147 | self.name =~ /^(.+)CustomField$/ | |
148 | begin; $1.constantize; rescue nil; end |
|
148 | begin; $1.constantize; rescue nil; end | |
149 | end |
|
149 | end | |
150 |
|
150 | |||
151 | # to move in project_custom_field |
|
151 | # to move in project_custom_field | |
152 | def self.for_all |
|
152 | def self.for_all | |
153 | find(:all, :conditions => ["is_for_all=?", true], :order => 'position') |
|
153 | find(:all, :conditions => ["is_for_all=?", true], :order => 'position') | |
154 | end |
|
154 | end | |
155 |
|
155 | |||
156 | def type_name |
|
156 | def type_name | |
157 | nil |
|
157 | nil | |
158 | end |
|
158 | end |
General Comments 0
You need to be logged in to leave comments.
Login now