Merge pull request #2537 from tchak/dossier-link

Refactor dossier_link champ
This commit is contained in:
gregoirenovel 2018-09-08 10:12:06 +02:00 committed by GitHub
commit 5b7213816e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 96 additions and 90 deletions

View file

@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
protect_from_forgery with: :exception, if: -> { !Rails.env.test? }
before_action :load_navbar_left_pannel_partial_url
before_action :set_raven_context
before_action :authorize_request_for_profiler
@ -41,6 +41,16 @@ class ApplicationController < ActionController::Base
protected
def authenticate_logged_user!
if gestionnaire_signed_in?
authenticate_gestionnaire!
elsif administrateur_signed_in?
authenticate_administrateur!
else
authenticate_user!
end
end
def authenticate_gestionnaire!
if gestionnaire_signed_in?
super
@ -80,6 +90,8 @@ class ApplicationController < ActionController::Base
logged_users.first
end
helper_method :logged_user
def logged_user_roles
roles = logged_users.map { |logged_user| logged_user.class.name }
roles.any? ? roles.join(', ') : 'Guest'

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

@ -162,13 +162,6 @@ class Users::DossiersController < UsersController
redirect_to url_for dossiers_path
end
def text_summary
dossier = Dossier.find(params[:dossier_id])
render json: { textSummary: dossier.text_summary }
rescue ActiveRecord::RecordNotFound
render json: {}, status: 404
end
private
def check_siret

View file

@ -1,42 +0,0 @@
import $ from 'jquery';
function showNotFound() {
$('.dossier-link .text-info').hide();
$('.dossier-link .text-warning').show();
}
function showData(data) {
$('.dossier-link .dossier-text-summary').text(data.textSummary);
$('.dossier-link .text-info').show();
$('.dossier-link .text-warning').hide();
}
function hideEverything() {
$('.dossier-link .text-info').hide();
$('.dossier-link .text-warning').hide();
}
function fetchProcedureLibelle(e) {
const dossierId = $(e.target).val();
if (dossierId) {
$.get(`/users/dossiers/${dossierId}/text_summary`)
.done(showData)
.fail(showNotFound);
} else {
hideEverything();
}
}
let timeOut;
function debounceFetchProcedureLibelle(e) {
if (timeOut) {
clearTimeout(timeOut);
}
timeOut = setTimeout(() => fetchProcedureLibelle(e), 300);
}
$(document).on(
'input',
'[data-type=dossier-link]',
debounceFetchProcedureLibelle
);

View file

@ -21,7 +21,6 @@ import '../new_design/form-validation';
import '../new_design/carto';
import '../new_design/select2';
import '../new_design/champs/dossier-link';
import '../new_design/champs/linked-drop-down-list';
import '../new_design/champs/siret';

View file

@ -10,6 +10,7 @@ class Administrateur < ApplicationRecord
has_many :administrateurs_procedures
has_many :admin_procedures, through: :administrateurs_procedures, source: :procedure
has_many :services
has_many :dossiers, -> { state_not_brouillon }, through: :procedures
before_validation -> { sanitize_email(:email) }
before_save :ensure_api_token

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
@ -158,8 +159,6 @@ Rails.application.routes.draw do
post '/siret_informations' => 'dossiers#siret_informations'
put '/change_siret' => 'dossiers#change_siret'
get 'text_summary' => 'dossiers#text_summary'
end
resource 'dossiers'

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

View file

@ -446,29 +446,4 @@ describe Users::DossiersController, type: :controller do
subject
end
end
describe 'Get #text_summary' do
let!(:dossier) { create(:dossier, procedure: procedure) }
context 'when user is connected' do
before { sign_in user }
context 'when the dossier exist' do
before { get :text_summary, params: { dossier_id: dossier.id } }
it 'returns the procedure name' do
expect(JSON.parse(response.body)).to eq("textSummary" => "Dossier en brouillon répondant à la démarche #{procedure.libelle} gérée par l'organisme #{procedure.organisation}")
end
end
context 'when the dossier does not exist' do
before { get :text_summary, params: { dossier_id: 666 } }
it { expect(response.code).to eq('404') }
end
end
context 'when user is not connected' do
before { get :text_summary, params: { dossier_id: dossier.id } }
it { expect(response.code).to eq('302') }
end
end
end