OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
extract journal notification setting to separate class
ulferts67ade5002d54
diff --git a/app/models/journal/notification_configuration.rb b/app/models/journal/notification_configuration.rbnew file mode 100644index 00000000000..3a5946bc918--- /dev/null+++ b/app/models/journal/notification_configuration.rb@@ -0,0 +1,61 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# 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.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class Journal::NotificationConfiguration+ class << self+ attr_reader :active++ def reset+ self.active = true+ end++ def with(send_notifications, &block)+ old_value = active++ self.active = send_notifications++ block.call+ ensure+ self.active = old_value+ end++ def with_reset(&block)+ block.call+ ensure+ reset+ end++ protected++ attr_writer :active+ end++ self.active = true+enddiff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rbindex c0316a0dfd6..6c1e5340bc9 100644--- a/app/models/journal_manager.rb+++ b/app/models/journal_manager.rb@@ -29,8 +29,6 @@class JournalManagerclass << self- attr_accessor :send_notification-def changes_on_association(current, predecessor, association, key, value)merged_journals = merge_reference_journals_by_id(current, predecessor, key.to_s, value.to_s)@@ -41,10 +39,6 @@ class JournalManagerto_changes_format(changes, association.to_s)end- def reset_notification- @send_notification = true- end-privatedef merge_reference_journals_by_id(new_journals, old_journals, id_key, value)@@ -152,8 +146,6 @@ class JournalManagerendend- self.send_notification = true-def self.journalized?(obj)not obj.nil? and obj.respond_to? :journalsend@@ -349,16 +341,4 @@ class JournalManagerh[e[0]] = (e[1].is_a?(String) ? e[1].gsub(/\r\n/, "\n") : e[1])endend-- def self.with_send_notifications(send_notifications, &block)- old_value = send_notification-- self.send_notification = send_notifications-- result = block.call- ensure- self.send_notification = old_value-- result- endenddiff --git a/app/services/add_work_package_note_service.rb b/app/services/add_work_package_note_service.rbindex ce99f7fef77..23cb7463dee 100644--- a/app/services/add_work_package_note_service.rb+++ b/app/services/add_work_package_note_service.rb@@ -42,7 +42,7 @@ class AddWorkPackageNoteServiceenddef call(notes, send_notifications: true)- JournalManager.with_send_notifications send_notifications do+ Journal::NotificationConfiguration.with send_notifications dowork_package.add_journal(user, notes)success, errors = validate_and_yield(work_package, user) dodiff --git a/app/services/shared/service_context.rb b/app/services/shared/service_context.rbindex e7d5b367566..fbcff84b4b7 100644--- a/app/services/shared/service_context.rb+++ b/app/services/shared/service_context.rb@@ -51,7 +51,7 @@ module SharedActiveRecord::Base.transaction doUser.execute_as user do- JournalManager.with_send_notifications(send_notifications) do+ Journal::NotificationConfiguration.with(send_notifications) doresult = yieldif result.failure?diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rbindex c13d39c908f..c1726117877 100644--- a/app/services/work_packages/update_ancestors_service.rb+++ b/app/services/work_packages/update_ancestors_service.rb@@ -44,7 +44,7 @@ class WorkPackages::UpdateAncestorsServiceset_journal_note(modified)# Do not send notification for parent updates- success = JournalManager.with_send_notifications(false) do+ success = Journal::NotificationConfiguration.with(false) domodified.all? { |wp| wp.save(validate: false) }enddiff --git a/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/save_hooks.rb b/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/save_hooks.rbindex fe5e6eda407..03bd529099c 100644--- a/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/save_hooks.rb+++ b/lib/plugins/acts_as_journalized/lib/redmine/acts/journalized/save_hooks.rb@@ -69,7 +69,7 @@ module Redmine::Acts::Journalizedenddef save_journals- with_ensured_journal_attributes do+ with_journal_attributes_and_notification_reset doadd_journal = journals.empty? || JournalManager.changed?(self) || !@journal_notes.empty?if add_journal@@ -77,10 +77,24 @@ module Redmine::Acts::JournalizedOpenProject::Notifications.send('journal_created',journal: journal,- send_notification: JournalManager.send_notification)+ send_notification: Journal::NotificationConfiguration.active)- # Need to clear the notification setting after each usage otherwise it might be cached- JournalManager.reset_notification+ end+ end+ end++ def add_journal(user = User.current, notes = '')+ @journal_user ||= user+ @journal_notes ||= notes+ end++ private++ def with_journal_attributes_and_notification_reset+ with_ensured_journal_attributes do+ # Need to clear the notification setting after each usage otherwise it might be cached+ Journal::NotificationConfiguration.with_reset do+ yieldendendend@@ -95,11 +109,6 @@ module Redmine::Acts::Journalized@journal_notes = nilend- def add_journal(user = User.current, notes = '')- @journal_user ||= user- @journal_notes ||= notes- end-module ClassMethodsendenddiff --git a/spec/models/journal/notification_configuration_spec.rb b/spec/models/journal/notification_configuration_spec.rbnew file mode 100644index 00000000000..d34a952aee5--- /dev/null+++ b/spec/models/journal/notification_configuration_spec.rb@@ -0,0 +1,68 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# 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.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++describe Journal::NotificationConfiguration, type: :model do+ describe '.with' do+ let!(:send_notification_before) { described_class.active }+ let(:proc_called_counter) { OpenStruct.new called: false, send_notifications: !send_notification_before }+ let(:proc) { Proc.new { proc_called_counter.called = true } }++ it 'executes the block' do+ described_class.with !send_notification_before, &proc++ expect(proc_called_counter.called)+ .to be_truthy+ end++ it 'uses the provided send_notifications value within the proc' do+ described_class.with !send_notification_before, &proc++ expect(proc_called_counter.send_notifications)+ .to eql !send_notification_before+ end++ it 'resets the send_notifications to the value before' do+ described_class.with !send_notification_before, &proc++ expect(described_class.active)+ .to eql send_notification_before+ end++ context 'with an exception being raised within the block' do+ it 'raises the exception but always resets the notification value' do+ expect { described_class.with(!send_notification_before) { raise ArgumentError } }+ .to raise_error ArgumentError++ expect(described_class.active)+ .to eql send_notification_before+ end+ end+ end+enddiff --git a/spec/models/work_package/work_package_action_mailer_spec.rb b/spec/models/work_package/work_package_action_mailer_spec.rbindex 18ca9307205..ac613f9a719 100644--- a/spec/models/work_package/work_package_action_mailer_spec.rb+++ b/spec/models/work_package/work_package_action_mailer_spec.rb@@ -47,7 +47,7 @@ describe WorkPackage, type: :model doallow(work_package).to receive(:recipients).and_return([user_1])allow(work_package).to receive(:watcher_recipients).and_return([user_2])- JournalManager.with_send_notifications true do+ Journal::NotificationConfiguration.with true dowork_package.saveendend@@ -79,7 +79,7 @@ describe WorkPackage, type: :model dobefore doActionMailer::Base.deliveries.clear # clear mails sent due to prior WP creation- JournalManager.with_send_notifications false do+ Journal::NotificationConfiguration.with false dowork_package.save!endend