From 21bb04d74ed4670be864861ce922e21b2a4b6444 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Tue, 12 Dec 2017 15:44:30 +0000 Subject: [PATCH] [fix #1066] Fix links to related dossier for experts - link to full dossier if gestionnaire is affected on procedure - link to avis if avis expert requested on related dossier --- app/helpers/dossier_link_helper.rb | 12 ++++++ .../dossiers/_champs.html.haml | 6 ++- spec/helpers/dossier_link_helper_spec.rb | 27 +++++++++++++ .../dossiers/_champs.html.haml_spec.rb | 40 ++++++++++++++----- 4 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 app/helpers/dossier_link_helper.rb create mode 100644 spec/helpers/dossier_link_helper_spec.rb diff --git a/app/helpers/dossier_link_helper.rb b/app/helpers/dossier_link_helper.rb new file mode 100644 index 000000000..0d46e0cb7 --- /dev/null +++ b/app/helpers/dossier_link_helper.rb @@ -0,0 +1,12 @@ +module DossierLinkHelper + def dossier_linked_path(gestionnaire, dossier) + if dossier.procedure.gestionnaires.include?(gestionnaire) + dossier_path(dossier.procedure, dossier) + else + avis = dossier.avis.find_by(gestionnaire: gestionnaire) + if avis.present? + avis_path(avis) + end + end + end +end diff --git a/app/views/new_gestionnaire/dossiers/_champs.html.haml b/app/views/new_gestionnaire/dossiers/_champs.html.haml index 7d7129330..3abfe424a 100644 --- a/app/views/new_gestionnaire/dossiers/_champs.html.haml +++ b/app/views/new_gestionnaire/dossiers/_champs.html.haml @@ -21,7 +21,11 @@ %td.rich-text - dossier = Dossier.includes(:procedure).find_by(id: c.value) - if dossier - = link_to("Dossier nº #{dossier.id}", dossier_path(dossier.procedure, dossier), target: '_blank') + - path = dossier_linked_path(current_gestionnaire, dossier) + - if path.present? + = link_to("Dossier nº #{dossier.id}", path, target: '_blank') + - else + Dossier nº #{dossier.id} %br = dossier.text_summary - else diff --git a/spec/helpers/dossier_link_helper_spec.rb b/spec/helpers/dossier_link_helper_spec.rb new file mode 100644 index 000000000..5613c5207 --- /dev/null +++ b/spec/helpers/dossier_link_helper_spec.rb @@ -0,0 +1,27 @@ +describe DossierLinkHelper do + describe "#dossier_linked_path" do + context "when no access" do + let(:gestionnaire) { create(:gestionnaire) } + let(:dossier) { create(:dossier) } + + it { expect(helper.dossier_linked_path(gestionnaire, dossier)).to be_nil } + end + + context "when access as gestionnaire" do + let(:dossier) { create(:dossier ) } + let(:gestionnaire) { create(:gestionnaire) } + + before { dossier.procedure.gestionnaires << gestionnaire } + + it { expect(helper.dossier_linked_path(gestionnaire, dossier)).to eq(dossier_path(dossier.procedure, dossier)) } + end + + context "when access as expert" do + let(:dossier) { create(:dossier ) } + let(:gestionnaire) { create(:gestionnaire) } + let!(:avis) { create(:avis, dossier: dossier, gestionnaire: gestionnaire) } + + it { expect(helper.dossier_linked_path(gestionnaire, dossier)).to eq(avis_path(avis)) } + end + end +end diff --git a/spec/views/new_gestionnaire/dossiers/_champs.html.haml_spec.rb b/spec/views/new_gestionnaire/dossiers/_champs.html.haml_spec.rb index 3ad9c9cb9..91075ffa1 100644 --- a/spec/views/new_gestionnaire/dossiers/_champs.html.haml_spec.rb +++ b/spec/views/new_gestionnaire/dossiers/_champs.html.haml_spec.rb @@ -1,31 +1,51 @@ describe 'new_gestionnaire/dossiers/champs.html.haml', type: :view do - before { render 'new_gestionnaire/dossiers/champs.html.haml', champs: champs } + let(:gestionnaire) { create(:gestionnaire) } + + before do + view.extend DossierLinkHelper + allow(view).to receive(:current_gestionnaire).and_return(gestionnaire) + end + + subject { render 'new_gestionnaire/dossiers/champs.html.haml', champs: champs } context "there are some champs" do let(:dossier) { create(:dossier) } + let(:avis) { create :avis, dossier: dossier, gestionnaire: gestionnaire } let(:champ1) { create(:champ, :checkbox, value: "true") } let(:champ2) { create(:champ, :header_section, value: "Section") } let(:champ3) { create(:champ, :explication, value: "mazette") } let(:champ4) { create(:champ, :dossier_link, value: dossier.id) } let(:champs) { [champ1, champ2, champ3, champ4] } - it { expect(rendered).to include(champ1.libelle) } - it { expect(rendered).to include(champ1.value) } + before { dossier.avis << avis } - it { expect(rendered).to have_css(".header-section") } - it { expect(rendered).to include(champ2.libelle) } + it { is_expected.to include(champ1.libelle) } + it { is_expected.to include(champ1.value) } - it { expect(rendered).not_to include(champ3.libelle) } - it { expect(rendered).not_to include(champ3.value) } + it { is_expected.to have_css(".header-section") } + it { is_expected.to include(champ2.libelle) } - it { expect(rendered).to have_link("Dossier nº #{dossier.id}") } - it { expect(rendered).to include(dossier.text_summary) } + it { is_expected.not_to include(champ3.libelle) } + it { is_expected.not_to include(champ3.value) } + + it { is_expected.to have_link("Dossier nº #{dossier.id}") } + it { is_expected.to include(dossier.text_summary) } + end + + context "with a dossier champ, but we are not authorized to acces the dossier" do + let(:dossier) { create(:dossier) } + let(:champ) { create(:champ, :dossier_link, value: dossier.id) } + let(:champs) { [champ] } + + it { is_expected.not_to have_link("Dossier nº #{dossier.id}") } + it { is_expected.to include("Dossier nº #{dossier.id}") } + it { is_expected.to include(dossier.text_summary) } end context "with a dossier_link champ but without value" do let(:champ) { create(:champ, :dossier_link, value: nil) } let(:champs) { [champ] } - it { expect(rendered).to include("Pas de dossier associé") } + it { is_expected.to include("Pas de dossier associé") } end end