Merge branch 'separate_user_and_issue_observer_from_mail_observer' of https://github.com/robbkidd/gitlabhq into robbkidd-separate_user_and_issue_observer_from_mail_observer

This commit is contained in:
randx 2012-06-24 09:33:22 +03:00
commit 55f8338502
19 changed files with 358 additions and 49 deletions

View file

@ -27,7 +27,7 @@ class Issue < ActiveRecord::Base
validates :title,
:presence => true,
:length => { :within => 0..255 }
validates :description,
:length => { :within => 0..2000 }
@ -55,6 +55,18 @@ class Issue < ActiveRecord::Base
def new?
today? && created_at == updated_at
end
def is_being_reassigned?
assignee_id_changed?
end
def is_being_closed?
closed_changed? && closed
end
def is_being_reopened?
closed_changed? && !closed
end
end
# == Schema Information
#

View file

@ -0,0 +1,23 @@
class IssueObserver < ActiveRecord::Observer
cattr_accessor :current_user
def after_create(issue)
Notify.new_issue_email(issue.id).deliver if issue.assignee != current_user
end
def after_update(issue)
send_reassigned_email(issue) if issue.is_being_reassigned?
Note.create_status_change_note(issue, current_user, 'closed') if issue.is_being_closed?
Note.create_status_change_note(issue, current_user, 'reopened') if issue.is_being_reopened?
end
protected
def send_reassigned_email(issue)
recipient_ids = [issue.assignee_id, issue.assignee_id_was].keep_if {|id| id != current_user.id }
recipient_ids.each do |recipient_id|
Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver
end
end
end

View file

@ -1,31 +1,18 @@
class MailerObserver < ActiveRecord::Observer
observe :issue, :user, :note, :merge_request
observe :note, :merge_request
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)
new_merge_request(model) if model.kind_of?(MergeRequest)
end
def after_update(model)
changed_merge_request(model) if model.kind_of?(MergeRequest)
changed_issue(model) if model.kind_of?(Issue)
end
protected
def new_issue(issue)
if issue.assignee != current_user
Notify.new_issue_email(issue.id).deliver
end
end
def new_user(user)
Notify.new_user_email(user.id, user.password).deliver
end
def new_note(note)
if note.notify
# Notify whole team except author of note
@ -65,12 +52,8 @@ class MailerObserver < ActiveRecord::Observer
status_notify_and_comment merge_request, :reassigned_merge_request_email
end
def changed_issue(issue)
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
#

View file

@ -42,6 +42,14 @@ class Note < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
def self.create_status_change_note(noteable, author, status)
create({ :noteable => noteable,
:project => noteable.project,
:author => author,
:note => "_Status changed to #{status}_" },
:without_protection => true)
end
def notify
@notify ||= false
end

View file

@ -0,0 +1,5 @@
class UserObserver < ActiveRecord::Observer
def after_create(user)
Notify.new_user_email(user.id, user.password).deliver
end
end