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
class ApplicationController < ActionController::Base
before_filter :authenticate_user!
+ before_filter :set_current_user_for_mailer
protect_from_forgery
helper_method :abilities, :can?
end
end
+ def set_current_user_for_mailer
+ MailerObserver.current_user = current_user
+ end
+
def abilities
@abilities ||= Six.new
end
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
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}
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
--- /dev/null
+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
:prefix => true
attr_protected :author, :author_id
+ attr_accessor :notify
validates_presence_of :project
scope :inc_author, includes(:author)
mount_uploader :attachment, AttachmentUploader
+
+ def notify
+ @notify ||= false
+ end
end
# == Schema Information
#
# 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.