choose zone options with correct label

label of the zone is the one available when the procedure has been published (or at the creation if the procedure has not been published)
This commit is contained in:
Christophe Robillard 2022-08-12 14:46:54 +02:00
parent 62f19aba8a
commit 9a9330159c
4 changed files with 56 additions and 6 deletions

View file

@ -1,9 +1,12 @@
module ZoneHelper
def grouped_options_for_zone
def grouped_options_for_zone(date)
collectivite = Zone.find_by(acronym: "COLLECTIVITE")
{
"--" => [[I18n.t('i_dont_know', scope: 'utils'), nil], [collectivite.label, collectivite.id]],
I18n.t('ministeres', scope: 'zones') => (Zone.order(:label) - [collectivite]).map { |m| [m.label, m.id] }
"--" => [
[I18n.t('i_dont_know', scope: 'utils'), nil],
[collectivite.label, collectivite.id]
],
I18n.t('ministeres', scope: 'zones') => (Zone.available_at(date) - [collectivite]).map { |m| [m.label_at(date), m.id] }
}
end
end

View file

@ -812,6 +812,10 @@ class Procedure < ApplicationRecord
api_particulier_sources['mesri'].present?
end
def published_or_created_at
published_at || created_at
end
private
def move_new_children_to_new_parent_coordinate(new_draft)

View file

@ -17,7 +17,7 @@
= f.label :zone do
= t('zone', scope: 'activerecord.attributes.procedure')
%span.mandatory *
= f.select :zone_id, grouped_options_for_zone
= f.select :zone_id, grouped_options_for_zone(@procedure.published_or_created_at)
%h3.header-subsection Logo de la démarche
= render Attachment::EditComponent.new(form: f, attached_file: @procedure.logo, direct_upload: true, user_can_destroy: true)

View file

@ -1,15 +1,58 @@
describe 'administrateurs/procedures/edit.html.haml' do
let(:logo) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') }
let(:procedure) { create(:procedure, logo: logo, lien_site_web: 'http://some.website') }
let(:populate_zones_task) { Rake::Task['zones:populate_zones'] }
before do
assign(:procedure, procedure)
render
Flipper.enable(:zonage)
populate_zones_task.invoke
end
after do
populate_zones_task.reenable
end
context 'when procedure logo is present' do
it 'display on the page' do
assign(:procedure, procedure)
render
expect(rendered).to have_selector('.procedure-logos')
end
end
context 'when procedure has never been published' do
before { Timecop.freeze(now) }
after { Timecop.return }
let(:procedure) { create(:procedure, zone: Zone.find_by(acronym: 'MTEI')) }
let(:now) { Time.zone.parse('18/05/2022') }
it 'displays zones with label available at the creation date' do
assign(:procedure, procedure)
render
expect(rendered).to have_content("Ministère du Travail")
expect(rendered).not_to have_content("Ministère du Travail, du Plein emploi et de l'Insertion")
end
end
context 'when procedure has been published' do
before { Timecop.freeze(now) }
after { Timecop.return }
let(:procedure) { create(:procedure, zone: Zone.find_by(acronym: 'MTEI')) }
let(:now) { Time.zone.parse('18/05/2022') }
it 'displays zones with label available at the creation date' do
Timecop.freeze(Time.zone.parse('22/05/2022')) do
procedure.publish!
end
assign(:procedure, procedure)
render
expect(rendered).to have_content("Ministère du Travail, du Plein emploi et de l'Insertion")
end
end
end