From 205eda8b33d69defecedf2d246858a46d3d1d7c0 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 14 Jul 2013 14:26:27 +0000 Subject: [PATCH] Use AR callbacks instead of observers (removed in Rails4) for notifications. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@12021 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/comment.rb | 10 ++++++++++ app/models/comment_observer.rb | 24 ------------------------ app/models/document.rb | 10 ++++++++++ app/models/document_observer.rb | 22 ---------------------- app/models/issue.rb | 7 +++++++ app/models/issue_observer.rb | 22 ---------------------- app/models/journal.rb | 11 +++++++++++ app/models/journal_observer.rb | 29 ----------------------------- app/models/message.rb | 7 +++++++ app/models/message_observer.rb | 22 ---------------------- app/models/news.rb | 7 +++++++ app/models/news_observer.rb | 22 ---------------------- app/models/wiki_content.rb | 17 +++++++++++++++++ app/models/wiki_content_observer.rb | 28 ---------------------------- config/application.rb | 3 --- test/unit/wiki_content_test.rb | 4 +++- 16 files changed, 72 insertions(+), 173 deletions(-) delete mode 100644 app/models/comment_observer.rb delete mode 100644 app/models/document_observer.rb delete mode 100644 app/models/issue_observer.rb delete mode 100644 app/models/journal_observer.rb delete mode 100644 app/models/message_observer.rb delete mode 100644 app/models/news_observer.rb delete mode 100644 app/models/wiki_content_observer.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 577afe942..ac60e497c 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -22,5 +22,15 @@ class Comment < ActiveRecord::Base validates_presence_of :commented, :author, :comments + after_create :send_notification + safe_attributes 'comments' + + private + + def send_notification + if commented.is_a?(News) && Setting.notified_events.include?('news_comment_added') + Mailer.news_comment_added(self).deliver + end + end end diff --git a/app/models/comment_observer.rb b/app/models/comment_observer.rb deleted file mode 100644 index e7c12c4b6..000000000 --- a/app/models/comment_observer.rb +++ /dev/null @@ -1,24 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class CommentObserver < ActiveRecord::Observer - def after_create(comment) - if comment.commented.is_a?(News) && Setting.notified_events.include?('news_comment_added') - Mailer.news_comment_added(comment).deliver - end - end -end diff --git a/app/models/document.rb b/app/models/document.rb index 0c2ce1736..ddf2a1f79 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -30,6 +30,8 @@ class Document < ActiveRecord::Base validates_presence_of :project, :title, :category validates_length_of :title, :maximum => 60 + after_create :send_notification + scope :visible, lambda {|*args| includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args)) } @@ -54,4 +56,12 @@ class Document < ActiveRecord::Base end @updated_on end + + private + + def send_notification + if Setting.notified_events.include?('document_added') + Mailer.document_added(self).deliver + end + end end diff --git a/app/models/document_observer.rb b/app/models/document_observer.rb deleted file mode 100644 index 4a61f1184..000000000 --- a/app/models/document_observer.rb +++ /dev/null @@ -1,22 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class DocumentObserver < ActiveRecord::Observer - def after_create(document) - Mailer.document_added(document).deliver if Setting.notified_events.include?('document_added') - end -end diff --git a/app/models/issue.rb b/app/models/issue.rb index a089f4067..5f54ce3bd 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -98,6 +98,7 @@ class Issue < ActiveRecord::Base # Should be after_create but would be called before previous after_save callbacks after_save :after_create_from_copy after_destroy :update_parent_attributes + after_create :send_notification # Returns a SQL conditions string used to find all issues visible by the specified user def self.visible_condition(user, options={}) @@ -1516,6 +1517,12 @@ class Issue < ActiveRecord::Base end end + def send_notification + if Setting.notified_events.include?('issue_added') + Mailer.deliver_issue_add(self) + end + end + # Query generator for selecting groups of issue counts for a project # based on specific criteria # diff --git a/app/models/issue_observer.rb b/app/models/issue_observer.rb deleted file mode 100644 index 58b636031..000000000 --- a/app/models/issue_observer.rb +++ /dev/null @@ -1,22 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class IssueObserver < ActiveRecord::Observer - def after_create(issue) - Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added') - end -end diff --git a/app/models/journal.rb b/app/models/journal.rb index 47ae12380..2e3aa7f9f 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -39,6 +39,7 @@ class Journal < ActiveRecord::Base " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"} before_create :split_private_notes + after_create :send_notification scope :visible, lambda {|*args| user = args.shift || User.current @@ -165,4 +166,14 @@ class Journal < ActiveRecord::Base end true end + + def send_notification + if notify? && (Setting.notified_events.include?('issue_updated') || + (Setting.notified_events.include?('issue_note_added') && notes.present?) || + (Setting.notified_events.include?('issue_status_updated') && new_status.present?) || + (Setting.notified_events.include?('issue_priority_updated') && new_value_for('priority_id').present?) + ) + Mailer.deliver_issue_edit(self) + end + end end diff --git a/app/models/journal_observer.rb b/app/models/journal_observer.rb deleted file mode 100644 index ff4b89837..000000000 --- a/app/models/journal_observer.rb +++ /dev/null @@ -1,29 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class JournalObserver < ActiveRecord::Observer - def after_create(journal) - if journal.notify? && - (Setting.notified_events.include?('issue_updated') || - (Setting.notified_events.include?('issue_note_added') && journal.notes.present?) || - (Setting.notified_events.include?('issue_status_updated') && journal.new_status.present?) || - (Setting.notified_events.include?('issue_priority_updated') && journal.new_value_for('priority_id').present?) - ) - Mailer.deliver_issue_edit(journal) - end - end -end diff --git a/app/models/message.rb b/app/models/message.rb index 767223748..4335fefa9 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -45,6 +45,7 @@ class Message < ActiveRecord::Base after_create :add_author_as_watcher, :reset_counters! after_update :update_messages_board after_destroy :reset_counters! + after_create :send_notification scope :visible, lambda {|*args| includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args)) @@ -105,4 +106,10 @@ class Message < ActiveRecord::Base def add_author_as_watcher Watcher.create(:watchable => self.root, :user => author) end + + def send_notification + if Setting.notified_events.include?('message_posted') + Mailer.message_posted(self).deliver + end + end end diff --git a/app/models/message_observer.rb b/app/models/message_observer.rb deleted file mode 100644 index 9ef52b6b4..000000000 --- a/app/models/message_observer.rb +++ /dev/null @@ -1,22 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class MessageObserver < ActiveRecord::Observer - def after_create(message) - Mailer.message_posted(message).deliver if Setting.notified_events.include?('message_posted') - end -end diff --git a/app/models/news.rb b/app/models/news.rb index c8a4112cc..2186beaed 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -33,6 +33,7 @@ class News < ActiveRecord::Base acts_as_watchable after_create :add_author_as_watcher + after_create :send_notification scope :visible, lambda {|*args| includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args)) @@ -63,4 +64,10 @@ class News < ActiveRecord::Base def add_author_as_watcher Watcher.create(:watchable => self, :user => author) end + + def send_notification + if Setting.notified_events.include?('news_added') + Mailer.news_added(self).deliver + end + end end diff --git a/app/models/news_observer.rb b/app/models/news_observer.rb deleted file mode 100644 index 8ba3894f4..000000000 --- a/app/models/news_observer.rb +++ /dev/null @@ -1,22 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class NewsObserver < ActiveRecord::Observer - def after_create(news) - Mailer.news_added(news).deliver if Setting.notified_events.include?('news_added') - end -end diff --git a/app/models/wiki_content.rb b/app/models/wiki_content.rb index a9ed005bc..20295e9d5 100644 --- a/app/models/wiki_content.rb +++ b/app/models/wiki_content.rb @@ -26,6 +26,8 @@ class WikiContent < ActiveRecord::Base acts_as_versioned + after_save :send_notification + def visible?(user=User.current) page.visible?(user) end @@ -145,4 +147,19 @@ class WikiContent < ActiveRecord::Base end end end + + private + + def send_notification + # new_record? returns false in after_save callbacks + if id_changed? + if Setting.notified_events.include?('wiki_content_added') + Mailer.wiki_content_added(self).deliver + end + elsif text_changed? + if Setting.notified_events.include?('wiki_content_updated') + Mailer.wiki_content_updated(self).deliver + end + end + end end diff --git a/app/models/wiki_content_observer.rb b/app/models/wiki_content_observer.rb deleted file mode 100644 index 6219f407b..000000000 --- a/app/models/wiki_content_observer.rb +++ /dev/null @@ -1,28 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class WikiContentObserver < ActiveRecord::Observer - def after_create(wiki_content) - Mailer.wiki_content_added(wiki_content).deliver if Setting.notified_events.include?('wiki_content_added') - end - - def after_update(wiki_content) - if wiki_content.text_changed? - Mailer.wiki_content_updated(wiki_content).deliver if Setting.notified_events.include?('wiki_content_updated') - end - end -end diff --git a/config/application.rb b/config/application.rb index e434bd2a3..e18447fa4 100644 --- a/config/application.rb +++ b/config/application.rb @@ -22,9 +22,6 @@ module RedmineApp # :all can be used as a placeholder for all plugins not explicitly named. # config.plugins = [ :exception_notification, :ssl_requirement, :all ] - # Activate observers that should always be running. - config.active_record.observers = :message_observer, :issue_observer, :journal_observer, :news_observer, :document_observer, :wiki_content_observer, :comment_observer - config.active_record.store_full_sti_class = true config.active_record.default_timezone = :local diff --git a/test/unit/wiki_content_test.rb b/test/unit/wiki_content_test.rb index 5956da963..e06b416f9 100644 --- a/test/unit/wiki_content_test.rb +++ b/test/unit/wiki_content_test.rb @@ -48,11 +48,12 @@ class WikiContentTest < ActiveSupport::TestCase page = WikiPage.new(:wiki => @wiki, :title => "A new page") page.content = WikiContent.new(:text => "Content text", :author => User.find(1), :comments => "My comment") - with_settings :notified_events => %w(wiki_content_added) do + with_settings :default_language => 'en', :notified_events => %w(wiki_content_added) do assert page.save end assert_equal 1, ActionMailer::Base.deliveries.size + assert_include 'wiki page has been added', mail_body(ActionMailer::Base.deliveries.last) end def test_update_should_be_versioned @@ -99,6 +100,7 @@ class WikiContentTest < ActiveSupport::TestCase end assert_equal 1, ActionMailer::Base.deliveries.size + assert_include 'wiki page has been updated', mail_body(ActionMailer::Base.deliveries.last) end def test_fetch_history -- 2.39.5