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

Do not change send_notifications value if passing nil

Christophe Bliard934cc626eda3
diff --git a/app/models/journal/notification_configuration.rb b/app/models/journal/notification_configuration.rb
index 806d209ab54..48488591f6e 100644
--- a/app/models/journal/notification_configuration.rb
+++ b/app/models/journal/notification_configuration.rb
@@ -28,13 +28,17 @@
class Journal::NotificationConfiguration
class << self
+ DEFAULT = true
+
# 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, &)
- if already_set?
+ if send_notifications.nil?
+ yield
+ elsif already_set?
log_warning(send_notifications)
yield
else
@@ -64,13 +68,13 @@ class Journal::NotificationConfiguration
return if active? == send_notifications
message = <<~MSG
- Ignoring setting journal notifications to '#{send_notifications}' as a parent block already set it to #{active?}"
+ Ignoring setting journal notifications to '#{send_notifications}' as a parent block already set it to #{active?}
MSG
- Rails.logger.debug message
+ Rails.logger.debug message.strip
end
def active
- @active ||= Concurrent::ThreadLocalVar.new(true)
+ @active ||= Concurrent::ThreadLocalVar.new(DEFAULT)
end
def already_set
diff --git a/spec/models/journal/notification_configuration_spec.rb b/spec/models/journal/notification_configuration_spec.rb
index b62a30bd45a..bc24294841d 100644
--- a/spec/models/journal/notification_configuration_spec.rb
+++ b/spec/models/journal/notification_configuration_spec.rb
@@ -60,13 +60,90 @@ describe Journal::NotificationConfiguration 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
+ context 'when called with nil' do
+ it 'defaults to true for send_notifications' do
+ described_class.with nil, &proc
+ expect(described_class.active?)
+ .to be(true)
end
+ end
- expect(proc_called_counter.send_notifications)
- .to eql !send_notification_before
+ context 'with nested calls' do
+ before do
+ allow(Rails.logger).to receive(:debug)
+
+ described_class.with outer_call_value do
+ described_class.with inner_call_value, &proc
+ end
+ end
+
+ context 'when send_notifications value of inner call is different from outer call' do
+ let(:outer_call_value) { !send_notification_before }
+ let(:inner_call_value) { send_notification_before }
+
+ it 'executes the block' do
+ expect(proc_called_counter.called)
+ .to be_truthy
+ end
+
+ it 'lets the outer block call dominate further block calls' do
+ expect(proc_called_counter.send_notifications)
+ .to eq(outer_call_value)
+ end
+
+ it 'logs a debug message' do
+ expect(Rails.logger).to have_received(:debug)
+ .with("Ignoring setting journal notifications to '#{inner_call_value}' " \
+ "as a parent block already set it to #{outer_call_value}")
+ end
+ end
+
+ context 'when send_notifications value of inner call is the same as for outer call' do
+ let(:outer_call_value) { !send_notification_before }
+ let(:inner_call_value) { outer_call_value }
+
+ it 'does not log any debug messages' do
+ expect(Rails.logger).not_to have_received(:debug)
+ end
+ end
+
+ context 'when send_notifications value of inner call is nil' do
+ let(:outer_call_value) { !send_notification_before }
+ let(:inner_call_value) { nil }
+
+ it 'executes the block' do
+ expect(proc_called_counter.called)
+ .to be_truthy
+ end
+
+ it 'keeps the value of the outer block call' do
+ expect(proc_called_counter.send_notifications)
+ .to eq(outer_call_value)
+ end
+
+ it 'does not log any debug messages' do
+ expect(Rails.logger).not_to have_received(:debug)
+ end
+ end
+
+ context 'when send_notifications value of outer call is nil' do
+ let(:outer_call_value) { nil }
+ let(:inner_call_value) { !send_notification_before }
+
+ it 'executes the block' do
+ expect(proc_called_counter.called)
+ .to be_truthy
+ end
+
+ it 'sets the value to the inner block call value' do
+ expect(proc_called_counter.send_notifications)
+ .to eq(inner_call_value)
+ end
+
+ it 'does not log any debug messages' do
+ expect(Rails.logger).not_to have_received(:debug)
+ end
+ end
end
it 'is thread safe' do