@@ -0,0 +1,76 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
|
18 | require File.expand_path('../../test_helper', __FILE__) | |||
|
19 | ||||
|
20 | class CustomFieldVersionFormatTest < ActiveSupport::TestCase | |||
|
21 | fixtures :custom_fields, :projects, :members, :users, :member_roles, :trackers, :issues, :versions | |||
|
22 | ||||
|
23 | def setup | |||
|
24 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'version') | |||
|
25 | end | |||
|
26 | ||||
|
27 | def test_possible_values_with_no_arguments | |||
|
28 | assert_equal [], @field.possible_values | |||
|
29 | assert_equal [], @field.possible_values(nil) | |||
|
30 | end | |||
|
31 | ||||
|
32 | def test_possible_values_with_project_resource | |||
|
33 | project = Project.find(1) | |||
|
34 | possible_values = @field.possible_values(project.issues.first) | |||
|
35 | assert possible_values.any? | |||
|
36 | assert_equal project.shared_versions.sort.collect(&:id).map(&:to_s), possible_values | |||
|
37 | end | |||
|
38 | ||||
|
39 | def test_possible_values_with_nil_project_resource | |||
|
40 | assert_equal [], @field.possible_values(Issue.new) | |||
|
41 | end | |||
|
42 | ||||
|
43 | def test_possible_values_options_with_no_arguments | |||
|
44 | assert_equal [], @field.possible_values_options | |||
|
45 | assert_equal [], @field.possible_values_options(nil) | |||
|
46 | end | |||
|
47 | ||||
|
48 | def test_possible_values_options_with_project_resource | |||
|
49 | project = Project.find(1) | |||
|
50 | possible_values_options = @field.possible_values_options(project.issues.first) | |||
|
51 | assert possible_values_options.any? | |||
|
52 | assert_equal project.shared_versions.sort.map {|u| [u.name, u.id.to_s]}, possible_values_options | |||
|
53 | end | |||
|
54 | ||||
|
55 | def test_possible_values_options_with_array | |||
|
56 | projects = Project.find([1, 2]) | |||
|
57 | possible_values_options = @field.possible_values_options(projects) | |||
|
58 | assert possible_values_options.any? | |||
|
59 | assert_equal (projects.first.shared_versions & projects.last.shared_versions).sort.map {|u| [u.name, u.id.to_s]}, possible_values_options | |||
|
60 | end | |||
|
61 | ||||
|
62 | def test_cast_blank_value | |||
|
63 | assert_equal nil, @field.cast_value(nil) | |||
|
64 | assert_equal nil, @field.cast_value("") | |||
|
65 | end | |||
|
66 | ||||
|
67 | def test_cast_valid_value | |||
|
68 | version = @field.cast_value("2") | |||
|
69 | assert_kind_of Version, version | |||
|
70 | assert_equal Version.find(2), version | |||
|
71 | end | |||
|
72 | ||||
|
73 | def test_cast_invalid_value | |||
|
74 | assert_equal nil, @field.cast_value("187") | |||
|
75 | end | |||
|
76 | end |
@@ -1,161 +1,161 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
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. | |
17 |
|
17 | |||
18 | class CustomField < ActiveRecord::Base |
|
18 | 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 | |
26 | validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats |
|
26 | validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats | |
27 |
|
27 | |||
28 | validate :validate_values |
|
28 | validate :validate_values | |
29 |
|
29 | |||
30 | def initialize(attributes = nil) |
|
30 | def initialize(attributes = nil) | |
31 | super |
|
31 | super | |
32 | self.possible_values ||= [] |
|
32 | self.possible_values ||= [] | |
33 | end |
|
33 | end | |
34 |
|
34 | |||
35 | def before_validation |
|
35 | def before_validation | |
36 | # make sure these fields are not searchable |
|
36 | # make sure these fields are not searchable | |
37 | self.searchable = false if %w(int float date bool).include?(field_format) |
|
37 | self.searchable = false if %w(int float date bool).include?(field_format) | |
38 | true |
|
38 | true | |
39 | end |
|
39 | end | |
40 |
|
40 | |||
41 | def validate_values |
|
41 | def validate_values | |
42 | if self.field_format == "list" |
|
42 | if self.field_format == "list" | |
43 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? |
|
43 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? | |
44 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array |
|
44 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | if regexp.present? |
|
47 | if regexp.present? | |
48 | begin |
|
48 | begin | |
49 | Regexp.new(regexp) |
|
49 | Regexp.new(regexp) | |
50 | rescue |
|
50 | rescue | |
51 | errors.add(:regexp, :invalid) |
|
51 | errors.add(:regexp, :invalid) | |
52 | end |
|
52 | end | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | # validate default value |
|
55 | # validate default value | |
56 | v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil) |
|
56 | v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil) | |
57 | v.custom_field.is_required = false |
|
57 | v.custom_field.is_required = false | |
58 | errors.add(:default_value, :invalid) unless v.valid? |
|
58 | errors.add(:default_value, :invalid) unless v.valid? | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def possible_values_options(obj=nil) |
|
61 | def possible_values_options(obj=nil) | |
62 | case field_format |
|
62 | case field_format | |
63 | when 'user', 'version' |
|
63 | when 'user', 'version' | |
64 | if obj.respond_to?(:project) && obj.project |
|
64 | if obj.respond_to?(:project) && obj.project | |
65 | case field_format |
|
65 | case field_format | |
66 | when 'user' |
|
66 | when 'user' | |
67 | obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} |
|
67 | obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} | |
68 | when 'version' |
|
68 | when 'version' | |
69 | obj.project.versions.sort.collect {|u| [u.to_s, u.id.to_s]} |
|
69 | obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} | |
70 | end |
|
70 | end | |
71 | elsif obj.is_a?(Array) |
|
71 | elsif obj.is_a?(Array) | |
72 | obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v} |
|
72 | obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v} | |
73 | else |
|
73 | else | |
74 | [] |
|
74 | [] | |
75 | end |
|
75 | end | |
76 | else |
|
76 | else | |
77 | read_attribute :possible_values |
|
77 | read_attribute :possible_values | |
78 | end |
|
78 | end | |
79 | end |
|
79 | end | |
80 |
|
80 | |||
81 | def possible_values(obj=nil) |
|
81 | def possible_values(obj=nil) | |
82 | case field_format |
|
82 | case field_format | |
83 | when 'user', 'version' |
|
83 | when 'user', 'version' | |
84 | possible_values_options(obj).collect(&:last) |
|
84 | possible_values_options(obj).collect(&:last) | |
85 | else |
|
85 | else | |
86 | read_attribute :possible_values |
|
86 | read_attribute :possible_values | |
87 | end |
|
87 | end | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | # Makes possible_values accept a multiline string |
|
90 | # Makes possible_values accept a multiline string | |
91 | def possible_values=(arg) |
|
91 | def possible_values=(arg) | |
92 | if arg.is_a?(Array) |
|
92 | if arg.is_a?(Array) | |
93 | write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?}) |
|
93 | write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?}) | |
94 | else |
|
94 | else | |
95 | self.possible_values = arg.to_s.split(/[\n\r]+/) |
|
95 | self.possible_values = arg.to_s.split(/[\n\r]+/) | |
96 | end |
|
96 | end | |
97 | end |
|
97 | end | |
98 |
|
98 | |||
99 | def cast_value(value) |
|
99 | def cast_value(value) | |
100 | casted = nil |
|
100 | casted = nil | |
101 | unless value.blank? |
|
101 | unless value.blank? | |
102 | case field_format |
|
102 | case field_format | |
103 | when 'string', 'text', 'list' |
|
103 | when 'string', 'text', 'list' | |
104 | casted = value |
|
104 | casted = value | |
105 | when 'date' |
|
105 | when 'date' | |
106 | casted = begin; value.to_date; rescue; nil end |
|
106 | casted = begin; value.to_date; rescue; nil end | |
107 | when 'bool' |
|
107 | when 'bool' | |
108 | casted = (value == '1' ? true : false) |
|
108 | casted = (value == '1' ? true : false) | |
109 | when 'int' |
|
109 | when 'int' | |
110 | casted = value.to_i |
|
110 | casted = value.to_i | |
111 | when 'float' |
|
111 | when 'float' | |
112 | casted = value.to_f |
|
112 | casted = value.to_f | |
113 | when 'user', 'version' |
|
113 | when 'user', 'version' | |
114 | casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) |
|
114 | casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) | |
115 | end |
|
115 | end | |
116 | end |
|
116 | end | |
117 | casted |
|
117 | casted | |
118 | end |
|
118 | end | |
119 |
|
119 | |||
120 | # Returns a ORDER BY clause that can used to sort customized |
|
120 | # Returns a ORDER BY clause that can used to sort customized | |
121 | # objects by their value of the custom field. |
|
121 | # objects by their value of the custom field. | |
122 | # Returns false, if the custom field can not be used for sorting. |
|
122 | # Returns false, if the custom field can not be used for sorting. | |
123 | def order_statement |
|
123 | def order_statement | |
124 | case field_format |
|
124 | case field_format | |
125 | when 'string', 'text', 'list', 'date', 'bool' |
|
125 | when 'string', 'text', 'list', 'date', 'bool' | |
126 | # COALESCE is here to make sure that blank and NULL values are sorted equally |
|
126 | # COALESCE is here to make sure that blank and NULL values are sorted equally | |
127 | "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" + |
|
127 | "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" + | |
128 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + |
|
128 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + | |
129 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + |
|
129 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + | |
130 | " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" |
|
130 | " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" | |
131 | when 'int', 'float' |
|
131 | when 'int', 'float' | |
132 | # Make the database cast values into numeric |
|
132 | # Make the database cast values into numeric | |
133 | # Postgresql will raise an error if a value can not be casted! |
|
133 | # Postgresql will raise an error if a value can not be casted! | |
134 | # CustomValue validations should ensure that it doesn't occur |
|
134 | # CustomValue validations should ensure that it doesn't occur | |
135 | "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" + |
|
135 | "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" + | |
136 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + |
|
136 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + | |
137 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + |
|
137 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + | |
138 | " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)" |
|
138 | " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)" | |
139 | else |
|
139 | else | |
140 | nil |
|
140 | nil | |
141 | end |
|
141 | end | |
142 | end |
|
142 | end | |
143 |
|
143 | |||
144 | def <=>(field) |
|
144 | def <=>(field) | |
145 | position <=> field.position |
|
145 | position <=> field.position | |
146 | end |
|
146 | end | |
147 |
|
147 | |||
148 | def self.customized_class |
|
148 | def self.customized_class | |
149 | self.name =~ /^(.+)CustomField$/ |
|
149 | self.name =~ /^(.+)CustomField$/ | |
150 | begin; $1.constantize; rescue nil; end |
|
150 | begin; $1.constantize; rescue nil; end | |
151 | end |
|
151 | end | |
152 |
|
152 | |||
153 | # to move in project_custom_field |
|
153 | # to move in project_custom_field | |
154 | def self.for_all |
|
154 | def self.for_all | |
155 | find(:all, :conditions => ["is_for_all=?", true], :order => 'position') |
|
155 | find(:all, :conditions => ["is_for_all=?", true], :order => 'position') | |
156 | end |
|
156 | end | |
157 |
|
157 | |||
158 | def type_name |
|
158 | def type_name | |
159 | nil |
|
159 | nil | |
160 | end |
|
160 | end | |
161 | end |
|
161 | end |
General Comments 0
You need to be logged in to leave comments.
Login now