From 39ecc669b19338f7b9b0a5512c3fe8d74d7c04c4 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 28 Apr 2022 15:06:04 +0200 Subject: [PATCH] refactor(champ): simplify dossier link champ --- .../champs/dossier_link_controller.rb | 9 ++------ app/views/champs/dossier_link/show.js.erb | 4 ++-- .../editable_champs/_dossier_link.html.haml | 4 ++-- config/routes.rb | 2 +- .../champs/dossier_link_controller_spec.rb | 22 +++++++++++-------- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/app/controllers/champs/dossier_link_controller.rb b/app/controllers/champs/dossier_link_controller.rb index 1ba3823e1..b41e26e6b 100644 --- a/app/controllers/champs/dossier_link_controller.rb +++ b/app/controllers/champs/dossier_link_controller.rb @@ -2,12 +2,7 @@ class Champs::DossierLinkController < ApplicationController before_action :authenticate_logged_user! def show - @position = params[:position] - - if params[:dossier].key?(:champs_attributes) - @dossier_id = params[:dossier][:champs_attributes][params[:position]][:value] - else - @dossier_id = params[:dossier][:champs_private_attributes][params[:position]][:value] - end + @champ = policy_scope(Champ).find(params[:champ_id]) + @linked_dossier_id = read_param_value(@champ.input_name, 'value') end end diff --git a/app/views/champs/dossier_link/show.js.erb b/app/views/champs/dossier_link/show.js.erb index dece3758a..cdf84195a 100644 --- a/app/views/champs/dossier_link/show.js.erb +++ b/app/views/champs/dossier_link/show.js.erb @@ -1,3 +1,3 @@ -<%= render_to_element(".dossier-link-#{@position} .help-block", +<%= render_to_element("##{@champ.input_group_id} .help-block", partial: 'shared/champs/dossier_link/help_block', - locals: { id: @dossier_id }) %> + locals: { id: @linked_dossier_id }) %> diff --git a/app/views/shared/dossiers/editable_champs/_dossier_link.html.haml b/app/views/shared/dossiers/editable_champs/_dossier_link.html.haml index 2ac650300..f3a72c1ed 100644 --- a/app/views/shared/dossiers/editable_champs/_dossier_link.html.haml +++ b/app/views/shared/dossiers/editable_champs/_dossier_link.html.haml @@ -1,11 +1,11 @@ -.dossier-link{ class: "dossier-link-#{form.index}" } +.dossier-link = form.number_field :value, id: champ.input_id, aria: { describedby: champ.describedby_id }, placeholder: "Numéro de dossier", autocomplete: 'off', required: champ.mandatory?, - data: { remote: true, url: champs_dossier_link_path(form.index) } + data: { remote: true, url: champs_dossier_link_path(champ.id) } .help-block = render partial: 'shared/champs/dossier_link/help_block', locals: { id: champ.value } diff --git a/config/routes.rb b/config/routes.rb index 0234134a6..367445016 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -141,8 +141,8 @@ Rails.application.routes.draw do end namespace :champs do - get ':position/dossier_link', to: 'dossier_link#show', as: :dossier_link get ':champ_id/siret', to: 'siret#show', as: :siret + get ':champ_id/dossier_link', to: 'dossier_link#show', as: :dossier_link post ':champ_id/carte', to: 'carte#show', as: :carte get ':champ_id/carte/features', to: 'carte#index', as: :carte_features diff --git a/spec/controllers/champs/dossier_link_controller_spec.rb b/spec/controllers/champs/dossier_link_controller_spec.rb index e76610703..319fad897 100644 --- a/spec/controllers/champs/dossier_link_controller_spec.rb +++ b/spec/controllers/champs/dossier_link_controller_spec.rb @@ -1,22 +1,26 @@ describe Champs::DossierLinkController, type: :controller do let(:user) { create(:user) } - let(:procedure) { create(:procedure, :published) } + let(:procedure) { create(:procedure, :published, :with_dossier_link) } describe '#show' do let(:dossier) { create(:dossier, user: user, procedure: procedure) } + let(:champ) { dossier.champs.first } context 'when user is connected' do render_views before { sign_in user } + let(:champs_attributes) do + champ_attributes = [] + champ_attributes[champ.id] = { value: dossier_id } + champ_attributes + end let(:params) do { + champ_id: champ.id, dossier: { - champs_attributes: { - '1' => { value: dossier_id.to_s } - } - }, - position: '1' + champs_attributes: champs_attributes + } } end let(:dossier_id) { dossier.id } @@ -30,7 +34,7 @@ describe Champs::DossierLinkController, type: :controller do expect(response.body).to include('Dossier en brouillon') expect(response.body).to include(procedure.libelle) expect(response.body).to include(procedure.organisation) - expect(response.body).to include('.dossier-link-1 .help-block') + expect(response.body).to include("##{champ.input_group_id} .help-block") end end @@ -42,14 +46,14 @@ describe Champs::DossierLinkController, type: :controller do it 'renders error message' do expect(response.body).to include('Ce dossier est inconnu') - expect(response.body).to include('.dossier-link-1 .help-block') + expect(response.body).to include("##{champ.input_group_id} .help-block") end end end context 'when user is not connected' do before do - get :show, params: { position: '1' }, format: :js, xhr: true + get :show, params: { champ_id: champ.id }, format: :js, xhr: true end it { expect(response.code).to eq('401') }