Commit 356430c3c0e8aed3f8c9f2e181aaeaeaa4f1d693

Authored by Robb Kidd
1 parent 02924de3

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
@@ -64,6 +64,10 @@ class Issue < ActiveRecord::Base @@ -64,6 +64,10 @@ class Issue < ActiveRecord::Base
64 def is_being_reassigned? 64 def is_being_reassigned?
65 assignee_id_changed? 65 assignee_id_changed?
66 end 66 end
  67 +
  68 + def is_being_closed?
  69 + closed_changed? && closed
  70 + end
67 end 71 end
68 # == Schema Information 72 # == Schema Information
69 # 73 #
app/models/issue_observer.rb
@@ -7,6 +7,7 @@ class IssueObserver < ActiveRecord::Observer @@ -7,6 +7,7 @@ class IssueObserver < ActiveRecord::Observer
7 7
8 def after_change(issue) 8 def after_change(issue)
9 send_reassigned_email(issue) if issue.is_being_reassigned? 9 send_reassigned_email(issue) if issue.is_being_reassigned?
  10 + Note.create_status_change_note(issue, current_user, 'closed') if issue.is_being_closed?
10 end 11 end
11 12
12 def send_reassigned_email(issue) 13 def send_reassigned_email(issue)
@@ -16,5 +17,4 @@ class IssueObserver < ActiveRecord::Observer @@ -16,5 +17,4 @@ class IssueObserver < ActiveRecord::Observer
16 Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was) 17 Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was)
17 end 18 end
18 end 19 end
19 -  
20 end 20 end
spec/models/issue_observer_spec.rb
@@ -26,18 +26,41 @@ describe IssueObserver do @@ -26,18 +26,41 @@ describe IssueObserver do
26 end 26 end
27 27
28 context 'when an issue is changed' do 28 context 'when an issue is changed' do
29 - it 'sends a reassigned email, if the issue is being reassigned' do  
30 - issue.should_receive(:is_being_reassigned?).and_return(true)  
31 - subject.should_receive(:send_reassigned_email).with(issue) 29 + before(:each) do
  30 + issue.stub(:is_being_reassigned?).and_return(false)
  31 + issue.stub(:is_being_closed?).and_return(false)
  32 + end
  33 +
  34 + context 'a reassigned email' do
  35 + it 'is sent if the issue is being reassigned' do
  36 + issue.should_receive(:is_being_reassigned?).and_return(true)
  37 + subject.should_receive(:send_reassigned_email).with(issue)
  38 +
  39 + subject.after_change(issue)
  40 + end
  41 +
  42 + it 'is not sent if the issue is not being reassigned' do
  43 + issue.should_receive(:is_being_reassigned?).and_return(false)
  44 + subject.should_not_receive(:send_reassigned_email)
32 45
33 - subject.after_change(issue) 46 + subject.after_change(issue)
  47 + end
34 end 48 end
35 49
36 - it 'does not send a reassigned email, if the issue was not reassigned' do  
37 - issue.should_receive(:is_being_reassigned?).and_return(false)  
38 - subject.should_not_receive(:send_reassigned_email) 50 + context 'a status "closed" note' do
  51 + it 'is created if the issue is being closed' do
  52 + issue.should_receive(:is_being_closed?).and_return(true)
  53 + Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
  54 +
  55 + subject.after_change(issue)
  56 + end
39 57
40 - subject.after_change(issue) 58 + it 'is not created if the issue is not being closed' do
  59 + issue.should_receive(:is_being_closed?).and_return(false)
  60 + Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
  61 +
  62 + subject.after_change(issue)
  63 + end
41 end 64 end
42 end 65 end
43 66
spec/models/issue_spec.rb
@@ -36,6 +36,25 @@ describe Issue do @@ -36,6 +36,25 @@ describe Issue do
36 end 36 end
37 end 37 end
38 38
  39 + describe '#is_being_closed?' do
  40 + it 'returns true if the closed attribute has changed and is now true' do
  41 + subject.closed = true
  42 + subject.is_being_closed?.should be_true
  43 + end
  44 + it 'returns false if the closed attribute has changed and is now false' do
  45 + issue = Factory.create(:issue,
  46 + :closed => true,
  47 + :author => Factory(:user),
  48 + :assignee => Factory(:user),
  49 + :project => Factory.create(:project))
  50 + issue.closed = false
  51 + issue.is_being_closed?.should be_false
  52 + end
  53 + it 'returns false if the closed attribute has not changed' do
  54 + subject.is_being_closed?.should be_false
  55 + end
  56 + end
  57 +
39 describe "plus 1" do 58 describe "plus 1" do
40 let(:project) { Factory(:project) } 59 let(:project) { Factory(:project) }
41 subject { 60 subject {