Commit 72b9c14ebca18b065085f18590e09ccb6137dc6f
1 parent
f5ee330a
Exists in
master
and in
4 other branches
refactored mail observer
Showing
1 changed file
with
40 additions
and
37 deletions
Show diff stats
app/models/mailer_observer.rb
| ... | ... | @@ -27,25 +27,31 @@ class MailerObserver < ActiveRecord::Observer |
| 27 | 27 | end |
| 28 | 28 | |
| 29 | 29 | def new_note(note) |
| 30 | - # Notify whole team except author of note | |
| 31 | 30 | if note.notify |
| 32 | - note.project.users.reject { |u| u.id == current_user.id } .each do |u| | |
| 33 | - case note.noteable_type | |
| 34 | - when "Commit" then | |
| 35 | - Notify.note_commit_email(u.id, note.id).deliver | |
| 36 | - when "Issue" then | |
| 37 | - Notify.note_issue_email(u.id, note.id).deliver | |
| 38 | - when "MergeRequest" then | |
| 39 | - Notify.note_merge_request_email(u.id, note.id).deliver | |
| 40 | - when "Snippet" | |
| 41 | - true | |
| 42 | - else | |
| 43 | - Notify.note_wall_email(u.id, note.id).deliver | |
| 44 | - end | |
| 45 | - end | |
| 46 | - # Notify only author of resource | |
| 31 | + # Notify whole team except author of note | |
| 32 | + notify_note(note) | |
| 47 | 33 | elsif note.notify_author |
| 34 | + # Notify only author of resource | |
| 48 | 35 | Notify.note_commit_email(note.commit_author.id, note.id).deliver |
| 36 | + else | |
| 37 | + # Otherwise ignore it | |
| 38 | + nil | |
| 39 | + end | |
| 40 | + end | |
| 41 | + | |
| 42 | + def notify_note note | |
| 43 | + # reject author of note from mail list | |
| 44 | + users = note.project.users.reject { |u| u.id == current_user.id } | |
| 45 | + | |
| 46 | + users.each do |u| | |
| 47 | + case note.noteable_type | |
| 48 | + when "Commit"; Notify.note_commit_email(u.id, note.id).deliver | |
| 49 | + when "Issue"; Notify.note_issue_email(u.id, note.id).deliver | |
| 50 | + when "MergeRequest"; Notify.note_merge_request_email(u.id, note.id).deliver | |
| 51 | + when "Snippet"; true | |
| 52 | + else | |
| 53 | + Notify.note_wall_email(u.id, note.id).deliver | |
| 54 | + end | |
| 49 | 55 | end |
| 50 | 56 | end |
| 51 | 57 | |
| ... | ... | @@ -56,37 +62,34 @@ class MailerObserver < ActiveRecord::Observer |
| 56 | 62 | end |
| 57 | 63 | |
| 58 | 64 | def changed_merge_request(merge_request) |
| 59 | - if merge_request.assignee_id_changed? | |
| 60 | - recipients_ids = merge_request.assignee_id_was, merge_request.assignee_id | |
| 61 | - recipients_ids.delete current_user.id | |
| 62 | - | |
| 63 | - User.find(recipients_ids).each do |user| | |
| 64 | - Notify.reassigned_merge_request_email(user.id, merge_request.id, merge_request.assignee_id_was).deliver | |
| 65 | - end | |
| 66 | - end | |
| 67 | - | |
| 68 | - if merge_request.closed_changed? | |
| 69 | - note = Note.new(:noteable => merge_request, :project => merge_request.project) | |
| 70 | - note.author = current_user | |
| 71 | - note.note = "_Status changed to #{merge_request.closed ? 'closed' : 'reopened'}_" | |
| 72 | - note.save() | |
| 73 | - end | |
| 65 | + status_notify_and_comment issue, :reassigned_merge_request_email | |
| 74 | 66 | end |
| 75 | 67 | |
| 76 | 68 | def changed_issue(issue) |
| 77 | - if issue.assignee_id_changed? | |
| 78 | - recipients_ids = issue.assignee_id_was, issue.assignee_id | |
| 69 | + status_notify_and_comment issue, :reassigned_issue_email | |
| 70 | + end | |
| 71 | + | |
| 72 | + # This method used for Issues & Merge Requests | |
| 73 | + # | |
| 74 | + # It create a comment for Issue or MR if someone close/reopen. | |
| 75 | + # It also notify via email if assignee was changed | |
| 76 | + # | |
| 77 | + def status_notify_and_comment target, mail_method | |
| 78 | + # If assigne changed - notify to recipients | |
| 79 | + if target.assignee_id_changed? | |
| 80 | + recipients_ids = target.assignee_id_was, target.assignee_id | |
| 79 | 81 | recipients_ids.delete current_user.id |
| 80 | 82 | |
| 81 | 83 | recipients_ids.each do |recipient_id| |
| 82 | - Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver | |
| 84 | + Notify.send(mail_method, recipient_id, target.id, target.assignee_id_was).deliver | |
| 83 | 85 | end |
| 84 | 86 | end |
| 85 | 87 | |
| 86 | - if issue.closed_changed? | |
| 87 | - note = Note.new(:noteable => issue, :project => issue.project) | |
| 88 | + # Create comment about status changed | |
| 89 | + if target.closed_changed? | |
| 90 | + note = Note.new(:noteable => target, :project => target.project) | |
| 88 | 91 | note.author = current_user |
| 89 | - note.note = "_Status changed to #{issue.closed ? 'closed' : 'reopened'}_" | |
| 92 | + note.note = "_Status changed to #{target.closed ? 'closed' : 'reopened'}_" | |
| 90 | 93 | note.save() |
| 91 | 94 | end |
| 92 | 95 | end | ... | ... |