Commit 6617eaaf9b9ff5a76cb2c4150623a685357966d4

Authored by Robb Kidd
1 parent 356430c3

Make IssueObserver handle issus, not MailerObserver

app/models/issue.rb
... ... @@ -68,6 +68,10 @@ class Issue < ActiveRecord::Base
68 68 def is_being_closed?
69 69 closed_changed? && closed
70 70 end
  71 +
  72 + def is_being_reopened?
  73 + closed_changed? && !closed
  74 + end
71 75 end
72 76 # == Schema Information
73 77 #
... ...
app/models/issue_observer.rb
... ... @@ -5,9 +5,10 @@ class IssueObserver < ActiveRecord::Observer
5 5 Notify.new_issue_email(issue.id) if issue.assignee != current_user
6 6 end
7 7  
8   - def after_change(issue)
  8 + def after_update(issue)
9 9 send_reassigned_email(issue) if issue.is_being_reassigned?
10 10 Note.create_status_change_note(issue, current_user, 'closed') if issue.is_being_closed?
  11 + Note.create_status_change_note(issue, current_user, 'reopened') if issue.is_being_reopened?
11 12 end
12 13  
13 14 def send_reassigned_email(issue)
... ...
app/models/mailer_observer.rb
... ... @@ -3,7 +3,6 @@ class MailerObserver < ActiveRecord::Observer
3 3 cattr_accessor :current_user
4 4  
5 5 def after_create(model)
6   - new_issue(model) if model.kind_of?(Issue)
7 6 new_user(model) if model.kind_of?(User)
8 7 new_note(model) if model.kind_of?(Note)
9 8 new_merge_request(model) if model.kind_of?(MergeRequest)
... ... @@ -11,17 +10,10 @@ class MailerObserver < ActiveRecord::Observer
11 10  
12 11 def after_update(model)
13 12 changed_merge_request(model) if model.kind_of?(MergeRequest)
14   - changed_issue(model) if model.kind_of?(Issue)
15 13 end
16 14  
17 15 protected
18 16  
19   - def new_issue(issue)
20   - if issue.assignee != current_user
21   - Notify.new_issue_email(issue.id).deliver
22   - end
23   - end
24   -
25 17 def new_user(user)
26 18 Notify.new_user_email(user.id, user.password).deliver
27 19 end
... ... @@ -65,12 +57,8 @@ class MailerObserver < ActiveRecord::Observer
65 57 status_notify_and_comment merge_request, :reassigned_merge_request_email
66 58 end
67 59  
68   - def changed_issue(issue)
69   - status_notify_and_comment issue, :reassigned_issue_email
70   - end
71   -
72 60 # This method used for Issues & Merge Requests
73   - #
  61 + #
74 62 # It create a comment for Issue or MR if someone close/reopen.
75 63 # It also notify via email if assignee was changed
76 64 #
... ...
config/application.rb
... ... @@ -23,7 +23,7 @@ module Gitlab
23 23 # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
24 24  
25 25 # Activate observers that should always be running.
26   - config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer
  26 + config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer
27 27  
28 28 # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
29 29 # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
... ...
spec/models/issue_observer_spec.rb
... ... @@ -9,7 +9,12 @@ describe IssueObserver do
9 9  
10 10 subject { IssueObserver.instance }
11 11  
12   - context 'when an issue is created' do
  12 + describe '#after_create' do
  13 +
  14 + it 'is called when an issue is created' do
  15 + subject.should_receive(:after_create)
  16 + Factory.create(:issue, :project => Factory.create(:project))
  17 + end
13 18  
14 19 it 'sends an email to the assignee' do
15 20 Notify.should_receive(:new_issue_email).with(issue.id)
... ... @@ -25,10 +30,18 @@ describe IssueObserver do
25 30 end
26 31 end
27 32  
28   - context 'when an issue is changed' do
  33 + context '#after_update' do
29 34 before(:each) do
30 35 issue.stub(:is_being_reassigned?).and_return(false)
31 36 issue.stub(:is_being_closed?).and_return(false)
  37 + issue.stub(:is_being_reopened?).and_return(false)
  38 + end
  39 +
  40 + it 'is called when an issue is changed' do
  41 + changed = Factory.create(:issue, :project => Factory.create(:project))
  42 + subject.should_receive(:after_update)
  43 + changed.description = 'I changed'
  44 + changed.save
32 45 end
33 46  
34 47 context 'a reassigned email' do
... ... @@ -36,14 +49,14 @@ describe IssueObserver do
36 49 issue.should_receive(:is_being_reassigned?).and_return(true)
37 50 subject.should_receive(:send_reassigned_email).with(issue)
38 51  
39   - subject.after_change(issue)
  52 + subject.after_update(issue)
40 53 end
41 54  
42 55 it 'is not sent if the issue is not being reassigned' do
43 56 issue.should_receive(:is_being_reassigned?).and_return(false)
44 57 subject.should_not_receive(:send_reassigned_email)
45 58  
46   - subject.after_change(issue)
  59 + subject.after_update(issue)
47 60 end
48 61 end
49 62  
... ... @@ -52,14 +65,30 @@ describe IssueObserver do
52 65 issue.should_receive(:is_being_closed?).and_return(true)
53 66 Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
54 67  
55   - subject.after_change(issue)
  68 + subject.after_update(issue)
56 69 end
57 70  
58 71 it 'is not created if the issue is not being closed' do
59 72 issue.should_receive(:is_being_closed?).and_return(false)
60 73 Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
61 74  
62   - subject.after_change(issue)
  75 + subject.after_update(issue)
  76 + end
  77 + end
  78 +
  79 + context 'a status "reopened" note' do
  80 + it 'is created if the issue is being reopened' do
  81 + issue.should_receive(:is_being_reopened?).and_return(true)
  82 + Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
  83 +
  84 + subject.after_update(issue)
  85 + end
  86 +
  87 + it 'is not created if the issue is not being reopened' do
  88 + issue.should_receive(:is_being_reopened?).and_return(false)
  89 + Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
  90 +
  91 + subject.after_update(issue)
63 92 end
64 93 end
65 94 end
... ...
spec/models/issue_spec.rb
... ... @@ -55,6 +55,26 @@ describe Issue do
55 55 end
56 56 end
57 57  
  58 +
  59 + describe '#is_being_reopened?' do
  60 + it 'returns true if the closed attribute has changed and is now false' do
  61 + issue = Factory.create(:issue,
  62 + :closed => true,
  63 + :author => Factory(:user),
  64 + :assignee => Factory(:user),
  65 + :project => Factory.create(:project))
  66 + issue.closed = false
  67 + issue.is_being_reopened?.should be_true
  68 + end
  69 + it 'returns false if the closed attribute has changed and is now true' do
  70 + subject.closed = true
  71 + subject.is_being_reopened?.should be_false
  72 + end
  73 + it 'returns false if the closed attribute has not changed' do
  74 + subject.is_being_reopened?.should be_false
  75 + end
  76 + end
  77 +
58 78 describe "plus 1" do
59 79 let(:project) { Factory(:project) }
60 80 subject {
... ... @@ -86,6 +106,7 @@ describe Issue do
86 106 subject.upvotes.should == 2
87 107 end
88 108 end
  109 +
89 110 end
90 111 # == Schema Information
91 112 #
... ...