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?
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)

View file

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