Commit 652d955c92937d74bd81b9af4adf3b1549d89876

Authored by Robert Speicher
1 parent b2b88b2f

Remove some duplication in the Notify mailer

app/mailers/notify.rb
... ... @@ -12,20 +12,20 @@ class Notify < ActionMailer::Base
12 12 def new_user_email(user_id, password)
13 13 @user = User.find(user_id)
14 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 16 end
17 17  
18 18 def new_issue_email(issue_id)
19 19 @issue = Issue.find(issue_id)
20 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 22 end
23 23  
24 24 def note_wall_email(recipient_id, note_id)
25 25 recipient = User.find(recipient_id)
26 26 @note = Note.find(note_id)
27 27 @project = @note.project
28   - mail(to: recipient.email, subject: "gitlab | #{@project.name}")
  28 + mail(to: recipient.email, subject: subject)
29 29 end
30 30  
31 31 def note_commit_email(recipient_id, note_id)
... ... @@ -34,7 +34,7 @@ class Notify < ActionMailer::Base
34 34 @commit = @note.target
35 35 @commit = CommitDecorator.decorate(@commit)
36 36 @project = @note.project
37   - mail(to: recipient.email, subject: "gitlab | note for commit #{@commit.short_id} | #{@commit.title} | #{@project.name}")
  37 + mail(to: recipient.email, subject: subject("note for commit #{@commit.short_id}", @commit.title))
38 38 end
39 39  
40 40 def note_merge_request_email(recipient_id, note_id)
... ... @@ -42,7 +42,7 @@ class Notify < ActionMailer::Base
42 42 @note = Note.find(note_id)
43 43 @merge_request = @note.noteable
44 44 @project = @note.project
45   - mail(to: recipient.email, subject: "gitlab | note for merge request !#{@merge_request.id} | #{@project.name}")
  45 + mail(to: recipient.email, subject: subject("note for merge request !#{@merge_request.id}"))
46 46 end
47 47  
48 48 def note_issue_email(recipient_id, note_id)
... ... @@ -50,7 +50,7 @@ class Notify < ActionMailer::Base
50 50 @note = Note.find(note_id)
51 51 @issue = @note.noteable
52 52 @project = @note.project
53   - mail(to: recipient.email, subject: "gitlab | note for issue ##{@issue.id} | #{@project.name}")
  53 + mail(to: recipient.email, subject: subject("note for issue ##{@issue.id}"))
54 54 end
55 55  
56 56 def note_wiki_email(recipient_id, note_id)
... ... @@ -58,13 +58,13 @@ class Notify < ActionMailer::Base
58 58 @note = Note.find(note_id)
59 59 @wiki = @note.noteable
60 60 @project = @note.project
61   - mail(to: recipient.email, subject: "gitlab | note for wiki | #{@project.name}")
  61 + mail(to: recipient.email, subject: subject("note for wiki"))
62 62 end
63 63  
64 64 def new_merge_request_email(merge_request_id)
65 65 @merge_request = MergeRequest.find(merge_request_id)
66 66 @project = @merge_request.project
67   - mail(to: @merge_request.assignee_email, subject: "gitlab | new merge request !#{@merge_request.id} | #{@merge_request.title} | #{@project.name}")
  67 + mail(to: @merge_request.assignee_email, subject: subject("new merge request !#{@merge_request.id}", @merge_request.title))
68 68 end
69 69  
70 70 def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id)
... ... @@ -72,7 +72,7 @@ class Notify < ActionMailer::Base
72 72 @merge_request = MergeRequest.find(merge_request_id)
73 73 @previous_assignee ||= User.find(previous_assignee_id)
74 74 @project = @merge_request.project
75   - mail(to: recipient.email, subject: "gitlab | changed merge request !#{@merge_request.id} | #{@merge_request.title} | #{@project.name}")
  75 + mail(to: recipient.email, subject: subject("changed merge request !#{@merge_request.id}", @merge_request.title))
76 76 end
77 77  
78 78 def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id)
... ... @@ -80,6 +80,30 @@ class Notify < ActionMailer::Base
80 80 @issue = Issue.find(issue_id)
81 81 @previous_assignee ||= User.find(previous_assignee_id)
82 82 @project = @issue.project
83   - mail(to: recipient.email, subject: "gitlab | changed issue ##{@issue.id} | #{@issue.title} | #{@project.name}")
  83 + mail(to: recipient.email, subject: subject("changed issue ##{@issue.id}", @issue.title))
  84 + end
  85 +
  86 + private
  87 +
  88 + # Formats arguments into a String suitable for use as an email subject
  89 + #
  90 + # extra - Extra Strings to be inserted into the subject
  91 + #
  92 + # Examples
  93 + #
  94 + # >> subject('Lorem ipsum')
  95 + # => "gitlab | Lorem ipsum"
  96 + #
  97 + # # Automatically inserts Project name when @project is set
  98 + # >> @project = Project.last
  99 + # => #<Project id: 1, name: "Ruby on Rails", path: "ruby_on_rails", ...>
  100 + # >> subject('Lorem ipsum')
  101 + # => "gitlab | Lorem ipsum | Ruby on Rails"
  102 + #
  103 + # # Accepts multiple arguments
  104 + # >> subject('Lorem ipsum', 'Dolor sit amet')
  105 + # => "gitlab | Lorem ipsum | Dolor sit amet"
  106 + def subject(*extra)
  107 + "gitlab | " << extra.join(' | ') << (@project ? " | #{@project.name}" : "")
84 108 end
85 109 end
... ...
spec/mailers/notify_spec.rb
... ... @@ -24,7 +24,7 @@ describe Notify do
24 24 end
25 25  
26 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 28 end
29 29  
30 30 it 'contains the new user\'s login name' do
... ... @@ -60,7 +60,7 @@ describe Notify do
60 60 it_behaves_like 'an assignee email'
61 61  
62 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 64 end
65 65  
66 66 it 'contains a link to the new issue' do
... ... @@ -76,7 +76,7 @@ describe Notify do
76 76 it_behaves_like 'a multiple recipients email'
77 77  
78 78 it 'has the correct subject' do
79   - should have_subject /changed issue/
  79 + should have_subject /changed issue ##{issue.id} \| #{issue.title}/
80 80 end
81 81  
82 82 it 'contains the name of the previous assignee' do
... ...