Commit 3784f134f1b2a34ce751bba0caf423d2779e604f
Exists in
master
and in
4 other branches
Merge branch 'refactor_notify' of https://github.com/tsigo/gitlabhq into tsigo-refactor_notify
Showing
2 changed files
with
48 additions
and
20 deletions
Show diff stats
app/mailers/notify.rb
@@ -12,74 +12,102 @@ class Notify < ActionMailer::Base | @@ -12,74 +12,102 @@ class Notify < ActionMailer::Base | ||
12 | def new_user_email(user_id, password) | 12 | def new_user_email(user_id, password) |
13 | @user = User.find(user_id) | 13 | @user = User.find(user_id) |
14 | @password = password | 14 | @password = password |
15 | - mail(to: @user.email, subject: "gitlab | Account was created for you") | 15 | + mail(to: @user.email, subject: subject("Account was created for you")) |
16 | end | 16 | end |
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) |
20 | @project = @issue.project | 20 | @project = @issue.project |
21 | - mail(to: @issue.assignee_email, subject: "gitlab | new issue ##{@issue.id} | #{@issue.title} | #{@project.name}") | 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) | 24 | def note_wall_email(recipient_id, note_id) |
25 | - recipient = User.find(recipient_id) | ||
26 | @note = Note.find(note_id) | 25 | @note = Note.find(note_id) |
27 | @project = @note.project | 26 | @project = @note.project |
28 | - mail(to: recipient.email, subject: "gitlab | #{@project.name}") | 27 | + mail(to: recipient(recipient_id), subject: subject) |
29 | end | 28 | end |
30 | 29 | ||
31 | def note_commit_email(recipient_id, note_id) | 30 | def note_commit_email(recipient_id, note_id) |
32 | - recipient = User.find(recipient_id) | ||
33 | @note = Note.find(note_id) | 31 | @note = Note.find(note_id) |
34 | @commit = @note.target | 32 | @commit = @note.target |
35 | @commit = CommitDecorator.decorate(@commit) | 33 | @commit = CommitDecorator.decorate(@commit) |
36 | @project = @note.project | 34 | @project = @note.project |
37 | - mail(to: recipient.email, subject: "gitlab | note for commit #{@commit.short_id} | #{@commit.title} | #{@project.name}") | 35 | + mail(to: recipient(recipient_id), subject: subject("note for commit #{@commit.short_id}", @commit.title)) |
38 | end | 36 | end |
39 | 37 | ||
40 | def note_merge_request_email(recipient_id, note_id) | 38 | def note_merge_request_email(recipient_id, note_id) |
41 | - recipient = User.find(recipient_id) | ||
42 | @note = Note.find(note_id) | 39 | @note = Note.find(note_id) |
43 | @merge_request = @note.noteable | 40 | @merge_request = @note.noteable |
44 | @project = @note.project | 41 | @project = @note.project |
45 | - mail(to: recipient.email, subject: "gitlab | note for merge request !#{@merge_request.id} | #{@project.name}") | 42 | + mail(to: recipient(recipient_id), subject: subject("note for merge request !#{@merge_request.id}")) |
46 | end | 43 | end |
47 | 44 | ||
48 | def note_issue_email(recipient_id, note_id) | 45 | def note_issue_email(recipient_id, note_id) |
49 | - recipient = User.find(recipient_id) | ||
50 | @note = Note.find(note_id) | 46 | @note = Note.find(note_id) |
51 | @issue = @note.noteable | 47 | @issue = @note.noteable |
52 | @project = @note.project | 48 | @project = @note.project |
53 | - mail(to: recipient.email, subject: "gitlab | note for issue ##{@issue.id} | #{@project.name}") | 49 | + mail(to: recipient(recipient_id), subject: subject("note for issue ##{@issue.id}")) |
54 | end | 50 | end |
55 | 51 | ||
56 | def note_wiki_email(recipient_id, note_id) | 52 | def note_wiki_email(recipient_id, note_id) |
57 | - recipient = User.find(recipient_id) | ||
58 | @note = Note.find(note_id) | 53 | @note = Note.find(note_id) |
59 | @wiki = @note.noteable | 54 | @wiki = @note.noteable |
60 | @project = @note.project | 55 | @project = @note.project |
61 | - mail(to: recipient.email, subject: "gitlab | note for wiki | #{@project.name}") | 56 | + mail(to: recipient(recipient_id), subject: subject("note for wiki")) |
62 | end | 57 | end |
63 | 58 | ||
64 | def new_merge_request_email(merge_request_id) | 59 | def new_merge_request_email(merge_request_id) |
65 | @merge_request = MergeRequest.find(merge_request_id) | 60 | @merge_request = MergeRequest.find(merge_request_id) |
66 | @project = @merge_request.project | 61 | @project = @merge_request.project |
67 | - mail(to: @merge_request.assignee_email, subject: "gitlab | new merge request !#{@merge_request.id} | #{@merge_request.title} | #{@project.name}") | 62 | + mail(to: @merge_request.assignee_email, subject: subject("new merge request !#{@merge_request.id}", @merge_request.title)) |
68 | end | 63 | end |
69 | 64 | ||
70 | def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id) | 65 | def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id) |
71 | - recipient = User.find(recipient_id) | ||
72 | @merge_request = MergeRequest.find(merge_request_id) | 66 | @merge_request = MergeRequest.find(merge_request_id) |
73 | @previous_assignee ||= User.find(previous_assignee_id) | 67 | @previous_assignee ||= User.find(previous_assignee_id) |
74 | @project = @merge_request.project | 68 | @project = @merge_request.project |
75 | - mail(to: recipient.email, subject: "gitlab | changed merge request !#{@merge_request.id} | #{@merge_request.title} | #{@project.name}") | 69 | + mail(to: recipient(recipient_id), subject: subject("changed merge request !#{@merge_request.id}", @merge_request.title)) |
76 | end | 70 | end |
77 | 71 | ||
78 | def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id) | 72 | def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id) |
79 | - recipient = User.find(recipient_id) | ||
80 | @issue = Issue.find(issue_id) | 73 | @issue = Issue.find(issue_id) |
81 | @previous_assignee ||= User.find(previous_assignee_id) | 74 | @previous_assignee ||= User.find(previous_assignee_id) |
82 | @project = @issue.project | 75 | @project = @issue.project |
83 | - mail(to: recipient.email, subject: "gitlab | changed issue ##{@issue.id} | #{@issue.title} | #{@project.name}") | 76 | + mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title)) |
77 | + end | ||
78 | + | ||
79 | + private | ||
80 | + | ||
81 | + # Look up a User by their ID and return their email address | ||
82 | + # | ||
83 | + # recipient_id - User ID | ||
84 | + # | ||
85 | + # Returns a String containing the User's email address. | ||
86 | + def recipient(recipient_id) | ||
87 | + if recipient = User.find(recipient_id) | ||
88 | + recipient.email | ||
89 | + end | ||
90 | + end | ||
91 | + | ||
92 | + # Formats arguments into a String suitable for use as an email subject | ||
93 | + # | ||
94 | + # extra - Extra Strings to be inserted into the subject | ||
95 | + # | ||
96 | + # Examples | ||
97 | + # | ||
98 | + # >> subject('Lorem ipsum') | ||
99 | + # => "gitlab | Lorem ipsum" | ||
100 | + # | ||
101 | + # # Automatically inserts Project name when @project is set | ||
102 | + # >> @project = Project.last | ||
103 | + # => #<Project id: 1, name: "Ruby on Rails", path: "ruby_on_rails", ...> | ||
104 | + # >> subject('Lorem ipsum') | ||
105 | + # => "gitlab | Lorem ipsum | Ruby on Rails" | ||
106 | + # | ||
107 | + # # Accepts multiple arguments | ||
108 | + # >> subject('Lorem ipsum', 'Dolor sit amet') | ||
109 | + # => "gitlab | Lorem ipsum | Dolor sit amet" | ||
110 | + def subject(*extra) | ||
111 | + "gitlab | " << extra.join(' | ') << (@project ? " | #{@project.name}" : "") | ||
84 | end | 112 | end |
85 | end | 113 | end |
spec/mailers/notify_spec.rb
@@ -24,7 +24,7 @@ describe Notify do | @@ -24,7 +24,7 @@ describe Notify do | ||
24 | end | 24 | end |
25 | 25 | ||
26 | it 'has the correct subject' do | 26 | it 'has the correct subject' do |
27 | - should have_subject /Account was created for you/ | 27 | + should have_subject /^gitlab \| Account was created for you$/ |
28 | end | 28 | end |
29 | 29 | ||
30 | it 'contains the new user\'s login name' do | 30 | it 'contains the new user\'s login name' do |
@@ -60,7 +60,7 @@ describe Notify do | @@ -60,7 +60,7 @@ describe Notify do | ||
60 | it_behaves_like 'an assignee email' | 60 | it_behaves_like 'an assignee email' |
61 | 61 | ||
62 | it 'has the correct subject' do | 62 | it 'has the correct subject' do |
63 | - should have_subject /new issue ##{issue.id}/ | 63 | + should have_subject /new issue ##{issue.id} \| #{issue.title} \| #{project.name}/ |
64 | end | 64 | end |
65 | 65 | ||
66 | it 'contains a link to the new issue' do | 66 | it 'contains a link to the new issue' do |
@@ -76,7 +76,7 @@ describe Notify do | @@ -76,7 +76,7 @@ describe Notify do | ||
76 | it_behaves_like 'a multiple recipients email' | 76 | it_behaves_like 'a multiple recipients email' |
77 | 77 | ||
78 | it 'has the correct subject' do | 78 | it 'has the correct subject' do |
79 | - should have_subject /changed issue/ | 79 | + should have_subject /changed issue ##{issue.id} \| #{issue.title}/ |
80 | end | 80 | end |
81 | 81 | ||
82 | it 'contains the name of the previous assignee' do | 82 | it 'contains the name of the previous assignee' do |