diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index 5374c5111..d214f30d0 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -13,19 +13,16 @@ class Champs::CarteController < ApplicationController coordinates = params[:dossier][:champs_private_attributes][params[:position]][:value] end - if params[:champ_id].present? - @champ = Champ + @champ = if params[:champ_id].present? + Champ .joins(:dossier) .where(dossiers: { user_id: logged_user_ids }) .find(params[:champ_id]) else - @champ = Champs::CarteChamp.new(type_de_champ: TypeDeChamp.new( - type_champ: TypeDeChamp.type_champs.fetch(:carte), - options: { - quartiers_prioritaires: true, - cadastres: true - } - )) + TypeDeChamp + .joins(:procedure) + .where(procedures: { administrateur_id: logged_user_ids }) + .find(params[:type_de_champ_id]).champ.build end geo_areas = [] diff --git a/app/helpers/champ_helper.rb b/app/helpers/champ_helper.rb index 573a8defe..ba376d8bb 100644 --- a/app/helpers/champ_helper.rb +++ b/app/helpers/champ_helper.rb @@ -9,4 +9,12 @@ module ChampHelper raw(champ.to_render_data.to_json) # rubocop:enable Rails/OutputSafety end + + def champ_carte_params(champ) + if champ.persisted? + { champ_id: champ.id } + else + { type_de_champ_id: champ.type_de_champ_id } + end + end end diff --git a/app/views/shared/dossiers/editable_champs/_carte.html.haml b/app/views/shared/dossiers/editable_champs/_carte.html.haml index eb218fa97..23c7c4e2e 100644 --- a/app/views/shared/dossiers/editable_champs/_carte.html.haml +++ b/app/views/shared/dossiers/editable_champs/_carte.html.haml @@ -8,4 +8,4 @@ = render partial: 'shared/champs/carte/geo_areas', locals: { champ: champ, error: false } = form.hidden_field :value, - data: { remote: true, url: champs_carte_path(form.index), params: { champ_id: champ&.id }.to_query, method: 'post' } + data: { remote: true, url: champs_carte_path(form.index), params: champ_carte_params(champ).to_query, method: 'post' } diff --git a/spec/features/new_administrateur/types_de_champ_spec.rb b/spec/features/new_administrateur/types_de_champ_spec.rb index 1ad203998..801f3c070 100644 --- a/spec/features/new_administrateur/types_de_champ_spec.rb +++ b/spec/features/new_administrateur/types_de_champ_spec.rb @@ -117,4 +117,19 @@ feature 'As an administrateur I can edit types de champ', js: true do expect(page).to have_content('Supprimer', count: 3) end + + it "Add carte champ" do + select('Carte', from: 'procedure_types_de_champ_attributes_0_type_champ') + fill_in 'procedure_types_de_champ_attributes_0_libelle', with: 'libellé de champ carte' + blur + check 'Quartiers prioritaires' + expect(page).to have_content('Formulaire enregistré') + + preview_window = window_opened_by { click_on 'Prévisualiser le formulaire' } + within_window(preview_window) do + expect(page).to have_content('libellé de champ carte') + expect(page).to have_content('Quartiers prioritaires') + expect(page).not_to have_content('Cadastres') + end + end end