refactored mail observer

This commit is contained in:
Dmitriy Zaporozhets 2012-06-14 08:24:10 +03:00
parent f5ee330a13
commit 72b9c14ebc

View file

@ -27,25 +27,31 @@ class MailerObserver < ActiveRecord::Observer
end
def new_note(note)
# Notify whole team except author of note
if note.notify
note.project.users.reject { |u| u.id == current_user.id } .each do |u|
case note.noteable_type
when "Commit" then
Notify.note_commit_email(u.id, note.id).deliver
when "Issue" then
Notify.note_issue_email(u.id, note.id).deliver
when "MergeRequest" then
Notify.note_merge_request_email(u.id, note.id).deliver
when "Snippet"
true
else
Notify.note_wall_email(u.id, note.id).deliver
end
end
# Notify only author of resource
# Notify whole team except author of note
notify_note(note)
elsif note.notify_author
# Notify only author of resource
Notify.note_commit_email(note.commit_author.id, note.id).deliver
else
# Otherwise ignore it
nil
end
end
def notify_note note
# reject author of note from mail list
users = note.project.users.reject { |u| u.id == current_user.id }
users.each do |u|
case note.noteable_type
when "Commit"; Notify.note_commit_email(u.id, note.id).deliver
when "Issue"; Notify.note_issue_email(u.id, note.id).deliver
when "MergeRequest"; Notify.note_merge_request_email(u.id, note.id).deliver
when "Snippet"; true
else
Notify.note_wall_email(u.id, note.id).deliver
end
end
end
@ -56,37 +62,34 @@ class MailerObserver < ActiveRecord::Observer
end
def changed_merge_request(merge_request)
if merge_request.assignee_id_changed?
recipients_ids = merge_request.assignee_id_was, merge_request.assignee_id
recipients_ids.delete current_user.id
User.find(recipients_ids).each do |user|
Notify.reassigned_merge_request_email(user.id, merge_request.id, merge_request.assignee_id_was).deliver
end
end
if merge_request.closed_changed?
note = Note.new(:noteable => merge_request, :project => merge_request.project)
note.author = current_user
note.note = "_Status changed to #{merge_request.closed ? 'closed' : 'reopened'}_"
note.save()
end
status_notify_and_comment issue, :reassigned_merge_request_email
end
def changed_issue(issue)
if issue.assignee_id_changed?
recipients_ids = issue.assignee_id_was, issue.assignee_id
status_notify_and_comment issue, :reassigned_issue_email
end
# This method used for Issues & Merge Requests
#
# It create a comment for Issue or MR if someone close/reopen.
# It also notify via email if assignee was changed
#
def status_notify_and_comment target, mail_method
# If assigne changed - notify to recipients
if target.assignee_id_changed?
recipients_ids = target.assignee_id_was, target.assignee_id
recipients_ids.delete current_user.id
recipients_ids.each do |recipient_id|
Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver
Notify.send(mail_method, recipient_id, target.id, target.assignee_id_was).deliver
end
end
if issue.closed_changed?
note = Note.new(:noteable => issue, :project => issue.project)
# Create comment about status changed
if target.closed_changed?
note = Note.new(:noteable => target, :project => target.project)
note.author = current_user
note.note = "_Status changed to #{issue.closed ? 'closed' : 'reopened'}_"
note.note = "_Status changed to #{target.closed ? 'closed' : 'reopened'}_"
note.save()
end
end