From c7a307553c2c334904fa2caedf387bf9a1a000eb Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 19 Dec 2019 13:22:40 +0100 Subject: [PATCH] helpers: allow `dossier_display_state` to take a state as input This allow to use either: - `dossier_display_state(dossier)` - `dossier_display_state(:en_construction)` --- app/helpers/dossier_helper.rb | 7 ++++--- spec/helpers/dossier_helper_spec.rb | 31 +++++++---------------------- 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/app/helpers/dossier_helper.rb b/app/helpers/dossier_helper.rb index e279f199b..f4dabce51 100644 --- a/app/helpers/dossier_helper.rb +++ b/app/helpers/dossier_helper.rb @@ -47,9 +47,10 @@ module DossierHelper dossier.brouillon? && dossier.procedure.close? end - def dossier_display_state(dossier, lower: false) - state = I18n.t(dossier.state, scope: [:activerecord, :attributes, :dossier, :state]) - lower ? state.downcase : state + def dossier_display_state(dossier_or_state, lower: false) + state = dossier_or_state.is_a?(Dossier) ? dossier_or_state.state : dossier_or_state + display_state = I18n.t(state, scope: [:activerecord, :attributes, :dossier, :state]) + lower ? display_state.downcase : display_state end def dossier_legacy_state(dossier) diff --git a/spec/helpers/dossier_helper_spec.rb b/spec/helpers/dossier_helper_spec.rb index e327ca095..2296235f9 100644 --- a/spec/helpers/dossier_helper_spec.rb +++ b/spec/helpers/dossier_helper_spec.rb @@ -132,37 +132,20 @@ RSpec.describe DossierHelper, type: :helper do expect(subject).to eq('Refusé') end - context "lower: true" do + context 'when requesting lowercase' do subject { dossier_display_state(dossier, lower: true) } - it 'brouillon is brouillon' do + it 'lowercases the display name' do dossier.brouillon! expect(subject).to eq('brouillon') end + end - it 'en_construction is En construction' do - dossier.en_construction! - expect(subject).to eq('en construction') - end + context 'when providing directly a state name' do + subject { dossier_display_state(:brouillon) } - it 'accepte is traité' do - dossier.accepte! - expect(subject).to eq('accepté') - end - - it 'en_instruction is reçu' do - dossier.en_instruction! - expect(subject).to eq('en instruction') - end - - it 'sans_suite is traité' do - dossier.sans_suite! - expect(subject).to eq('sans suite') - end - - it 'refuse is traité' do - dossier.refuse! - expect(subject).to eq('refusé') + it 'generates a display name for the given state' do + expect(subject).to eq('Brouillon') end end end