On Thu, Dec 18, 2008 at 11:36 AM, LAN-SUN-LUK Benjamin
<Benjamin.LAN-SUN-LUK(a)supinfo.com> wrote:
Signed-off-by: Benjamin LAN-SUN-LUK <benjamin.lan-sun-luk(a)supinfo.com>
---
app/models/sprint.rb | 18 ++++++++++++++++++
1 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/app/models/sprint.rb b/app/models/sprint.rb
index 1fffb5b..bd7c788 100644
--- a/app/models/sprint.rb
+++ b/app/models/sprint.rb
@@ -56,6 +56,8 @@ class Sprint < ActiveRecord::Base
validates_presence_of :goals,
:message => 'Please list the goals for this sprint.'
+ after_update :close_user_stories
+
belongs_to :product
has_many :backlog_items, :dependent => :destroy
@@ -188,4 +190,20 @@ class Sprint < ActiveRecord::Base
return false
end
+
+ private
+
+ # When the user close a sprint, every user stories are close if the
related
+ # backlog item are completed.
+ def close_user_stories
+ return unless status == STATUS_CLOSED
+
+ self.backlog_items.each do |backlog_item|
+ if backlog_item.state == BacklogItem::STATE_COMPLETED
+ user_story = backlog_item.user_story
+ user_story.closed = true
+ user_story.save!
+ end
+ end
+ end
end
--
1.6.0.2
This works, but there's a bug in it. If, while saving the sprint, an
exception is thrown and the sprint update fails, the user story is
still closed. The whole transaction, from changing the sprint state to
updating the user stories should be atomic: if the sprint save fails
then the user stories shouldn't be closed. Is there a way to nest
transactions?
However, the above code meets requirements, so I'm going to ACK it
since it does work.
--
Darryl L. Pierce <mcpierce(a)gmail.com>
Visit the Infobahn Offramp: <
http://mcpierce.multiply.com>
"Bury me next to my wife. Nothing too fancy..." - Ulysses S. Grant