From f6035552e5d83b36f69e1ac7fa4b40ee5f7ab4fb Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Thu, 17 May 2012 17:23:34 -0400 Subject: [PATCH] New IssueObserver class and spec. --- app/models/issue_observer.rb | 17 +++++++++++++++++ spec/models/issue_observer_spec.rb | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 0 deletions(-) create mode 100644 app/models/issue_observer.rb create mode 100644 spec/models/issue_observer_spec.rb diff --git a/app/models/issue_observer.rb b/app/models/issue_observer.rb new file mode 100644 index 0000000..e18195a --- /dev/null +++ b/app/models/issue_observer.rb @@ -0,0 +1,17 @@ +class IssueObserver < ActiveRecord::Observer + cattr_accessor :current_user + + def after_create(issue) + Notify.new_issue_email(issue.id) if issue.assignee != current_user + end + + def after_change(issue) + if issue.assignee_id_changed? + recipient_ids = [issue.assignee_id, issue.assignee_id_was].keep_if {|id| id != current_user.id } + + recipient_ids.each do |recipient_id| + Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was) + end + end + end +end diff --git a/spec/models/issue_observer_spec.rb b/spec/models/issue_observer_spec.rb new file mode 100644 index 0000000..166e03c --- /dev/null +++ b/spec/models/issue_observer_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe IssueObserver do + let(:some_user) { Factory.new(:user, :id => 1) } + let(:assignee) { Factory.new(:user, :id => 2) } + let(:issue) { Factory.new(:issue, :id => 42, :assignee => assignee) } + + before(:each) { subject.stub(:current_user).and_return(some_user) } + + subject { IssueObserver.instance } + + context 'when an issue is created' do + + it 'sends an email to the assignee' do + Notify.should_receive(:new_issue_email).with(issue.id) + + subject.after_create(issue) + end + + it 'does not send an email to the assignee if assignee created the issue' do + subject.stub(:current_user).and_return(assignee) + Notify.should_not_receive(:new_issue_email) + + subject.after_create(issue) + end + end + + context 'when an issue is modified' do + it 'but not reassigned, does not send a reassigned email' do + issue.stub(:assignee_id_changed?).and_return(false) + Notify.should_not_receive(:reassigned_issue_email) + + subject.after_change(issue) + end + + context 'and is reassigned' do + let(:previous_assignee) { Factory.new(:user, :id => 3) } + + before(:each) do + issue.stub(:assignee_id_changed?).and_return(true) + issue.stub(:assignee_id_was).and_return(previous_assignee.id) + end + + it 'sends a reassigned email to the previous and current assignees' do + Notify.should_receive(:reassigned_issue_email).with(assignee.id, issue.id, previous_assignee.id) + Notify.should_receive(:reassigned_issue_email).with(previous_assignee.id, issue.id, previous_assignee.id) + + subject.after_change(issue) + end + + context 'does not send an email to the user who made the reassignment' do + it 'if the user is the assignee' do + subject.stub(:current_user).and_return(assignee) + Notify.should_not_receive(:reassigned_issue_email).with(assignee.id, issue.id, previous_assignee.id) + + subject.after_change(issue) + end + it 'if the user is the previous assignee' do + subject.stub(:current_user).and_return(previous_assignee) + Notify.should_not_receive(:reassigned_issue_email).with(previous_assignee.id, issue.id, previous_assignee.id) + + subject.after_change(issue) + end + end + end + end +end -- libgit2 0.21.2