Commit f6035552e5d83b36f69e1ac7fa4b40ee5f7ab4fb

Authored by Robb Kidd
1 parent 6507c108

New IssueObserver class and spec.

Handles emails for new issues and reassigned issues.
Need to add creating a Note on Issue close.
app/models/issue_observer.rb 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +class IssueObserver < ActiveRecord::Observer
  2 + cattr_accessor :current_user
  3 +
  4 + def after_create(issue)
  5 + Notify.new_issue_email(issue.id) if issue.assignee != current_user
  6 + end
  7 +
  8 + def after_change(issue)
  9 + if issue.assignee_id_changed?
  10 + recipient_ids = [issue.assignee_id, issue.assignee_id_was].keep_if {|id| id != current_user.id }
  11 +
  12 + recipient_ids.each do |recipient_id|
  13 + Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was)
  14 + end
  15 + end
  16 + end
  17 +end
spec/models/issue_observer_spec.rb 0 → 100644
@@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
  1 +require 'spec_helper'
  2 +
  3 +describe IssueObserver do
  4 + let(:some_user) { Factory.new(:user, :id => 1) }
  5 + let(:assignee) { Factory.new(:user, :id => 2) }
  6 + let(:issue) { Factory.new(:issue, :id => 42, :assignee => assignee) }
  7 +
  8 + before(:each) { subject.stub(:current_user).and_return(some_user) }
  9 +
  10 + subject { IssueObserver.instance }
  11 +
  12 + context 'when an issue is created' do
  13 +
  14 + it 'sends an email to the assignee' do
  15 + Notify.should_receive(:new_issue_email).with(issue.id)
  16 +
  17 + subject.after_create(issue)
  18 + end
  19 +
  20 + it 'does not send an email to the assignee if assignee created the issue' do
  21 + subject.stub(:current_user).and_return(assignee)
  22 + Notify.should_not_receive(:new_issue_email)
  23 +
  24 + subject.after_create(issue)
  25 + end
  26 + end
  27 +
  28 + context 'when an issue is modified' do
  29 + it 'but not reassigned, does not send a reassigned email' do
  30 + issue.stub(:assignee_id_changed?).and_return(false)
  31 + Notify.should_not_receive(:reassigned_issue_email)
  32 +
  33 + subject.after_change(issue)
  34 + end
  35 +
  36 + context 'and is reassigned' do
  37 + let(:previous_assignee) { Factory.new(:user, :id => 3) }
  38 +
  39 + before(:each) do
  40 + issue.stub(:assignee_id_changed?).and_return(true)
  41 + issue.stub(:assignee_id_was).and_return(previous_assignee.id)
  42 + end
  43 +
  44 + it 'sends a reassigned email to the previous and current assignees' do
  45 + Notify.should_receive(:reassigned_issue_email).with(assignee.id, issue.id, previous_assignee.id)
  46 + Notify.should_receive(:reassigned_issue_email).with(previous_assignee.id, issue.id, previous_assignee.id)
  47 +
  48 + subject.after_change(issue)
  49 + end
  50 +
  51 + context 'does not send an email to the user who made the reassignment' do
  52 + it 'if the user is the assignee' do
  53 + subject.stub(:current_user).and_return(assignee)
  54 + Notify.should_not_receive(:reassigned_issue_email).with(assignee.id, issue.id, previous_assignee.id)
  55 +
  56 + subject.after_change(issue)
  57 + end
  58 + it 'if the user is the previous assignee' do
  59 + subject.stub(:current_user).and_return(previous_assignee)
  60 + Notify.should_not_receive(:reassigned_issue_email).with(previous_assignee.id, issue.id, previous_assignee.id)
  61 +
  62 + subject.after_change(issue)
  63 + end
  64 + end
  65 + end
  66 + end
  67 +end