Notification refactoring

This commit is contained in:
Valery Sizov 2011-12-17 15:58:35 +02:00
parent f7859ec1ed
commit 7713f7fefb
7 changed files with 55 additions and 28 deletions

View file

@ -0,0 +1,41 @@
class MailerObserver < ActiveRecord::Observer
observe :issue, :user, :note, :snippet
cattr_accessor :current_user
def after_create(model)
new_issue(model) if model.kind_of?(Issue)
new_user(model) if model.kind_of?(User)
new_note(model) if model.kind_of?(Note)
end
protected
def new_issue(issue)
if issue.assignee != current_user
Notify.new_issue_email(issue).deliver
end
end
def new_user(user)
Notify.new_user_email(user, user.password).deliver
end
def new_note(note)
return unless 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, note).deliver
when "Issue" then
Notify.note_issue_email(u, note).deliver
when "MergeRequest"
true # someone should write email notification
when "Snippet"
true
else
Notify.note_wall_email(u, note).deliver
end
end
end
end

View file

@ -13,6 +13,7 @@ class Note < ActiveRecord::Base
:prefix => true
attr_protected :author, :author_id
attr_accessor :notify
validates_presence_of :project
@ -35,6 +36,10 @@ class Note < ActiveRecord::Base
scope :inc_author, includes(:author)
mount_uploader :attachment, AttachmentUploader
def notify
@notify ||= false
end
end
# == Schema Information
#