fix: "remove" a champ to prefill (#8612)

* fix: "remove" a champ to prefill

Removing a champ to prefill on /preremplir used to fail, because of a
collusion (having the same form multiple times on un page looks like a
bad idea, when it comes to multiple input fields).

* fix specs
This commit is contained in:
Sébastien Carceles 2023-02-13 09:17:22 +01:00 committed by GitHub
parent 4b0e6ea382
commit 1a72b80f8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 20 deletions

View file

@ -25,8 +25,6 @@ class PrefillDescriptionsController < ApplicationController
end
def prefill_description_params
params.require(:procedure).permit(selected_type_de_champ_ids: [])
rescue ActionController::ParameterMissing
{ selected_type_de_champ_ids: [] }
params.require(:type_de_champ).permit(:selected_type_de_champ_ids)
end
end

View file

@ -11,7 +11,7 @@ class PrefillDescription < SimpleDelegator
end
def update(attributes)
@selected_type_de_champ_ids = attributes[:selected_type_de_champ_ids].presence || []
@selected_type_de_champ_ids = attributes[:selected_type_de_champ_ids]&.split(' ') || []
end
def types_de_champ

View file

@ -7,14 +7,12 @@
.card
.card-title.flex.justify-between.align-center
= type_de_champ.libelle
= form_for prefill_description, url: prefill_description_path(prefill_description.path), data: { turbo: true } do |f|
= form_for type_de_champ, url: prefill_description_path(prefill_description.path), data: { turbo: true } do |f|
- if prefill_description.include?(type_de_champ.id)
- (prefill_description.selected_type_de_champ_ids - [type_de_champ.id.to_s]).each do |id|
= f.hidden_field :selected_type_de_champ_ids, value: id, multiple: true
= f.hidden_field :selected_type_de_champ_ids, value: (prefill_description.selected_type_de_champ_ids - [type_de_champ.id.to_s])
= f.submit t("views.prefill_descriptions.edit.champ_remove"), class: 'fr-btn fr-btn--secondary fr-btn--md'
- elsif prefillable
- (prefill_description.selected_type_de_champ_ids + [type_de_champ.id.to_s]).each do |id|
= f.hidden_field :selected_type_de_champ_ids, value: id, multiple: true
- elsif prefillable && !prefill_description.include?(type_de_champ.id)
= f.hidden_field :selected_type_de_champ_ids, value: (prefill_description.selected_type_de_champ_ids + [type_de_champ.id.to_s])
= f.submit t("views.prefill_descriptions.edit.champ_add"), class: 'fr-btn fr-btn--md'
- else
%button.fr-btn.fr-btn--secondary{ disabled: true }

View file

@ -55,14 +55,14 @@ describe PrefillDescriptionsController, type: :controller do
let(:type_de_champ2) { create(:type_de_champ_text, procedure: procedure) }
subject(:update_request) do
patch :update, params: { path: procedure.path, procedure: params }, format: :turbo_stream
patch :update, params: { path: procedure.path, type_de_champ: params }, format: :turbo_stream
end
before { update_request }
context 'when adding a type_de_champ_id' do
let(:type_de_champ_to_add) { create(:type_de_champ_text, procedure: procedure) }
let(:params) { { selected_type_de_champ_ids: [type_de_champ.id, type_de_champ_to_add.id] } }
let(:params) { { selected_type_de_champ_ids: [type_de_champ.id, type_de_champ_to_add.id].join(' ') } }
it { expect(response).to render_template(:update) }
@ -91,7 +91,7 @@ describe PrefillDescriptionsController, type: :controller do
context 'when removing a type_de_champ_id' do
let(:type_de_champ_to_remove) { type_de_champ2 }
let(:params) { { selected_type_de_champ_ids: [type_de_champ] } }
let(:params) { { selected_type_de_champ_ids: type_de_champ } }
it { expect(response).to render_template(:update) }
@ -121,7 +121,7 @@ describe PrefillDescriptionsController, type: :controller do
context 'when removing the last type de champ' do
let(:type_de_champ_to_remove) { type_de_champ }
let(:params) { { selected_type_de_champ_ids: [] } }
let(:params) { { selected_type_de_champ_ids: '' } }
it { expect(response).to render_template(:update) }

View file

@ -3,12 +3,10 @@ RSpec.describe PrefillDescription, type: :model do
describe '#update' do
let(:prefill_description) { described_class.new(build(:procedure)) }
let(:selected_type_de_champ_ids) { ["1", "2"] }
subject(:update) { prefill_description.update(attributes) }
let(:selected_type_de_champ_ids) { ['1', '2'] }
subject(:update) { prefill_description.update({ selected_type_de_champ_ids: selected_type_de_champ_ids.join(' ') }) }
context 'when selected_type_de_champ_ids are given' do
let(:attributes) { { selected_type_de_champ_ids: selected_type_de_champ_ids } }
it 'populate selected_type_de_champ_ids' do
expect { update }.to change { prefill_description.selected_type_de_champ_ids }.from([]).to(selected_type_de_champ_ids)
end
@ -57,7 +55,7 @@ RSpec.describe PrefillDescription, type: :model do
subject(:included) { prefill_description.include?(type_de_champ_id) }
context 'when the id has been added to the prefill_description' do
before { prefill_description.update(selected_type_de_champ_ids: ["1"]) }
before { prefill_description.update(selected_type_de_champ_ids: '1') }
it { expect(included).to eq(true) }
end
@ -73,7 +71,7 @@ RSpec.describe PrefillDescription, type: :model do
subject(:too_long) { prefill_description.link_too_long? }
before { prefill_description.update(selected_type_de_champ_ids: create_list(:type_de_champ_text, type_de_champs_count, procedure: procedure).map(&:id)) }
before { prefill_description.update(selected_type_de_champ_ids: create_list(:type_de_champ_text, type_de_champs_count, procedure: procedure).map(&:id)).join(' ') }
context 'when the prefill link is too long' do
let(:type_de_champs_count) { 65 }