use message_type if given to ServiceResult

pull/10372/head
Christophe Bliard 3 years ago
parent 73ca1b383c
commit 878fee2749
No known key found for this signature in database
GPG Key ID: 2BC07603210C3FA4
  1. 14
      app/services/service_result.rb
  2. 28
      spec/services/service_result_spec.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

@ -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

Loading…
Cancel
Save