Add a champs/dossier_link endpoint and use it to fetch dossier info

This commit is contained in:
Paul Chavard 2018-09-07 17:16:29 +01:00
parent e2a2748e79
commit c49db4b5a4
6 changed files with 82 additions and 12 deletions

View file

@ -0,0 +1,11 @@
class Champs::DossierLinkController < ApplicationController
before_action :authenticate_logged_user!
def show
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
end
end

View file

@ -0,0 +1,3 @@
<%= render_to_element('.dossier-link .help-block',
partial: 'shared/champs/dossier_link/help_block',
locals: { id: @dossier_id }) %>

View file

@ -0,0 +1,8 @@
- if id.present?
- dossier = logged_user.dossiers.find_by(id: id)
- if dossier.blank?
%p.text-warning
Ce dossier est inconnu
- else
%p.text-info
%span.dossier-text-summary= sanitize(dossier.text_summary)

View file

@ -1,18 +1,9 @@
- dossier = Dossier.find_by(id: champ.value)
- show_text_summary = dossier.present?
- show_warning = !show_text_summary && champ.value.present?
- text_summary = sanitize(dossier&.text_summary)
.dossier-link
= form.number_field :value,
placeholder: "Numéro de dossier",
autocomplete: 'off',
'data-type': 'dossier-link',
required: champ.mandatory?
required: champ.mandatory?,
data: { remote: true, url: champs_dossier_link_path(form.index) }
.help-block
%p.text-info{ style: show_text_summary ? nil : 'display: none;' }
%span.dossier-text-summary= text_summary
%p.text-warning{ style: show_warning ? nil : 'display: none;' }
Ce dossier est inconnu
= render partial: 'shared/champs/dossier_link/help_block', locals: { id: champ.value }

View file

@ -111,6 +111,7 @@ Rails.application.routes.draw do
namespace :champs do
get ':champ_id/siret' => 'siret#index', as: 'siret'
get ':position/dossier_link', to: 'dossier_link#show', as: :dossier_link
end
namespace :commencer do

View file

@ -0,0 +1,56 @@
require 'spec_helper'
describe Champs::DossierLinkController, type: :controller do
let(:user) { create(:user) }
let(:procedure) { create(:procedure, :published) }
describe '#show' do
let(:dossier) { create(:dossier, user: user, procedure: procedure) }
context 'when user is connected' do
render_views
before { sign_in user }
let(:params) do
{
dossier: {
champs_attributes: {
'1' => { value: "#{dossier_id}" }
}
},
position: '1'
}
end
let(:dossier_id) { dossier.id }
context 'when the dossier exist' do
before {
get :show, params: params, format: 'js'
}
it 'returns the procedure name' do
expect(response.body).to include('Dossier en brouillon')
expect(response.body).to include(procedure.libelle)
expect(response.body).to include(procedure.organisation)
end
end
context 'when the dossier does not exist' do
let(:dossier_id) { '13' }
before {
get :show, params: params, format: 'js'
}
it { expect(response.body).to include('Ce dossier est inconnu') }
end
end
context 'when user is not connected' do
before {
get :show, params: { position: '1' }, format: 'js'
}
it { expect(response.code).to eq('401') }
end
end
end