Commit 61e5fb6f370bc336799211d311131132a2159906
Exists in
master
and in
4 other branches
Merge pull request #1695 from riyad/cleanup-note-observer-and-notifications
Code cleanup on NoteObserver and Notify
Showing
3 changed files
with
117 additions
and
62 deletions
Show diff stats
app/mailers/notify.rb
| @@ -9,11 +9,11 @@ class Notify < ActionMailer::Base | @@ -9,11 +9,11 @@ class Notify < ActionMailer::Base | ||
| 9 | 9 | ||
| 10 | default from: Gitlab.config.email_from | 10 | default from: Gitlab.config.email_from |
| 11 | 11 | ||
| 12 | - def new_user_email(user_id, password) | ||
| 13 | - @user = User.find(user_id) | ||
| 14 | - @password = password | ||
| 15 | - mail(to: @user.email, subject: subject("Account was created for you")) | ||
| 16 | - end | 12 | + |
| 13 | + | ||
| 14 | + # | ||
| 15 | + # Issue | ||
| 16 | + # | ||
| 17 | 17 | ||
| 18 | def new_issue_email(issue_id) | 18 | def new_issue_email(issue_id) |
| 19 | @issue = Issue.find(issue_id) | 19 | @issue = Issue.find(issue_id) |
| @@ -21,12 +21,46 @@ class Notify < ActionMailer::Base | @@ -21,12 +21,46 @@ class Notify < ActionMailer::Base | ||
| 21 | mail(to: @issue.assignee_email, subject: subject("new issue ##{@issue.id}", @issue.title)) | 21 | mail(to: @issue.assignee_email, subject: subject("new issue ##{@issue.id}", @issue.title)) |
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | - def note_wall_email(recipient_id, note_id) | ||
| 25 | - @note = Note.find(note_id) | ||
| 26 | - @project = @note.project | ||
| 27 | - mail(to: recipient(recipient_id), subject: subject) | 24 | + def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id) |
| 25 | + @issue = Issue.find(issue_id) | ||
| 26 | + @previous_assignee ||= User.find(previous_assignee_id) | ||
| 27 | + @project = @issue.project | ||
| 28 | + mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title)) | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | + def issue_status_changed_email(recipient_id, issue_id, status, updated_by_user_id) | ||
| 32 | + @issue = Issue.find issue_id | ||
| 33 | + @issue_status = status | ||
| 34 | + @updated_by = User.find updated_by_user_id | ||
| 35 | + mail(to: recipient(recipient_id), | ||
| 36 | + subject: subject("changed issue ##{@issue.id}", @issue.title)) | ||
| 37 | + end | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + # | ||
| 42 | + # Merge Request | ||
| 43 | + # | ||
| 44 | + | ||
| 45 | + def new_merge_request_email(merge_request_id) | ||
| 46 | + @merge_request = MergeRequest.find(merge_request_id) | ||
| 47 | + @project = @merge_request.project | ||
| 48 | + mail(to: @merge_request.assignee_email, subject: subject("new merge request !#{@merge_request.id}", @merge_request.title)) | ||
| 49 | + end | ||
| 50 | + | ||
| 51 | + def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id) | ||
| 52 | + @merge_request = MergeRequest.find(merge_request_id) | ||
| 53 | + @previous_assignee ||= User.find(previous_assignee_id) | ||
| 54 | + @project = @merge_request.project | ||
| 55 | + mail(to: recipient(recipient_id), subject: subject("changed merge request !#{@merge_request.id}", @merge_request.title)) | ||
| 28 | end | 56 | end |
| 29 | 57 | ||
| 58 | + | ||
| 59 | + | ||
| 60 | + # | ||
| 61 | + # Note | ||
| 62 | + # | ||
| 63 | + | ||
| 30 | def note_commit_email(recipient_id, note_id) | 64 | def note_commit_email(recipient_id, note_id) |
| 31 | @note = Note.find(note_id) | 65 | @note = Note.find(note_id) |
| 32 | @commit = @note.noteable | 66 | @commit = @note.noteable |
| @@ -35,6 +69,13 @@ class Notify < ActionMailer::Base | @@ -35,6 +69,13 @@ class Notify < ActionMailer::Base | ||
| 35 | mail(to: recipient(recipient_id), subject: subject("note for commit #{@commit.short_id}", @commit.title)) | 69 | mail(to: recipient(recipient_id), subject: subject("note for commit #{@commit.short_id}", @commit.title)) |
| 36 | end | 70 | end |
| 37 | 71 | ||
| 72 | + def note_issue_email(recipient_id, note_id) | ||
| 73 | + @note = Note.find(note_id) | ||
| 74 | + @issue = @note.noteable | ||
| 75 | + @project = @note.project | ||
| 76 | + mail(to: recipient(recipient_id), subject: subject("note for issue ##{@issue.id}")) | ||
| 77 | + end | ||
| 78 | + | ||
| 38 | def note_merge_request_email(recipient_id, note_id) | 79 | def note_merge_request_email(recipient_id, note_id) |
| 39 | @note = Note.find(note_id) | 80 | @note = Note.find(note_id) |
| 40 | @merge_request = @note.noteable | 81 | @merge_request = @note.noteable |
| @@ -42,11 +83,10 @@ class Notify < ActionMailer::Base | @@ -42,11 +83,10 @@ class Notify < ActionMailer::Base | ||
| 42 | mail(to: recipient(recipient_id), subject: subject("note for merge request !#{@merge_request.id}")) | 83 | mail(to: recipient(recipient_id), subject: subject("note for merge request !#{@merge_request.id}")) |
| 43 | end | 84 | end |
| 44 | 85 | ||
| 45 | - def note_issue_email(recipient_id, note_id) | 86 | + def note_wall_email(recipient_id, note_id) |
| 46 | @note = Note.find(note_id) | 87 | @note = Note.find(note_id) |
| 47 | - @issue = @note.noteable | ||
| 48 | @project = @note.project | 88 | @project = @note.project |
| 49 | - mail(to: recipient(recipient_id), subject: subject("note for issue ##{@issue.id}")) | 89 | + mail(to: recipient(recipient_id), subject: subject) |
| 50 | end | 90 | end |
| 51 | 91 | ||
| 52 | def note_wiki_email(recipient_id, note_id) | 92 | def note_wiki_email(recipient_id, note_id) |
| @@ -56,25 +96,11 @@ class Notify < ActionMailer::Base | @@ -56,25 +96,11 @@ class Notify < ActionMailer::Base | ||
| 56 | mail(to: recipient(recipient_id), subject: subject("note for wiki")) | 96 | mail(to: recipient(recipient_id), subject: subject("note for wiki")) |
| 57 | end | 97 | end |
| 58 | 98 | ||
| 59 | - def new_merge_request_email(merge_request_id) | ||
| 60 | - @merge_request = MergeRequest.find(merge_request_id) | ||
| 61 | - @project = @merge_request.project | ||
| 62 | - mail(to: @merge_request.assignee_email, subject: subject("new merge request !#{@merge_request.id}", @merge_request.title)) | ||
| 63 | - end | ||
| 64 | 99 | ||
| 65 | - def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id) | ||
| 66 | - @merge_request = MergeRequest.find(merge_request_id) | ||
| 67 | - @previous_assignee ||= User.find(previous_assignee_id) | ||
| 68 | - @project = @merge_request.project | ||
| 69 | - mail(to: recipient(recipient_id), subject: subject("changed merge request !#{@merge_request.id}", @merge_request.title)) | ||
| 70 | - end | ||
| 71 | 100 | ||
| 72 | - def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id) | ||
| 73 | - @issue = Issue.find(issue_id) | ||
| 74 | - @previous_assignee ||= User.find(previous_assignee_id) | ||
| 75 | - @project = @issue.project | ||
| 76 | - mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title)) | ||
| 77 | - end | 101 | + # |
| 102 | + # Project | ||
| 103 | + # | ||
| 78 | 104 | ||
| 79 | def project_access_granted_email(user_project_id) | 105 | def project_access_granted_email(user_project_id) |
| 80 | @users_project = UsersProject.find user_project_id | 106 | @users_project = UsersProject.find user_project_id |
| @@ -83,14 +109,19 @@ class Notify < ActionMailer::Base | @@ -83,14 +109,19 @@ class Notify < ActionMailer::Base | ||
| 83 | subject: subject("access to project was granted")) | 109 | subject: subject("access to project was granted")) |
| 84 | end | 110 | end |
| 85 | 111 | ||
| 86 | - def issue_status_changed_email(recipient_id, issue_id, status, updated_by_user_id) | ||
| 87 | - @issue = Issue.find issue_id | ||
| 88 | - @issue_status = status | ||
| 89 | - @updated_by = User.find updated_by_user_id | ||
| 90 | - mail(to: recipient(recipient_id), | ||
| 91 | - subject: subject("changed issue ##{@issue.id}", @issue.title)) | 112 | + |
| 113 | + | ||
| 114 | + # | ||
| 115 | + # User | ||
| 116 | + # | ||
| 117 | + | ||
| 118 | + def new_user_email(user_id, password) | ||
| 119 | + @user = User.find(user_id) | ||
| 120 | + @password = password | ||
| 121 | + mail(to: @user.email, subject: subject("Account was created for you")) | ||
| 92 | end | 122 | end |
| 93 | 123 | ||
| 124 | + | ||
| 94 | private | 125 | private |
| 95 | 126 | ||
| 96 | # Look up a User by their ID and return their email address | 127 | # Look up a User by their ID and return their email address |
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 |