Commit 6d92aa6d12b875b4987666da98658191ae426892

Authored by randx
1 parent 55f83385

Fix IssueObserver current_user assign. Refactored observers

app/controllers/application_controller.rb
1 class ApplicationController < ActionController::Base 1 class ApplicationController < ActionController::Base
2 before_filter :authenticate_user! 2 before_filter :authenticate_user!
3 before_filter :reject_blocked! 3 before_filter :reject_blocked!
4 - before_filter :set_current_user_for_mailer, :check_token_auth 4 + before_filter :set_current_user_for_mailer
  5 + before_filter :check_token_auth
  6 + before_filter :set_current_user_for_observers
  7 +
5 protect_from_forgery 8 protect_from_forgery
  9 +
6 helper_method :abilities, :can? 10 helper_method :abilities, :can?
7 11
8 rescue_from Gitlab::Gitolite::AccessDenied do |exception| 12 rescue_from Gitlab::Gitolite::AccessDenied do |exception|
@@ -58,6 +62,10 @@ class ApplicationController &lt; ActionController::Base @@ -58,6 +62,10 @@ class ApplicationController &lt; ActionController::Base
58 MailerObserver.current_user = current_user 62 MailerObserver.current_user = current_user
59 end 63 end
60 64
  65 + def set_current_user_for_observers
  66 + IssueObserver.current_user = current_user
  67 + end
  68 +
61 def abilities 69 def abilities
62 @abilities ||= Six.new 70 @abilities ||= Six.new
63 end 71 end
app/models/activity_observer.rb
@@ -1,25 +0,0 @@ @@ -1,25 +0,0 @@
1 -class ActivityObserver < ActiveRecord::Observer  
2 - observe :issue, :merge_request  
3 -  
4 - def after_create(record)  
5 - Event.create(  
6 - :project => record.project,  
7 - :target_id => record.id,  
8 - :target_type => record.class.name,  
9 - :action => Event.determine_action(record),  
10 - :author_id => record.author_id  
11 - )  
12 - end  
13 -  
14 - def after_save(record)  
15 - if record.changed.include?("closed")  
16 - Event.create(  
17 - :project => record.project,  
18 - :target_id => record.id,  
19 - :target_type => record.class.name,  
20 - :action => (record.closed ? Event::Closed : Event::Reopened),  
21 - :author_id => record.author_id_of_changes  
22 - )  
23 - end  
24 - end  
25 -end  
app/models/issue_observer.rb
@@ -1,23 +0,0 @@ @@ -1,23 +0,0 @@
1 -class IssueObserver < ActiveRecord::Observer  
2 - cattr_accessor :current_user  
3 -  
4 - def after_create(issue)  
5 - Notify.new_issue_email(issue.id).deliver if issue.assignee != current_user  
6 - end  
7 -  
8 - def after_update(issue)  
9 - send_reassigned_email(issue) if issue.is_being_reassigned?  
10 - Note.create_status_change_note(issue, current_user, 'closed') if issue.is_being_closed?  
11 - Note.create_status_change_note(issue, current_user, 'reopened') if issue.is_being_reopened?  
12 - end  
13 -  
14 - protected  
15 -  
16 - def send_reassigned_email(issue)  
17 - recipient_ids = [issue.assignee_id, issue.assignee_id_was].keep_if {|id| id != current_user.id }  
18 -  
19 - recipient_ids.each do |recipient_id|  
20 - Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver  
21 - end  
22 - end  
23 -end  
app/models/key_observer.rb
@@ -1,9 +0,0 @@ @@ -1,9 +0,0 @@
1 -class KeyObserver < ActiveRecord::Observer  
2 - def after_save(key)  
3 - key.update_repository  
4 - end  
5 -  
6 - def after_destroy(key)  
7 - key.repository_delete_key  
8 - end  
9 -end  
app/models/mailer_observer.rb
@@ -1,79 +0,0 @@ @@ -1,79 +0,0 @@
1 -class MailerObserver < ActiveRecord::Observer  
2 - observe :note, :merge_request  
3 - cattr_accessor :current_user  
4 -  
5 - def after_create(model)  
6 - new_note(model) if model.kind_of?(Note)  
7 - new_merge_request(model) if model.kind_of?(MergeRequest)  
8 - end  
9 -  
10 - def after_update(model)  
11 - changed_merge_request(model) if model.kind_of?(MergeRequest)  
12 - end  
13 -  
14 - protected  
15 -  
16 - def new_note(note)  
17 - if note.notify  
18 - # Notify whole team except author of note  
19 - notify_note(note)  
20 - elsif note.notify_author  
21 - # Notify only author of resource  
22 - Notify.note_commit_email(note.commit_author.id, note.id).deliver  
23 - else  
24 - # Otherwise ignore it  
25 - nil  
26 - end  
27 - end  
28 -  
29 - def notify_note note  
30 - # reject author of note from mail list  
31 - users = note.project.users.reject { |u| u.id == current_user.id }  
32 -  
33 - users.each do |u|  
34 - case note.noteable_type  
35 - when "Commit"; Notify.note_commit_email(u.id, note.id).deliver  
36 - when "Issue"; Notify.note_issue_email(u.id, note.id).deliver  
37 - when "MergeRequest"; Notify.note_merge_request_email(u.id, note.id).deliver  
38 - when "Snippet"; true  
39 - else  
40 - Notify.note_wall_email(u.id, note.id).deliver  
41 - end  
42 - end  
43 - end  
44 -  
45 - def new_merge_request(merge_request)  
46 - if merge_request.assignee != current_user  
47 - Notify.new_merge_request_email(merge_request.id).deliver  
48 - end  
49 - end  
50 -  
51 - def changed_merge_request(merge_request)  
52 - status_notify_and_comment merge_request, :reassigned_merge_request_email  
53 - end  
54 -  
55 - # This method used for Issues & Merge Requests  
56 - #  
57 - # It create a comment for Issue or MR if someone close/reopen.  
58 - # It also notify via email if assignee was changed  
59 - #  
60 - def status_notify_and_comment target, mail_method  
61 - # If assigne changed - notify to recipients  
62 - if target.assignee_id_changed?  
63 - recipients_ids = target.assignee_id_was, target.assignee_id  
64 - recipients_ids.delete current_user.id  
65 -  
66 - recipients_ids.each do |recipient_id|  
67 - Notify.send(mail_method, recipient_id, target.id, target.assignee_id_was).deliver  
68 - end  
69 - end  
70 -  
71 - # Create comment about status changed  
72 - if target.closed_changed?  
73 - note = Note.new(:noteable => target, :project => target.project)  
74 - note.author = current_user  
75 - note.note = "_Status changed to #{target.closed ? 'closed' : 'reopened'}_"  
76 - note.save()  
77 - end  
78 - end  
79 -end  
app/models/project_observer.rb
@@ -1,9 +0,0 @@ @@ -1,9 +0,0 @@
1 -class ProjectObserver < ActiveRecord::Observer  
2 - def after_save(project)  
3 - project.update_repository  
4 - end  
5 -  
6 - def after_destroy(project)  
7 - project.destroy_repository  
8 - end  
9 -end  
app/models/user_observer.rb
@@ -1,5 +0,0 @@ @@ -1,5 +0,0 @@
1 -class UserObserver < ActiveRecord::Observer  
2 - def after_create(user)  
3 - Notify.new_user_email(user.id, user.password).deliver  
4 - end  
5 -end  
app/observers/activity_observer.rb 0 → 100644
@@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
  1 +class ActivityObserver < ActiveRecord::Observer
  2 + observe :issue, :merge_request
  3 +
  4 + def after_create(record)
  5 + Event.create(
  6 + :project => record.project,
  7 + :target_id => record.id,
  8 + :target_type => record.class.name,
  9 + :action => Event.determine_action(record),
  10 + :author_id => record.author_id
  11 + )
  12 + end
  13 +
  14 + def after_save(record)
  15 + if record.changed.include?("closed")
  16 + Event.create(
  17 + :project => record.project,
  18 + :target_id => record.id,
  19 + :target_type => record.class.name,
  20 + :action => (record.closed ? Event::Closed : Event::Reopened),
  21 + :author_id => record.author_id_of_changes
  22 + )
  23 + end
  24 + end
  25 +end
app/observers/issue_observer.rb 0 → 100644
@@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
  1 +class IssueObserver < ActiveRecord::Observer
  2 + cattr_accessor :current_user
  3 +
  4 + def after_create(issue)
  5 + Notify.new_issue_email(issue.id).deliver if issue.assignee != current_user
  6 + end
  7 +
  8 + def after_update(issue)
  9 + send_reassigned_email(issue) if issue.is_being_reassigned?
  10 + Note.create_status_change_note(issue, current_user, 'closed') if issue.is_being_closed?
  11 + Note.create_status_change_note(issue, current_user, 'reopened') if issue.is_being_reopened?
  12 + end
  13 +
  14 + protected
  15 +
  16 + def send_reassigned_email(issue)
  17 + recipient_ids = [issue.assignee_id, issue.assignee_id_was].keep_if {|id| id != current_user.id }
  18 +
  19 + recipient_ids.each do |recipient_id|
  20 + Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver
  21 + end
  22 + end
  23 +end
app/observers/key_observer.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class KeyObserver < ActiveRecord::Observer
  2 + def after_save(key)
  3 + key.update_repository
  4 + end
  5 +
  6 + def after_destroy(key)
  7 + key.repository_delete_key
  8 + end
  9 +end
app/observers/mailer_observer.rb 0 → 100644
@@ -0,0 +1,79 @@ @@ -0,0 +1,79 @@
  1 +class MailerObserver < ActiveRecord::Observer
  2 + observe :note, :merge_request
  3 + cattr_accessor :current_user
  4 +
  5 + def after_create(model)
  6 + new_note(model) if model.kind_of?(Note)
  7 + new_merge_request(model) if model.kind_of?(MergeRequest)
  8 + end
  9 +
  10 + def after_update(model)
  11 + changed_merge_request(model) if model.kind_of?(MergeRequest)
  12 + end
  13 +
  14 + protected
  15 +
  16 + def new_note(note)
  17 + if note.notify
  18 + # Notify whole team except author of note
  19 + notify_note(note)
  20 + elsif note.notify_author
  21 + # Notify only author of resource
  22 + Notify.note_commit_email(note.commit_author.id, note.id).deliver
  23 + else
  24 + # Otherwise ignore it
  25 + nil
  26 + end
  27 + end
  28 +
  29 + def notify_note note
  30 + # reject author of note from mail list
  31 + users = note.project.users.reject { |u| u.id == current_user.id }
  32 +
  33 + users.each do |u|
  34 + case note.noteable_type
  35 + when "Commit"; Notify.note_commit_email(u.id, note.id).deliver
  36 + when "Issue"; Notify.note_issue_email(u.id, note.id).deliver
  37 + when "MergeRequest"; Notify.note_merge_request_email(u.id, note.id).deliver
  38 + when "Snippet"; true
  39 + else
  40 + Notify.note_wall_email(u.id, note.id).deliver
  41 + end
  42 + end
  43 + end
  44 +
  45 + def new_merge_request(merge_request)
  46 + if merge_request.assignee != current_user
  47 + Notify.new_merge_request_email(merge_request.id).deliver
  48 + end
  49 + end
  50 +
  51 + def changed_merge_request(merge_request)
  52 + status_notify_and_comment merge_request, :reassigned_merge_request_email
  53 + end
  54 +
  55 + # This method used for Issues & Merge Requests
  56 + #
  57 + # It create a comment for Issue or MR if someone close/reopen.
  58 + # It also notify via email if assignee was changed
  59 + #
  60 + def status_notify_and_comment target, mail_method
  61 + # If assigne changed - notify to recipients
  62 + if target.assignee_id_changed?
  63 + recipients_ids = target.assignee_id_was, target.assignee_id
  64 + recipients_ids.delete current_user.id
  65 +
  66 + recipients_ids.each do |recipient_id|
  67 + Notify.send(mail_method, recipient_id, target.id, target.assignee_id_was).deliver
  68 + end
  69 + end
  70 +
  71 + # Create comment about status changed
  72 + if target.closed_changed?
  73 + note = Note.new(:noteable => target, :project => target.project)
  74 + note.author = current_user
  75 + note.note = "_Status changed to #{target.closed ? 'closed' : 'reopened'}_"
  76 + note.save()
  77 + end
  78 + end
  79 +end
app/observers/project_observer.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class ProjectObserver < ActiveRecord::Observer
  2 + def after_save(project)
  3 + project.update_repository
  4 + end
  5 +
  6 + def after_destroy(project)
  7 + project.destroy_repository
  8 + end
  9 +end
app/observers/user_observer.rb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +class UserObserver < ActiveRecord::Observer
  2 + def after_create(user)
  3 + Notify.new_user_email(user.id, user.password).deliver
  4 + end
  5 +end