From 356430c3c0e8aed3f8c9f2e181aaeaeaa4f1d693 Mon Sep 17 00:00:00 2001
From: Robb Kidd <robb@thekidds.org>
Date: Sun, 20 May 2012 15:06:13 -0400
Subject: [PATCH] Add method for an issue to know whether it is being closed

Update IssueObserver to create a Note on the issue its being closed.
---
 app/models/issue.rb                |  4 +++
 app/models/issue_observer.rb       |  2 +-
 spec/models/issue_observer_spec.rb | 39 ++++++++++++++++++++++++------
 spec/models/issue_spec.rb          | 19 +++++++++++++++
 4 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/app/models/issue.rb b/app/models/issue.rb
index f1cb2e220b..fd9db26432 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -64,6 +64,10 @@ class Issue < ActiveRecord::Base
   def is_being_reassigned?
     assignee_id_changed?
   end
+
+  def is_being_closed?
+    closed_changed? && closed
+  end
 end
 # == Schema Information
 #
diff --git a/app/models/issue_observer.rb b/app/models/issue_observer.rb
index 38c4fde91a..461b3eb0df 100644
--- a/app/models/issue_observer.rb
+++ b/app/models/issue_observer.rb
@@ -7,6 +7,7 @@ class IssueObserver < ActiveRecord::Observer
 
   def after_change(issue)
     send_reassigned_email(issue) if issue.is_being_reassigned?
+    Note.create_status_change_note(issue, current_user, 'closed') if issue.is_being_closed?
   end
 
   def send_reassigned_email(issue)
@@ -16,5 +17,4 @@ class IssueObserver < ActiveRecord::Observer
       Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was)
     end
   end
-
 end
diff --git a/spec/models/issue_observer_spec.rb b/spec/models/issue_observer_spec.rb
index 7de9a0ae62..fec09488d8 100644
--- a/spec/models/issue_observer_spec.rb
+++ b/spec/models/issue_observer_spec.rb
@@ -26,18 +26,41 @@ describe IssueObserver do
   end
 
   context 'when an issue is changed' do
-    it 'sends a reassigned email, if the issue is being reassigned' do
-      issue.should_receive(:is_being_reassigned?).and_return(true)
-      subject.should_receive(:send_reassigned_email).with(issue)
+    before(:each) do
+      issue.stub(:is_being_reassigned?).and_return(false)
+      issue.stub(:is_being_closed?).and_return(false)
+    end
+
+    context 'a reassigned email' do
+      it 'is sent if the issue is being reassigned' do
+        issue.should_receive(:is_being_reassigned?).and_return(true)
+        subject.should_receive(:send_reassigned_email).with(issue)
+
+        subject.after_change(issue)
+      end
+
+      it 'is not sent if the issue is not being reassigned' do
+        issue.should_receive(:is_being_reassigned?).and_return(false)
+        subject.should_not_receive(:send_reassigned_email)
 
-      subject.after_change(issue)
+        subject.after_change(issue)
+      end
     end
 
-    it 'does not send a reassigned email, if the issue was not reassigned' do
-      issue.should_receive(:is_being_reassigned?).and_return(false)
-      subject.should_not_receive(:send_reassigned_email)
+    context 'a status "closed" note' do
+      it 'is created if the issue is being closed' do
+        issue.should_receive(:is_being_closed?).and_return(true)
+        Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
+
+        subject.after_change(issue)
+      end
 
-      subject.after_change(issue)
+      it 'is not created if the issue is not being closed' do
+        issue.should_receive(:is_being_closed?).and_return(false)
+        Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
+
+        subject.after_change(issue)
+      end
     end
   end
 
diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb
index 68c05f2edf..9668f0b23f 100644
--- a/spec/models/issue_spec.rb
+++ b/spec/models/issue_spec.rb
@@ -36,6 +36,25 @@ describe Issue do
     end
   end
 
+  describe '#is_being_closed?' do
+    it 'returns true if the closed attribute has changed and is now true' do
+      subject.closed = true
+      subject.is_being_closed?.should be_true
+    end
+    it 'returns false if the closed attribute has changed and is now false' do
+      issue = Factory.create(:issue,
+                             :closed => true,
+                             :author => Factory(:user),
+                             :assignee => Factory(:user),
+                             :project => Factory.create(:project))
+      issue.closed = false
+      issue.is_being_closed?.should be_false
+    end
+    it 'returns false if the closed attribute has not changed' do
+      subject.is_being_closed?.should be_false
+    end
+  end
+
   describe "plus 1" do
     let(:project) { Factory(:project) }
     subject {
-- 
2.30.9