From 7fa18cad5771ea63cb1c55a24ade75a573af5dae Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 12 Feb 2012 19:41:42 +0000 Subject: [PATCH] Prevent ActiveRecord::StaleObjectError in Issue#reschedule_after (#7920). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8864 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/issue.rb | 8 +++++++- test/unit/issue_test.rb | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/issue.rb b/app/models/issue.rb index 64a99e6be..2750d7f63 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -638,7 +638,13 @@ class Issue < ActiveRecord::Base if leaf? if start_date.nil? || start_date < date self.start_date, self.due_date = date, date + duration - save + begin + save + rescue ActiveRecord::StaleObjectError + reload + self.start_date, self.due_date = date, date + duration + save + end end else leaves.each do |leaf| diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index 14abd413b..3ef7c7b59 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -768,6 +768,19 @@ class IssueTest < ActiveSupport::TestCase assert_equal issue1.due_date + 1, issue2.reload.start_date end + def test_rescheduling_a_stale_issue_should_not_raise_an_error + stale = Issue.find(1) + issue = Issue.find(1) + issue.subject = "Updated" + issue.save! + + date = 10.days.from_now.to_date + assert_nothing_raised do + stale.reschedule_after(date) + end + assert_equal date, stale.reload.start_date + end + def test_overdue assert Issue.new(:due_date => 1.day.ago.to_date).overdue? assert !Issue.new(:due_date => Date.today).overdue?