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