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, attr_accessor :success,
:result, :result,
:errors, :errors,
:message_type,
:state, :state,
:dependent_results :dependent_results
@ -47,6 +46,7 @@ class ServiceResult
initialize_errors(errors) initialize_errors(errors)
@message = message @message = message
@message_type = message_type
self.dependent_results = dependent_results self.dependent_results = dependent_results
end end
@ -75,10 +75,8 @@ class ServiceResult
## ##
# Print messages to flash # Print messages to flash
def apply_flash_message!(flash) def apply_flash_message!(flash)
type = get_message_type if message
flash[message_type] = message
if message && type
flash[type] = message
end end
end end
@ -182,9 +180,9 @@ class ServiceResult
end end
end end
def get_message_type def message_type
if message_type.present? if @message_type
message_type.to_sym @message_type.to_sym
elsif success? elsif success?
:notice :notice
else else

@ -112,4 +112,32 @@ describe ServiceResult, type: :model do
expect(instance.result).to be_nil expect(instance.result).to be_nil
end end
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 end

Loading…
Cancel
Save