Commit 3784f134f1b2a34ce751bba0caf423d2779e604f

Authored by randx
2 parents f088eaa9 c5b13cc9

Merge branch 'refactor_notify' of https://github.com/tsigo/gitlabhq into tsigo-refactor_notify

app/mailers/notify.rb
... ... @@ -12,74 +12,102 @@ 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   - recipient = User.find(recipient_id)
26 25 @note = Note.find(note_id)
27 26 @project = @note.project
28   - mail(to: recipient.email, subject: "gitlab | #{@project.name}")
  27 + mail(to: recipient(recipient_id), subject: subject)
29 28 end
30 29  
31 30 def note_commit_email(recipient_id, note_id)
32   - recipient = User.find(recipient_id)
33 31 @note = Note.find(note_id)
34 32 @commit = @note.target
35 33 @commit = CommitDecorator.decorate(@commit)
36 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 36 end
39 37  
40 38 def note_merge_request_email(recipient_id, note_id)
41   - recipient = User.find(recipient_id)
42 39 @note = Note.find(note_id)
43 40 @merge_request = @note.noteable
44 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 43 end
47 44  
48 45 def note_issue_email(recipient_id, note_id)
49   - recipient = User.find(recipient_id)
50 46 @note = Note.find(note_id)
51 47 @issue = @note.noteable
52 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 50 end
55 51  
56 52 def note_wiki_email(recipient_id, note_id)
57   - recipient = User.find(recipient_id)
58 53 @note = Note.find(note_id)
59 54 @wiki = @note.noteable
60 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 57 end
63 58  
64 59 def new_merge_request_email(merge_request_id)
65 60 @merge_request = MergeRequest.find(merge_request_id)
66 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 63 end
69 64  
70 65 def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id)
71   - recipient = User.find(recipient_id)
72 66 @merge_request = MergeRequest.find(merge_request_id)
73 67 @previous_assignee ||= User.find(previous_assignee_id)
74 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 70 end
77 71  
78 72 def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id)
79   - recipient = User.find(recipient_id)
80 73 @issue = Issue.find(issue_id)
81 74 @previous_assignee ||= User.find(previous_assignee_id)
82 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 112 end
85 113 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
... ...