Commit 413778b6458c71706685150f191becee5d398391
1 parent
853c69c4
Exists in
master
and in
4 other branches
Rename NoteObserver methods and clarify things
Showing
2 changed files
with
51 additions
and
27 deletions
Show diff stats
app/observers/note_observer.rb
| 1 | class NoteObserver < ActiveRecord::Observer | 1 | class NoteObserver < ActiveRecord::Observer |
| 2 | 2 | ||
| 3 | def after_create(note) | 3 | def after_create(note) |
| 4 | + send_notify_mails(note) | ||
| 5 | + end | ||
| 6 | + | ||
| 7 | + protected | ||
| 8 | + | ||
| 9 | + def send_notify_mails(note) | ||
| 4 | if note.notify | 10 | if note.notify |
| 5 | - # Notify whole team except author of note | ||
| 6 | - notify_team_of_new_note(note) | 11 | + notify_team(note) |
| 7 | elsif note.notify_author | 12 | elsif note.notify_author |
| 8 | # Notify only author of resource | 13 | # Notify only author of resource |
| 9 | Notify.note_commit_email(note.commit_author.id, note.id).deliver | 14 | Notify.note_commit_email(note.commit_author.id, note.id).deliver |
| @@ -13,15 +18,15 @@ class NoteObserver < ActiveRecord::Observer | @@ -13,15 +18,15 @@ class NoteObserver < ActiveRecord::Observer | ||
| 13 | end | 18 | end |
| 14 | end | 19 | end |
| 15 | 20 | ||
| 16 | - protected | ||
| 17 | - | ||
| 18 | - def notify_team_of_new_note(note) | ||
| 19 | - note_is_on = note.noteable_type || 'Wall' | ||
| 20 | - notify_method = 'note_' + note_is_on.underscore + '_email' | 21 | + # Notifies the whole team except the author of note |
| 22 | + def notify_team(note) | ||
| 23 | + # Note: wall posts are not "attached" to anything, so fall back to "Wall" | ||
| 24 | + noteable_type = note.noteable_type || "Wall" | ||
| 25 | + notify_method = "note_#{noteable_type.underscore}_email".to_sym | ||
| 21 | 26 | ||
| 22 | if Notify.respond_to? notify_method | 27 | if Notify.respond_to? notify_method |
| 23 | team_without_note_author(note).map do |u| | 28 | team_without_note_author(note).map do |u| |
| 24 | - Notify.send(notify_method.to_sym, u.id, note.id).deliver | 29 | + Notify.send(notify_method, u.id, note.id).deliver |
| 25 | end | 30 | end |
| 26 | end | 31 | end |
| 27 | end | 32 | end |
spec/observers/note_observer_spec.rb
| @@ -3,8 +3,11 @@ require 'spec_helper' | @@ -3,8 +3,11 @@ require 'spec_helper' | ||
| 3 | describe NoteObserver do | 3 | describe NoteObserver do |
| 4 | subject { NoteObserver.instance } | 4 | subject { NoteObserver.instance } |
| 5 | 5 | ||
| 6 | + let(:team_without_author) { (1..2).map { |n| double :user, id: n } } | ||
| 7 | + let(:delivery_success) { double deliver: true } | ||
| 8 | + | ||
| 6 | describe '#after_create' do | 9 | describe '#after_create' do |
| 7 | - let(:note) { double :note, notify: false, notify_author: false } | 10 | + let(:note) { double :note } |
| 8 | 11 | ||
| 9 | it 'is called after a note is created' do | 12 | it 'is called after a note is created' do |
| 10 | subject.should_receive :after_create | 13 | subject.should_receive :after_create |
| @@ -14,40 +17,51 @@ describe NoteObserver do | @@ -14,40 +17,51 @@ describe NoteObserver do | ||
| 14 | end | 17 | end |
| 15 | end | 18 | end |
| 16 | 19 | ||
| 20 | + it 'sends out notifications' do | ||
| 21 | + subject.should_receive(:send_notify_mails).with(note) | ||
| 22 | + | ||
| 23 | + subject.after_create(note) | ||
| 24 | + end | ||
| 25 | + end | ||
| 26 | + | ||
| 27 | + describe "#send_notify_mails" do | ||
| 28 | + let(:note) { double :note, notify: false, notify_author: false } | ||
| 29 | + | ||
| 17 | it 'notifies team of new note when flagged to notify' do | 30 | it 'notifies team of new note when flagged to notify' do |
| 18 | note.stub(:notify).and_return(true) | 31 | note.stub(:notify).and_return(true) |
| 19 | - subject.should_receive(:notify_team_of_new_note).with(note) | 32 | + subject.should_receive(:notify_team).with(note) |
| 20 | 33 | ||
| 21 | subject.after_create(note) | 34 | subject.after_create(note) |
| 22 | end | 35 | end |
| 36 | + | ||
| 23 | it 'does not notify team of new note when not flagged to notify' do | 37 | it 'does not notify team of new note when not flagged to notify' do |
| 24 | - subject.should_not_receive(:notify_team_of_new_note).with(note) | 38 | + subject.should_not_receive(:notify_team).with(note) |
| 25 | 39 | ||
| 26 | subject.after_create(note) | 40 | subject.after_create(note) |
| 27 | end | 41 | end |
| 42 | + | ||
| 28 | it 'notifies the author of a commit when flagged to notify the author' do | 43 | it 'notifies the author of a commit when flagged to notify the author' do |
| 29 | note.stub(:notify_author).and_return(true) | 44 | note.stub(:notify_author).and_return(true) |
| 30 | note.stub(:id).and_return(42) | 45 | note.stub(:id).and_return(42) |
| 31 | author = double :user, id: 1 | 46 | author = double :user, id: 1 |
| 32 | note.stub(:commit_author).and_return(author) | 47 | note.stub(:commit_author).and_return(author) |
| 33 | - Notify.should_receive(:note_commit_email).and_return(double(deliver: true)) | 48 | + Notify.should_receive(:note_commit_email).and_return(delivery_success) |
| 34 | 49 | ||
| 35 | subject.after_create(note) | 50 | subject.after_create(note) |
| 36 | end | 51 | end |
| 52 | + | ||
| 37 | it 'does not notify the author of a commit when not flagged to notify the author' do | 53 | it 'does not notify the author of a commit when not flagged to notify the author' do |
| 38 | Notify.should_not_receive(:note_commit_email) | 54 | Notify.should_not_receive(:note_commit_email) |
| 39 | 55 | ||
| 40 | subject.after_create(note) | 56 | subject.after_create(note) |
| 41 | end | 57 | end |
| 58 | + | ||
| 42 | it 'does nothing if no notify flags are set' do | 59 | it 'does nothing if no notify flags are set' do |
| 43 | subject.after_create(note).should be_nil | 60 | subject.after_create(note).should be_nil |
| 44 | end | 61 | end |
| 45 | end | 62 | end |
| 46 | 63 | ||
| 47 | - | ||
| 48 | - let(:team_without_author) { (1..2).map { |n| double :user, id: n } } | ||
| 49 | - | ||
| 50 | - describe '#notify_team_of_new_note' do | 64 | + describe '#notify_team' do |
| 51 | let(:note) { double :note, id: 1 } | 65 | let(:note) { double :note, id: 1 } |
| 52 | 66 | ||
| 53 | before :each do | 67 | before :each do |
| @@ -57,40 +71,45 @@ describe NoteObserver do | @@ -57,40 +71,45 @@ describe NoteObserver do | ||
| 57 | context 'notifies team of a new note on' do | 71 | context 'notifies team of a new note on' do |
| 58 | it 'a commit' do | 72 | it 'a commit' do |
| 59 | note.stub(:noteable_type).and_return('Commit') | 73 | note.stub(:noteable_type).and_return('Commit') |
| 60 | - Notify.should_receive(:note_commit_email).twice.and_return(double(deliver: true)) | 74 | + Notify.should_receive(:note_commit_email).twice.and_return(delivery_success) |
| 61 | 75 | ||
| 62 | - subject.send(:notify_team_of_new_note, note) | 76 | + subject.send(:notify_team, note) |
| 63 | end | 77 | end |
| 78 | + | ||
| 64 | it 'an issue' do | 79 | it 'an issue' do |
| 65 | note.stub(:noteable_type).and_return('Issue') | 80 | note.stub(:noteable_type).and_return('Issue') |
| 66 | - Notify.should_receive(:note_issue_email).twice.and_return(double(deliver: true)) | 81 | + Notify.should_receive(:note_issue_email).twice.and_return(delivery_success) |
| 67 | 82 | ||
| 68 | - subject.send(:notify_team_of_new_note, note) | 83 | + subject.send(:notify_team, note) |
| 69 | end | 84 | end |
| 85 | + | ||
| 70 | it 'a wiki page' do | 86 | it 'a wiki page' do |
| 71 | note.stub(:noteable_type).and_return('Wiki') | 87 | note.stub(:noteable_type).and_return('Wiki') |
| 72 | - Notify.should_receive(:note_wiki_email).twice.and_return(double(deliver: true)) | 88 | + Notify.should_receive(:note_wiki_email).twice.and_return(delivery_success) |
| 73 | 89 | ||
| 74 | - subject.send(:notify_team_of_new_note, note) | 90 | + subject.send(:notify_team, note) |
| 75 | end | 91 | end |
| 92 | + | ||
| 76 | it 'a merge request' do | 93 | it 'a merge request' do |
| 77 | note.stub(:noteable_type).and_return('MergeRequest') | 94 | note.stub(:noteable_type).and_return('MergeRequest') |
| 78 | - Notify.should_receive(:note_merge_request_email).twice.and_return(double(deliver: true)) | 95 | + Notify.should_receive(:note_merge_request_email).twice.and_return(delivery_success) |
| 79 | 96 | ||
| 80 | - subject.send(:notify_team_of_new_note, note) | 97 | + subject.send(:notify_team, note) |
| 81 | end | 98 | end |
| 99 | + | ||
| 82 | it 'a wall' do | 100 | it 'a wall' do |
| 101 | + # Note: wall posts have #noteable_type of nil | ||
| 83 | note.stub(:noteable_type).and_return(nil) | 102 | note.stub(:noteable_type).and_return(nil) |
| 84 | - Notify.should_receive(:note_wall_email).twice.and_return(double(deliver: true)) | 103 | + Notify.should_receive(:note_wall_email).twice.and_return(delivery_success) |
| 85 | 104 | ||
| 86 | - subject.send(:notify_team_of_new_note, note) | 105 | + subject.send(:notify_team, note) |
| 87 | end | 106 | end |
| 88 | end | 107 | end |
| 89 | 108 | ||
| 90 | it 'does nothing for a new note on a snippet' do | 109 | it 'does nothing for a new note on a snippet' do |
| 91 | note.stub(:noteable_type).and_return('Snippet') | 110 | note.stub(:noteable_type).and_return('Snippet') |
| 92 | 111 | ||
| 93 | - subject.send(:notify_team_of_new_note, note).should be_nil | 112 | + subject.send(:notify_team, note).should be_nil |
| 94 | end | 113 | end |
| 95 | end | 114 | end |
| 96 | 115 |