From 7713f7fefb4601b7ddede29d61fbf80e2b2929b8 Mon Sep 17 00:00:00 2001
From: Valery Sizov <vsv2711@gmail.com>
Date: Sat, 17 Dec 2011 15:58:35 +0200
Subject: [PATCH] Notification refactoring

---
 app/controllers/admin/users_controller.rb |  1 -
 app/controllers/application_controller.rb |  5 +++
 app/controllers/issues_controller.rb      |  5 +--
 app/controllers/notes_controller.rb       | 24 ++-----------
 app/models/mailer_observer.rb             | 41 +++++++++++++++++++++++
 app/models/note.rb                        |  5 +++
 config/application.rb                     |  2 +-
 7 files changed, 55 insertions(+), 28 deletions(-)
 create mode 100644 app/models/mailer_observer.rb

diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index 1c008d90f7d7c..033332a78e355 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -27,7 +27,6 @@ def create
 
     respond_to do |format|
       if @admin_user.save
-        Notify.new_user_email(@admin_user, params[:user][:password]).deliver
         format.html { redirect_to [:admin, @admin_user], notice: 'User was successfully created.' }
         format.json { render json: @admin_user, status: :created, location: @admin_user }
       else
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 6f7ed9a3ce410..00ab93a153dd7 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,5 +1,6 @@
 class ApplicationController < ActionController::Base
   before_filter :authenticate_user!
+  before_filter :set_current_user_for_mailer
   protect_from_forgery
   helper_method :abilities, :can?
 
@@ -19,6 +20,10 @@ def layout_by_resource
     end
   end
 
+  def set_current_user_for_mailer
+    MailerObserver.current_user = current_user
+  end
+
   def abilities
     @abilities ||= Six.new
   end
diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb
index 9bf22d8cddc53..143bc19104410 100644
--- a/app/controllers/issues_controller.rb
+++ b/app/controllers/issues_controller.rb
@@ -67,10 +67,7 @@ def show
   def create
     @issue = @project.issues.new(params[:issue])
     @issue.author = current_user
-
-    if @issue.save && @issue.assignee != current_user
-      Notify.new_issue_email(@issue).deliver
-    end
+    @issue.save
 
     respond_with(@issue)
   end
diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb
index b8e04f1c1a805..5d99d1e273e60 100644
--- a/app/controllers/notes_controller.rb
+++ b/app/controllers/notes_controller.rb
@@ -12,10 +12,8 @@ class NotesController < ApplicationController
   def create
     @note = @project.notes.new(params[:note])
     @note.author = current_user
-
-    if @note.save
-      notify if params[:notify] == '1'
-    end
+    @note.notify = true if params[:notify] == '1'
+    @note.save
 
     respond_to do |format|
       format.html {redirect_to :back}
@@ -35,22 +33,4 @@ def destroy
     end
   end
 
-  protected
-
-  def notify
-    @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
diff --git a/app/models/mailer_observer.rb b/app/models/mailer_observer.rb
new file mode 100644
index 0000000000000..0f1e498d300d7
--- /dev/null
+++ b/app/models/mailer_observer.rb
@@ -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
diff --git a/app/models/note.rb b/app/models/note.rb
index 946f506264a21..9a38cd77ffa02 100644
--- a/app/models/note.rb
+++ b/app/models/note.rb
@@ -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
 #
diff --git a/config/application.rb b/config/application.rb
index 3481c6d61e453..bdd5bbf35b8d0 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -23,7 +23,7 @@ class Application < Rails::Application
     # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
 
     # Activate observers that should always be running.
-    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+    config.active_record.observers = :mailer_observer
 
     # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
     # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
-- 
GitLab