##// END OF EJS Templates
Added observers to watch model objects for mail delivery instead of calling Mailer....
Eric Davis -
r2548:b4be8849c0de
parent child
Show More
@@ -0,0 +1,22
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class DocumentObserver < ActiveRecord::Observer
19 def after_create(document)
20 Mailer.deliver_document_added(document) if Setting.notified_events.include?('document_added')
21 end
22 end
@@ -0,0 +1,22
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class IssueObserver < ActiveRecord::Observer
19 def after_create(issue)
20 Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added')
21 end
22 end
@@ -0,0 +1,22
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class JournalObserver < ActiveRecord::Observer
19 def after_create(journal)
20 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
21 end
22 end
@@ -0,0 +1,22
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class NewsObserver < ActiveRecord::Observer
19 def after_create(news)
20 Mailer.deliver_news_added(news) if Setting.notified_events.include?('news_added')
21 end
22 end
@@ -48,7 +48,6 class DocumentsController < ApplicationController
48 if request.post? and @document.save
48 if request.post? and @document.save
49 attach_files(@document, params[:attachments])
49 attach_files(@document, params[:attachments])
50 flash[:notice] = l(:notice_successful_create)
50 flash[:notice] = l(:notice_successful_create)
51 Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
52 redirect_to :action => 'index', :project_id => @project
51 redirect_to :action => 'index', :project_id => @project
53 end
52 end
54 end
53 end
@@ -146,7 +146,6 class IssuesController < ApplicationController
146 if @issue.save
146 if @issue.save
147 attach_files(@issue, params[:attachments])
147 attach_files(@issue, params[:attachments])
148 flash[:notice] = l(:notice_successful_create)
148 flash[:notice] = l(:notice_successful_create)
149 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
150 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
149 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
151 redirect_to(params[:continue] ? { :action => 'new', :tracker_id => @issue.tracker } :
150 redirect_to(params[:continue] ? { :action => 'new', :tracker_id => @issue.tracker } :
152 { :action => 'show', :id => @issue })
151 { :action => 'show', :id => @issue })
@@ -193,7 +192,6 class IssuesController < ApplicationController
193 if !journal.new_record?
192 if !journal.new_record?
194 # Only send notification if something was actually changed
193 # Only send notification if something was actually changed
195 flash[:notice] = l(:notice_successful_update)
194 flash[:notice] = l(:notice_successful_update)
196 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
197 end
195 end
198 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal})
196 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal})
199 redirect_to(params[:back_to] || {:action => 'show', :id => @issue})
197 redirect_to(params[:back_to] || {:action => 'show', :id => @issue})
@@ -247,10 +245,7 class IssuesController < ApplicationController
247 issue.custom_field_values = custom_field_values if custom_field_values && !custom_field_values.empty?
245 issue.custom_field_values = custom_field_values if custom_field_values && !custom_field_values.empty?
248 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
246 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
249 # Don't save any change to the issue if the user is not authorized to apply the requested status
247 # Don't save any change to the issue if the user is not authorized to apply the requested status
250 if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
248 unless (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
251 # Send notification for each issue (if changed)
252 Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated')
253 else
254 # Keep unsaved issue ids to display them in flash error
249 # Keep unsaved issue ids to display them in flash error
255 unsaved_issue_ids << issue.id
250 unsaved_issue_ids << issue.id
256 end
251 end
@@ -45,7 +45,6 class NewsController < ApplicationController
45 @news.attributes = params[:news]
45 @news.attributes = params[:news]
46 if @news.save
46 if @news.save
47 flash[:notice] = l(:notice_successful_create)
47 flash[:notice] = l(:notice_successful_create)
48 Mailer.deliver_news_added(@news) if Setting.notified_events.include?('news_added')
49 redirect_to :controller => 'news', :action => 'index', :project_id => @project
48 redirect_to :controller => 'news', :action => 'index', :project_id => @project
50 end
49 end
51 end
50 end
@@ -113,7 +113,6 class Changeset < ActiveRecord::Base
113 issue.status = fix_status
113 issue.status = fix_status
114 issue.done_ratio = done_ratio if done_ratio
114 issue.done_ratio = done_ratio if done_ratio
115 issue.save
115 issue.save
116 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
117 end
116 end
118 end
117 end
119 referenced_issues += target_issues
118 referenced_issues += target_issues
@@ -110,13 +110,11 class MailHandler < ActionMailer::Base
110 end
110 end
111 h
111 h
112 end
112 end
113 # add To and Cc as watchers before saving so the watchers can reply to Redmine
114 add_watchers(issue)
113 issue.save!
115 issue.save!
114 add_attachments(issue)
116 add_attachments(issue)
115 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
117 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
116 # add To and Cc as watchers
117 add_watchers(issue)
118 # send notification after adding watchers so that they can reply to Redmine
119 Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added')
120 issue
118 issue
121 end
119 end
122
120
@@ -148,7 +146,6 class MailHandler < ActionMailer::Base
148 end
146 end
149 issue.save!
147 issue.save!
150 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
148 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
151 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
152 journal
149 journal
153 end
150 end
154
151
@@ -36,7 +36,7 Rails::Initializer.run do |config|
36
36
37 # Activate observers that should always be running
37 # Activate observers that should always be running
38 # config.active_record.observers = :cacher, :garbage_collector
38 # config.active_record.observers = :cacher, :garbage_collector
39 config.active_record.observers = :message_observer
39 config.active_record.observers = :message_observer, :issue_observer, :journal_observer, :news_observer, :document_observer
40
40
41 # Make Active Record use UTC-base instead of local time
41 # Make Active Record use UTC-base instead of local time
42 # config.active_record.default_timezone = :utc
42 # config.active_record.default_timezone = :utc
@@ -66,6 +66,8 class DocumentsControllerTest < Test::Unit::TestCase
66 end
66 end
67
67
68 def test_new_with_one_attachment
68 def test_new_with_one_attachment
69 ActionMailer::Base.deliveries.clear
70 Setting.notified_events << 'document_added'
69 @request.session[:user_id] = 2
71 @request.session[:user_id] = 2
70 set_tmp_attachments_directory
72 set_tmp_attachments_directory
71
73
@@ -82,6 +84,7 class DocumentsControllerTest < Test::Unit::TestCase
82 assert_equal Enumeration.find(2), document.category
84 assert_equal Enumeration.find(2), document.category
83 assert_equal 1, document.attachments.size
85 assert_equal 1, document.attachments.size
84 assert_equal 'testfile.txt', document.attachments.first.filename
86 assert_equal 'testfile.txt', document.attachments.first.filename
87 assert_equal 1, ActionMailer::Base.deliveries.size
85 end
88 end
86
89
87 def test_edit_routing
90 def test_edit_routing
@@ -503,6 +503,21 class IssuesControllerTest < Test::Unit::TestCase
503 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
503 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
504 end
504 end
505
505
506 def test_post_new_should_send_a_notification
507 ActionMailer::Base.deliveries.clear
508 @request.session[:user_id] = 2
509 post :new, :project_id => 1,
510 :issue => {:tracker_id => 3,
511 :subject => 'This is the test_new issue',
512 :description => 'This is the description',
513 :priority_id => 5,
514 :estimated_hours => '',
515 :custom_field_values => {'2' => 'Value for field 2'}}
516 assert_redirected_to :action => 'show'
517
518 assert_equal 1, ActionMailer::Base.deliveries.size
519 end
520
506 def test_post_should_preserve_fields_values_on_validation_failure
521 def test_post_should_preserve_fields_values_on_validation_failure
507 @request.session[:user_id] = 2
522 @request.session[:user_id] = 2
508 post :new, :project_id => 1,
523 post :new, :project_id => 1,
@@ -760,6 +775,20 class IssuesControllerTest < Test::Unit::TestCase
760 # No email should be sent
775 # No email should be sent
761 assert ActionMailer::Base.deliveries.empty?
776 assert ActionMailer::Base.deliveries.empty?
762 end
777 end
778
779 def test_post_edit_should_send_a_notification
780 @request.session[:user_id] = 2
781 ActionMailer::Base.deliveries.clear
782 issue = Issue.find(1)
783 old_subject = issue.subject
784 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
785
786 post :edit, :id => 1, :issue => {:subject => new_subject,
787 :priority_id => '6',
788 :category_id => '1' # no change
789 }
790 assert_equal 1, ActionMailer::Base.deliveries.size
791 end
763
792
764 def test_post_edit_with_invalid_spent_time
793 def test_post_edit_with_invalid_spent_time
765 @request.session[:user_id] = 2
794 @request.session[:user_id] = 2
@@ -797,6 +826,22 class IssuesControllerTest < Test::Unit::TestCase
797 assert_equal 1, journal.details.size
826 assert_equal 1, journal.details.size
798 end
827 end
799
828
829 def test_bullk_edit_should_send_a_notification
830 @request.session[:user_id] = 2
831 ActionMailer::Base.deliveries.clear
832 post(:bulk_edit,
833 {
834 :ids => [1, 2],
835 :priority_id => 7,
836 :assigned_to_id => '',
837 :custom_field_values => {'2' => ''},
838 :notes => 'Bulk editing'
839 })
840
841 assert_response 302
842 assert_equal 2, ActionMailer::Base.deliveries.size
843 end
844
800 def test_bulk_edit_custom_field
845 def test_bulk_edit_custom_field
801 @request.session[:user_id] = 2
846 @request.session[:user_id] = 2
802 # update issues priority
847 # update issues priority
@@ -112,6 +112,9 class NewsControllerTest < Test::Unit::TestCase
112 end
112 end
113
113
114 def test_post_new
114 def test_post_new
115 ActionMailer::Base.deliveries.clear
116 Setting.notified_events << 'news_added'
117
115 @request.session[:user_id] = 2
118 @request.session[:user_id] = 2
116 post :new, :project_id => 1, :news => { :title => 'NewsControllerTest',
119 post :new, :project_id => 1, :news => { :title => 'NewsControllerTest',
117 :description => 'This is the description',
120 :description => 'This is the description',
@@ -123,6 +126,7 class NewsControllerTest < Test::Unit::TestCase
123 assert_equal 'This is the description', news.description
126 assert_equal 'This is the description', news.description
124 assert_equal User.find(2), news.author
127 assert_equal User.find(2), news.author
125 assert_equal Project.find(1), news.project
128 assert_equal Project.find(1), news.project
129 assert_equal 1, ActionMailer::Base.deliveries.size
126 end
130 end
127
131
128 def test_edit_routing
132 def test_edit_routing
@@ -24,6 +24,7 class ChangesetTest < Test::Unit::TestCase
24 end
24 end
25
25
26 def test_ref_keywords_any
26 def test_ref_keywords_any
27 ActionMailer::Base.deliveries.clear
27 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
28 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
28 Setting.commit_fix_done_ratio = '90'
29 Setting.commit_fix_done_ratio = '90'
29 Setting.commit_ref_keywords = '*'
30 Setting.commit_ref_keywords = '*'
@@ -38,6 +39,7 class ChangesetTest < Test::Unit::TestCase
38 fixed = Issue.find(1)
39 fixed = Issue.find(1)
39 assert fixed.closed?
40 assert fixed.closed?
40 assert_equal 90, fixed.done_ratio
41 assert_equal 90, fixed.done_ratio
42 assert_equal 1, ActionMailer::Base.deliveries.size
41 end
43 end
42
44
43 def test_ref_keywords_any_line_start
45 def test_ref_keywords_any_line_start
@@ -25,6 +25,15 class DocumentTest < Test::Unit::TestCase
25 assert doc.save
25 assert doc.save
26 end
26 end
27
27
28 def test_create_should_send_email_notification
29 ActionMailer::Base.deliveries.clear
30 Setting.notified_events << 'document_added'
31 doc = Document.new(:project => Project.find(1), :title => 'New document', :category => Enumeration.find_by_name('User documentation'))
32
33 assert doc.save
34 assert_equal 1, ActionMailer::Base.deliveries.size
35 end
36
28 def test_create_with_default_category
37 def test_create_with_default_category
29 # Sets a default category
38 # Sets a default category
30 e = Enumeration.find_by_name('Technical documentation')
39 e = Enumeration.find_by_name('Technical documentation')
@@ -241,4 +241,12 class IssueTest < Test::Unit::TestCase
241 assert !Issue.new(:due_date => nil).overdue?
241 assert !Issue.new(:due_date => nil).overdue?
242 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
242 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
243 end
243 end
244
245 def test_create_should_send_email_notification
246 ActionMailer::Base.deliveries.clear
247 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.priorities.first, :subject => 'test_create', :estimated_hours => '1:30')
248
249 assert issue.save
250 assert_equal 1, ActionMailer::Base.deliveries.size
251 end
244 end
252 end
@@ -36,4 +36,15 class JournalTest < Test::Unit::TestCase
36 assert_kind_of IssueStatus, status
36 assert_kind_of IssueStatus, status
37 assert_equal 2, status.id
37 assert_equal 2, status.id
38 end
38 end
39
40 def test_create_should_send_email_notification
41 ActionMailer::Base.deliveries.clear
42 issue = Issue.find(:first)
43 user = User.find(:first)
44 journal = issue.init_journal(user, issue)
45
46 assert journal.save
47 assert_equal 1, ActionMailer::Base.deliveries.size
48 end
49
39 end
50 end
@@ -133,6 +133,14 class MailHandlerTest < Test::Unit::TestCase
133 Role.anonymous.add_permission!(:add_issues)
133 Role.anonymous.add_permission!(:add_issues)
134 assert_equal false, submit_email('ticket_without_from_header.eml')
134 assert_equal false, submit_email('ticket_without_from_header.eml')
135 end
135 end
136
137 def test_add_issue_should_send_email_notification
138 ActionMailer::Base.deliveries.clear
139 # This email contains: 'Project: onlinestore'
140 issue = submit_email('ticket_on_given_project.eml')
141 assert issue.is_a?(Issue)
142 assert_equal 1, ActionMailer::Base.deliveries.size
143 end
136
144
137 def test_add_issue_note
145 def test_add_issue_note
138 journal = submit_email('ticket_reply.eml')
146 journal = submit_email('ticket_reply.eml')
@@ -152,6 +160,13 class MailHandlerTest < Test::Unit::TestCase
152 assert_match /This is reply/, journal.notes
160 assert_match /This is reply/, journal.notes
153 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
161 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
154 end
162 end
163
164 def test_add_issue_note_should_send_email_notification
165 ActionMailer::Base.deliveries.clear
166 journal = submit_email('ticket_reply.eml')
167 assert journal.is_a?(Journal)
168 assert_equal 1, ActionMailer::Base.deliveries.size
169 end
155
170
156 def test_reply_to_a_message
171 def test_reply_to_a_message
157 m = submit_email('message_reply.eml')
172 m = submit_email('message_reply.eml')
@@ -20,9 +20,23 require File.dirname(__FILE__) + '/../test_helper'
20 class NewsTest < Test::Unit::TestCase
20 class NewsTest < Test::Unit::TestCase
21 fixtures :projects, :users, :roles, :members, :enabled_modules, :news
21 fixtures :projects, :users, :roles, :members, :enabled_modules, :news
22
22
23 def valid_news
24 { :title => 'Test news', :description => 'Lorem ipsum etc', :author => User.find(:first) }
25 end
26
27
23 def setup
28 def setup
24 end
29 end
25
30
31 def test_create_should_send_email_notification
32 ActionMailer::Base.deliveries.clear
33 Setting.notified_events << 'news_added'
34 news = Project.find(:first).news.new(valid_news)
35
36 assert news.save
37 assert_equal 1, ActionMailer::Base.deliveries.size
38 end
39
26 def test_should_include_news_for_projects_with_news_enabled
40 def test_should_include_news_for_projects_with_news_enabled
27 project = projects(:projects_001)
41 project = projects(:projects_001)
28 assert project.enabled_modules.any?{ |em| em.name == 'news' }
42 assert project.enabled_modules.any?{ |em| em.name == 'news' }
@@ -37,7 +51,7 class NewsTest < Test::Unit::TestCase
37 assert ! project.enabled_modules.any?{ |em| em.name == 'news' }
51 assert ! project.enabled_modules.any?{ |em| em.name == 'news' }
38
52
39 # Add a piece of news to the project
53 # Add a piece of news to the project
40 news = project.news.create(:title => 'Test news', :description => 'This should not be returned by News.latest')
54 news = project.news.create(valid_news)
41
55
42 # News.latest should not return that new piece of news
56 # News.latest should not return that new piece of news
43 assert News.latest.include?(news) == false
57 assert News.latest.include?(news) == false
@@ -50,14 +64,14 class NewsTest < Test::Unit::TestCase
50
64
51 def test_should_limit_the_amount_of_returned_news
65 def test_should_limit_the_amount_of_returned_news
52 # Make sure we have a bunch of news stories
66 # Make sure we have a bunch of news stories
53 10.times { projects(:projects_001).news.create(:title => 'Test news', :description => 'Lorem ipsum etc') }
67 10.times { projects(:projects_001).news.create(valid_news) }
54 assert_equal 2, News.latest(users(:users_002), 2).size
68 assert_equal 2, News.latest(users(:users_002), 2).size
55 assert_equal 6, News.latest(users(:users_002), 6).size
69 assert_equal 6, News.latest(users(:users_002), 6).size
56 end
70 end
57
71
58 def test_should_return_5_news_stories_by_default
72 def test_should_return_5_news_stories_by_default
59 # Make sure we have a bunch of news stories
73 # Make sure we have a bunch of news stories
60 10.times { projects(:projects_001).news.create(:title => 'Test news', :description => 'Lorem ipsum etc') }
74 10.times { projects(:projects_001).news.create(valid_news) }
61 assert_equal 5, News.latest(users(:users_004)).size
75 assert_equal 5, News.latest(users(:users_004)).size
62 end
76 end
63 end
77 end
General Comments 0
You need to be logged in to leave comments. Login now