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

use message_type if given to ServiceResult

Christophe Bliard878fee274908
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index 0dc60742a5a..258b6fcf80b 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -30,7 +30,6 @@ class ServiceResult
attr_accessor :success,
:result,
:errors,
- :message_type,
:state,
:dependent_results
@@ -47,6 +46,7 @@ class ServiceResult
initialize_errors(errors)
@message = message
+ @message_type = message_type
self.dependent_results = dependent_results
end
@@ -75,10 +75,8 @@ class ServiceResult
##
# Print messages to flash
def apply_flash_message!(flash)
- type = get_message_type
-
- if message && type
- flash[type] = message
+ if message
+ flash[message_type] = message
end
end
@@ -182,9 +180,9 @@ class ServiceResult
end
end
- def get_message_type
- if message_type.present?
- message_type.to_sym
+ def message_type
+ if @message_type
+ @message_type.to_sym
elsif success?
:notice
else
diff --git a/spec/services/service_result_spec.rb b/spec/services/service_result_spec.rb
index 65a208e5010..d059aac618c 100644
--- a/spec/services/service_result_spec.rb
+++ b/spec/services/service_result_spec.rb
@@ -112,4 +112,32 @@ describe ServiceResult, type: :model do
expect(instance.result).to be_nil
end
end
+
+ describe 'apply_flash_message!' do
+ let(:service_result) { described_class.new(message: message, **params) }
+ let(:params) { {} }
+ let(:message) { 'some message' }
+
+ subject(:flash) do
+ {}.tap { service_result.apply_flash_message!(_1) }
+ end
+
+ context 'when successful' do
+ let(:params) { { success: true } }
+
+ it { is_expected.to include(notice: message) }
+ end
+
+ context 'when failure' do
+ let(:params) { { success: false } }
+
+ it { is_expected.to include(error: message) }
+ end
+
+ context 'when setting message_type to :info' do
+ let(:params) { { message_type: :info } }
+
+ it { is_expected.to include(info: message) }
+ end
+ end
end