##// END OF EJS Templates
When moving an issue to another project, reassign it to the category with same name if any (#1653)....
Jean-Philippe Lang -
r1688:b68fd4c04bed
parent child
Show More
@@ -1,257 +1,259
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 Issue < ActiveRecord::Base
19 19 belongs_to :project
20 20 belongs_to :tracker
21 21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 25 belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id'
26 26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27 27
28 28 has_many :journals, :as => :journalized, :dependent => :destroy
29 29 has_many :attachments, :as => :container, :dependent => :destroy
30 30 has_many :time_entries, :dependent => :delete_all
31 31 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
32 32
33 33 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
34 34 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
35 35
36 36 acts_as_customizable
37 37 acts_as_watchable
38 38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
39 39 :include => [:project, :journals],
40 40 # sort by id so that limited eager loading doesn't break with postgresql
41 41 :order_column => "#{table_name}.id"
42 42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
43 43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}
44 44
45 45 validates_presence_of :subject, :description, :priority, :project, :tracker, :author, :status
46 46 validates_length_of :subject, :maximum => 255
47 47 validates_inclusion_of :done_ratio, :in => 0..100
48 48 validates_numericality_of :estimated_hours, :allow_nil => true
49 49
50 50 def after_initialize
51 51 if new_record?
52 52 # set default values for new records only
53 53 self.status ||= IssueStatus.default
54 54 self.priority ||= Enumeration.default('IPRI')
55 55 end
56 56 end
57 57
58 58 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
59 59 def available_custom_fields
60 60 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
61 61 end
62 62
63 63 def copy_from(arg)
64 64 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
65 65 self.attributes = issue.attributes.dup
66 66 self.custom_values = issue.custom_values.collect {|v| v.clone}
67 67 self
68 68 end
69 69
70 70 # Move an issue to a new project and tracker
71 71 def move_to(new_project, new_tracker = nil)
72 72 transaction do
73 73 if new_project && project_id != new_project.id
74 74 # delete issue relations
75 75 unless Setting.cross_project_issue_relations?
76 76 self.relations_from.clear
77 77 self.relations_to.clear
78 78 end
79 79 # issue is moved to another project
80 self.category = nil
80 # reassign to the category with same name if any
81 new_category = category.nil? ? nil : new_project.issue_categories.find_by_name(category.name)
82 self.category = new_category
81 83 self.fixed_version = nil
82 84 self.project = new_project
83 85 end
84 86 if new_tracker
85 87 self.tracker = new_tracker
86 88 end
87 89 if save
88 90 # Manually update project_id on related time entries
89 91 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
90 92 else
91 93 rollback_db_transaction
92 94 return false
93 95 end
94 96 end
95 97 return true
96 98 end
97 99
98 100 def priority_id=(pid)
99 101 self.priority = nil
100 102 write_attribute(:priority_id, pid)
101 103 end
102 104
103 105 def estimated_hours=(h)
104 106 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
105 107 end
106 108
107 109 def validate
108 110 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
109 111 errors.add :due_date, :activerecord_error_not_a_date
110 112 end
111 113
112 114 if self.due_date and self.start_date and self.due_date < self.start_date
113 115 errors.add :due_date, :activerecord_error_greater_than_start_date
114 116 end
115 117
116 118 if start_date && soonest_start && start_date < soonest_start
117 119 errors.add :start_date, :activerecord_error_invalid
118 120 end
119 121 end
120 122
121 123 def validate_on_create
122 124 errors.add :tracker_id, :activerecord_error_invalid unless project.trackers.include?(tracker)
123 125 end
124 126
125 127 def before_create
126 128 # default assignment based on category
127 129 if assigned_to.nil? && category && category.assigned_to
128 130 self.assigned_to = category.assigned_to
129 131 end
130 132 end
131 133
132 134 def before_save
133 135 if @current_journal
134 136 # attributes changes
135 137 (Issue.column_names - %w(id description)).each {|c|
136 138 @current_journal.details << JournalDetail.new(:property => 'attr',
137 139 :prop_key => c,
138 140 :old_value => @issue_before_change.send(c),
139 141 :value => send(c)) unless send(c)==@issue_before_change.send(c)
140 142 }
141 143 # custom fields changes
142 144 custom_values.each {|c|
143 145 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
144 146 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
145 147 @current_journal.details << JournalDetail.new(:property => 'cf',
146 148 :prop_key => c.custom_field_id,
147 149 :old_value => @custom_values_before_change[c.custom_field_id],
148 150 :value => c.value)
149 151 }
150 152 @current_journal.save
151 153 end
152 154 # Save the issue even if the journal is not saved (because empty)
153 155 true
154 156 end
155 157
156 158 def after_save
157 159 # Reload is needed in order to get the right status
158 160 reload
159 161
160 162 # Update start/due dates of following issues
161 163 relations_from.each(&:set_issue_to_dates)
162 164
163 165 # Close duplicates if the issue was closed
164 166 if @issue_before_change && !@issue_before_change.closed? && self.closed?
165 167 duplicates.each do |duplicate|
166 168 # Reload is need in case the duplicate was updated by a previous duplicate
167 169 duplicate.reload
168 170 # Don't re-close it if it's already closed
169 171 next if duplicate.closed?
170 172 # Same user and notes
171 173 duplicate.init_journal(@current_journal.user, @current_journal.notes)
172 174 duplicate.update_attribute :status, self.status
173 175 end
174 176 end
175 177 end
176 178
177 179 def init_journal(user, notes = "")
178 180 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
179 181 @issue_before_change = self.clone
180 182 @issue_before_change.status = self.status
181 183 @custom_values_before_change = {}
182 184 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
183 185 @current_journal
184 186 end
185 187
186 188 # Return true if the issue is closed, otherwise false
187 189 def closed?
188 190 self.status.is_closed?
189 191 end
190 192
191 193 # Users the issue can be assigned to
192 194 def assignable_users
193 195 project.assignable_users
194 196 end
195 197
196 198 # Returns an array of status that user is able to apply
197 199 def new_statuses_allowed_to(user)
198 200 statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker)
199 201 statuses << status unless statuses.empty?
200 202 statuses.uniq.sort
201 203 end
202 204
203 205 # Returns the mail adresses of users that should be notified for the issue
204 206 def recipients
205 207 recipients = project.recipients
206 208 # Author and assignee are always notified unless they have been locked
207 209 recipients << author.mail if author && author.active?
208 210 recipients << assigned_to.mail if assigned_to && assigned_to.active?
209 211 recipients.compact.uniq
210 212 end
211 213
212 214 def spent_hours
213 215 @spent_hours ||= time_entries.sum(:hours) || 0
214 216 end
215 217
216 218 def relations
217 219 (relations_from + relations_to).sort
218 220 end
219 221
220 222 def all_dependent_issues
221 223 dependencies = []
222 224 relations_from.each do |relation|
223 225 dependencies << relation.issue_to
224 226 dependencies += relation.issue_to.all_dependent_issues
225 227 end
226 228 dependencies
227 229 end
228 230
229 231 # Returns an array of issues that duplicate this one
230 232 def duplicates
231 233 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
232 234 end
233 235
234 236 # Returns the due date or the target due date if any
235 237 # Used on gantt chart
236 238 def due_before
237 239 due_date || (fixed_version ? fixed_version.effective_date : nil)
238 240 end
239 241
240 242 def duration
241 243 (start_date && due_date) ? due_date - start_date : 0
242 244 end
243 245
244 246 def soonest_start
245 247 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
246 248 end
247 249
248 250 def self.visible_by(usr)
249 251 with_scope(:find => { :conditions => Project.visible_by(usr) }) do
250 252 yield
251 253 end
252 254 end
253 255
254 256 def to_s
255 257 "#{tracker} ##{id}: #{subject}"
256 258 end
257 259 end
@@ -1,17 +1,22
1 1 ---
2 2 issue_categories_001:
3 3 name: Printing
4 4 project_id: 1
5 5 assigned_to_id: 2
6 6 id: 1
7 7 issue_categories_002:
8 8 name: Recipes
9 9 project_id: 1
10 10 assigned_to_id:
11 11 id: 2
12 12 issue_categories_003:
13 13 name: Stock management
14 14 project_id: 2
15 15 assigned_to_id:
16 16 id: 3
17 issue_categories_004:
18 name: Printing
19 project_id: 2
20 assigned_to_id:
21 id: 4
17 22 No newline at end of file
@@ -1,184 +1,193
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class IssueTest < Test::Unit::TestCase
21 21 fixtures :projects, :users, :members,
22 22 :trackers, :projects_trackers,
23 23 :issue_statuses, :issue_categories,
24 24 :enumerations,
25 25 :issues,
26 26 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
27 27 :time_entries
28 28
29 29 def test_create
30 30 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30')
31 31 assert issue.save
32 32 issue.reload
33 33 assert_equal 1.5, issue.estimated_hours
34 34 end
35 35
36 36 def test_create_with_required_custom_field
37 37 field = IssueCustomField.find_by_name('Database')
38 38 field.update_attribute(:is_required, true)
39 39
40 40 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'test_create', :description => 'IssueTest#test_create_with_required_custom_field')
41 41 assert issue.available_custom_fields.include?(field)
42 42 # No value for the custom field
43 43 assert !issue.save
44 44 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
45 45 # Blank value
46 46 issue.custom_field_values = { field.id => '' }
47 47 assert !issue.save
48 48 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
49 49 # Invalid value
50 50 issue.custom_field_values = { field.id => 'SQLServer' }
51 51 assert !issue.save
52 52 assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values)
53 53 # Valid value
54 54 issue.custom_field_values = { field.id => 'PostgreSQL' }
55 55 assert issue.save
56 56 issue.reload
57 57 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
58 58 end
59 59
60 60 def test_update_issue_with_required_custom_field
61 61 field = IssueCustomField.find_by_name('Database')
62 62 field.update_attribute(:is_required, true)
63 63
64 64 issue = Issue.find(1)
65 65 assert_nil issue.custom_value_for(field)
66 66 assert issue.available_custom_fields.include?(field)
67 67 # No change to custom values, issue can be saved
68 68 assert issue.save
69 69 # Blank value
70 70 issue.custom_field_values = { field.id => '' }
71 71 assert !issue.save
72 72 # Valid value
73 73 issue.custom_field_values = { field.id => 'PostgreSQL' }
74 74 assert issue.save
75 75 issue.reload
76 76 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
77 77 end
78 78
79 79 def test_should_not_update_attributes_if_custom_fields_validation_fails
80 80 issue = Issue.find(1)
81 81 field = IssueCustomField.find_by_name('Database')
82 82 assert issue.available_custom_fields.include?(field)
83 83
84 84 issue.custom_field_values = { field.id => 'Invalid' }
85 85 issue.subject = 'Should be not be saved'
86 86 assert !issue.save
87 87
88 88 issue.reload
89 89 assert_equal "Can't print recipes", issue.subject
90 90 end
91 91
92 92 def test_should_not_recreate_custom_values_objects_on_update
93 93 field = IssueCustomField.find_by_name('Database')
94 94
95 95 issue = Issue.find(1)
96 96 issue.custom_field_values = { field.id => 'PostgreSQL' }
97 97 assert issue.save
98 98 custom_value = issue.custom_value_for(field)
99 99 issue.reload
100 100 issue.custom_field_values = { field.id => 'MySQL' }
101 101 assert issue.save
102 102 issue.reload
103 103 assert_equal custom_value.id, issue.custom_value_for(field).id
104 104 end
105 105
106 106 def test_category_based_assignment
107 107 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
108 108 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
109 109 end
110 110
111 111 def test_copy
112 112 issue = Issue.new.copy_from(1)
113 113 assert issue.save
114 114 issue.reload
115 115 orig = Issue.find(1)
116 116 assert_equal orig.subject, issue.subject
117 117 assert_equal orig.tracker, issue.tracker
118 118 assert_equal orig.custom_values.first.value, issue.custom_values.first.value
119 119 end
120 120
121 121 def test_should_close_duplicates
122 122 # Create 3 issues
123 123 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test')
124 124 assert issue1.save
125 125 issue2 = issue1.clone
126 126 assert issue2.save
127 127 issue3 = issue1.clone
128 128 assert issue3.save
129 129
130 130 # 2 is a dupe of 1
131 131 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
132 132 # And 3 is a dupe of 2
133 133 IssueRelation.create(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
134 134 # And 3 is a dupe of 1 (circular duplicates)
135 135 IssueRelation.create(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
136 136
137 137 assert issue1.reload.duplicates.include?(issue2)
138 138
139 139 # Closing issue 1
140 140 issue1.init_journal(User.find(:first), "Closing issue1")
141 141 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
142 142 assert issue1.save
143 143 # 2 and 3 should be also closed
144 144 assert issue2.reload.closed?
145 145 assert issue3.reload.closed?
146 146 end
147 147
148 148 def test_should_not_close_duplicated_issue
149 149 # Create 3 issues
150 150 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test')
151 151 assert issue1.save
152 152 issue2 = issue1.clone
153 153 assert issue2.save
154 154
155 155 # 2 is a dupe of 1
156 156 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
157 157 # 2 is a dup of 1 but 1 is not a duplicate of 2
158 158 assert !issue2.reload.duplicates.include?(issue1)
159 159
160 160 # Closing issue 2
161 161 issue2.init_journal(User.find(:first), "Closing issue2")
162 162 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
163 163 assert issue2.save
164 164 # 1 should not be also closed
165 165 assert !issue1.reload.closed?
166 166 end
167 167
168 def test_move_to_another_project
168 def test_move_to_another_project_with_same_category
169 169 issue = Issue.find(1)
170 170 assert issue.move_to(Project.find(2))
171 171 issue.reload
172 172 assert_equal 2, issue.project_id
173 # Category removed
174 assert_nil issue.category
173 # Category changes
174 assert_equal 4, issue.category_id
175 175 # Make sure time entries were move to the target project
176 176 assert_equal 2, issue.time_entries.first.project_id
177 177 end
178 178
179 def test_move_to_another_project_without_same_category
180 issue = Issue.find(2)
181 assert issue.move_to(Project.find(2))
182 issue.reload
183 assert_equal 2, issue.project_id
184 # Category cleared
185 assert_nil issue.category_id
186 end
187
179 188 def test_issue_destroy
180 189 Issue.find(1).destroy
181 190 assert_nil Issue.find_by_id(1)
182 191 assert_nil TimeEntry.find_by_issue_id(1)
183 192 end
184 193 end
General Comments 0
You need to be logged in to leave comments. Login now