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)`
This commit is contained in:
Pierre de La Morinerie 2019-12-19 13:22:40 +01:00
parent 0b54af822f
commit c7a307553c
2 changed files with 11 additions and 27 deletions

View file

@ -47,9 +47,10 @@ module DossierHelper
dossier.brouillon? && dossier.procedure.close? dossier.brouillon? && dossier.procedure.close?
end end
def dossier_display_state(dossier, lower: false) def dossier_display_state(dossier_or_state, lower: false)
state = I18n.t(dossier.state, scope: [:activerecord, :attributes, :dossier, :state]) state = dossier_or_state.is_a?(Dossier) ? dossier_or_state.state : dossier_or_state
lower ? state.downcase : state display_state = I18n.t(state, scope: [:activerecord, :attributes, :dossier, :state])
lower ? display_state.downcase : display_state
end end
def dossier_legacy_state(dossier) def dossier_legacy_state(dossier)

View file

@ -132,37 +132,20 @@ RSpec.describe DossierHelper, type: :helper do
expect(subject).to eq('Refusé') expect(subject).to eq('Refusé')
end end
context "lower: true" do context 'when requesting lowercase' do
subject { dossier_display_state(dossier, lower: true) } subject { dossier_display_state(dossier, lower: true) }
it 'brouillon is brouillon' do it 'lowercases the display name' do
dossier.brouillon! dossier.brouillon!
expect(subject).to eq('brouillon') expect(subject).to eq('brouillon')
end end
end
it 'en_construction is En construction' do context 'when providing directly a state name' do
dossier.en_construction! subject { dossier_display_state(:brouillon) }
expect(subject).to eq('en construction')
end
it 'accepte is traité' do it 'generates a display name for the given state' do
dossier.accepte! expect(subject).to eq('Brouillon')
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é')
end end
end end
end end