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

let first notification setting dominate child setting

ulfertsa6689a90061b
diff --git a/app/models/journal/notification_configuration.rb b/app/models/journal/notification_configuration.rb
index 6d36b33fb9a..ea9d6d80279 100644
--- a/app/models/journal/notification_configuration.rb
+++ b/app/models/journal/notification_configuration.rb
@@ -30,24 +30,51 @@
class Journal::NotificationConfiguration
class << self
+ # Allows controlling whether notifications are sent out for created journals.
+ # After the block is executed, the setting is returned to its original state which is true by default.
+ # In case the method is called multiple times within itself, the first setting prevails.
+ # This allows to control the setting globally without having to pass the setting down the call stack in
+ # order to ensure all subsequent code follows the provided setting.
def with(send_notifications, &block)
+ if already_set
+ log_warning(send_notifications)
+ yield
+ else
+ with_first(send_notifications, &block)
+ end
+ end
+
+ def active?
+ active
+ end
+
+ protected
+
+ def with_first(send_notifications)
old_value = active
+ self.already_set = true
self.active = send_notifications
- block.call
+ yield
ensure
self.active = old_value
+ self.already_set = false
end
- def active?
- active
- end
+ def log_warning(send_notifications)
+ return if active == send_notifications
- protected
+ message = <<~MSG
+ Ignoring setting journal notifications to '#{send_notifications}' as a parent block already send it to #{active}"
+ MSG
+ Rails.logger.debug message
+ end
- attr_accessor :active
+ attr_accessor :active,
+ :already_set
end
self.active = true
+ self.already_set = false
end
diff --git a/spec/models/journal/notification_configuration_spec.rb b/spec/models/journal/notification_configuration_spec.rb
index 6a47ebf47db..73282660a94 100644
--- a/spec/models/journal/notification_configuration_spec.rb
+++ b/spec/models/journal/notification_configuration_spec.rb
@@ -31,8 +31,13 @@ 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 } }
+ let!(:proc_called_counter) { OpenStruct.new called: false, send_notifications: send_notification_before }
+ let(:proc) do
+ Proc.new do
+ proc_called_counter.called = true
+ proc_called_counter.send_notifications = described_class.active?
+ end
+ end
it 'executes the block' do
described_class.with !send_notification_before, &proc
@@ -55,6 +60,15 @@ describe Journal::NotificationConfiguration, type: :model do
.to eql send_notification_before
end
+ it 'lets the first block dominate further block calls' do
+ described_class.with !send_notification_before do
+ described_class.with send_notification_before, &proc
+ end
+
+ expect(proc_called_counter.send_notifications)
+ .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 } }