Skip to content

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

extract journal notification setting to separate class

ulferts67ade5002d54
diff --git a/app/models/journal/notification_configuration.rb b/app/models/journal/notification_configuration.rb
new file mode 100644
index 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
+end
diff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rb
index c0316a0dfd6..6c1e5340bc9 100644
--- a/app/models/journal_manager.rb
+++ b/app/models/journal_manager.rb
@@ -29,8 +29,6 @@
class JournalManager
class << 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 JournalManager
to_changes_format(changes, association.to_s)
end
- def reset_notification
- @send_notification = true
- end
-
private
def merge_reference_journals_by_id(new_journals, old_journals, id_key, value)
@@ -152,8 +146,6 @@ class JournalManager
end
end
- self.send_notification = true
-
def self.journalized?(obj)
not obj.nil? and obj.respond_to? :journals
end
@@ -349,16 +341,4 @@ class JournalManager
h[e[0]] = (e[1].is_a?(String) ? e[1].gsub(/\r\n/, "\n") : e[1])
end
end
-
- 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
- end
end
diff --git a/app/services/add_work_package_note_service.rb b/app/services/add_work_package_note_service.rb
index 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 AddWorkPackageNoteService
end
def call(notes, send_notifications: true)
- JournalManager.with_send_notifications send_notifications do
+ Journal::NotificationConfiguration.with send_notifications do
work_package.add_journal(user, notes)
success, errors = validate_and_yield(work_package, user) do
diff --git a/app/services/shared/service_context.rb b/app/services/shared/service_context.rb
index e7d5b367566..fbcff84b4b7 100644
--- a/app/services/shared/service_context.rb
+++ b/app/services/shared/service_context.rb
@@ -51,7 +51,7 @@ module Shared
ActiveRecord::Base.transaction do
User.execute_as user do
- JournalManager.with_send_notifications(send_notifications) do
+ Journal::NotificationConfiguration.with(send_notifications) do
result = yield
if result.failure?
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rb
index 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::UpdateAncestorsService
set_journal_note(modified)
# Do not send notification for parent updates
- success = JournalManager.with_send_notifications(false) do
+ success = Journal::NotificationConfiguration.with(false) do
modified.all? { |wp| wp.save(validate: false) }
end
diff --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.rb
index 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::Journalized
end
def save_journals
- with_ensured_journal_attributes do
+ with_journal_attributes_and_notification_reset do
add_journal = journals.empty? || JournalManager.changed?(self) || !@journal_notes.empty?
if add_journal
@@ -77,10 +77,24 @@ module Redmine::Acts::Journalized
OpenProject::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
+ yield
end
end
end
@@ -95,11 +109,6 @@ module Redmine::Acts::Journalized
@journal_notes = nil
end
- def add_journal(user = User.current, notes = '')
- @journal_user ||= user
- @journal_notes ||= notes
- end
-
module ClassMethods
end
end
diff --git a/spec/models/journal/notification_configuration_spec.rb b/spec/models/journal/notification_configuration_spec.rb
new file mode 100644
index 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
+end
diff --git a/spec/models/work_package/work_package_action_mailer_spec.rb b/spec/models/work_package/work_package_action_mailer_spec.rb
index 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 do
allow(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 do
work_package.save
end
end
@@ -79,7 +79,7 @@ describe WorkPackage, type: :model do
before do
ActionMailer::Base.deliveries.clear # clear mails sent due to prior WP creation
- JournalManager.with_send_notifications false do
+ Journal::NotificationConfiguration.with false do
work_package.save!
end
end