From bc237152e772b552acb3b052fdf43c48f77fa721 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Tue, 10 Sep 2024 11:43:25 +0200 Subject: [PATCH] feat(gallery): add origin tag to gallery item --- .../attachment/gallery_item_component.rb | 31 +++++++++++++++++-- .../gallery_item_component.html.haml | 3 ++ .../attachment/gallery_item_component_spec.rb | 22 ++++++++++--- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/app/components/attachment/gallery_item_component.rb b/app/components/attachment/gallery_item_component.rb index 8944d677a..b2f3c5b13 100644 --- a/app/components/attachment/gallery_item_component.rb +++ b/app/components/attachment/gallery_item_component.rb @@ -16,7 +16,34 @@ class Attachment::GalleryItemComponent < ApplicationComponent def gallery_demande? = @gallery_demande def libelle - attachment.record.class.in?([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp]) ? attachment.record.libelle : 'Pièce jointe au message' + from_dossier? ? attachment.record.libelle : 'Pièce jointe au message' + end + + def from_dossier? + attachment.record.class.in?([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp]) + end + + def from_messagerie? + attachment.record.is_a?(Commentaire) + end + + def from_messagerie_instructeur? + from_messagerie? && attachment.record.instructeur.present? + end + + def from_messagerie_usager? + from_messagerie? && attachment.record.instructeur.nil? + end + + def origin + case + when from_dossier? + 'Dossier usager' + when from_messagerie_instructeur? + 'Messagerie (instructeur)' + when from_messagerie_usager? + 'Messagerie (usager)' + end end def title @@ -40,7 +67,7 @@ class Attachment::GalleryItemComponent < ApplicationComponent end def updated? - attachment.record.class.in?([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp]) && updated_at > attachment.record.dossier.depose_at + from_dossier? && updated_at > attachment.record.dossier.depose_at end def updated_at diff --git a/app/components/attachment/gallery_item_component/gallery_item_component.html.haml b/app/components/attachment/gallery_item_component/gallery_item_component.html.haml index 486f14cef..10666cb00 100644 --- a/app/components/attachment/gallery_item_component/gallery_item_component.html.haml +++ b/app/components/attachment/gallery_item_component/gallery_item_component.html.haml @@ -1,5 +1,8 @@ .gallery-item - if !gallery_demande? + .fr-mb-1v + .fr-tag + = origin .fr-mb-2v.champ-updated{ class: badge_updated_class } = t(updated? ? '.updated_at' : '.created_at', datetime: helpers.try_format_datetime(updated_at, format: :veryshort)) - if displayable_pdf?(blob) || displayable_image?(blob) diff --git a/spec/components/attachment/gallery_item_component_spec.rb b/spec/components/attachment/gallery_item_component_spec.rb index ec80160bf..3ecb40f29 100644 --- a/spec/components/attachment/gallery_item_component_spec.rb +++ b/spec/components/attachment/gallery_item_component_spec.rb @@ -3,6 +3,7 @@ require 'rails_helper' RSpec.describe Attachment::GalleryItemComponent, type: :component do + let(:instructeur) { create(:instructeur) } let(:procedure) { create(:procedure, :published, types_de_champ_public:) } let(:types_de_champ_public) { [{ type: :piece_justificative }] } let(:dossier) { create(:dossier, :with_populated_champs, :en_construction, procedure:) } @@ -18,10 +19,11 @@ RSpec.describe Attachment::GalleryItemComponent, type: :component do let(:libelle) { champ.libelle } let(:attachment) { champ.piece_justificative_file.attachments.first } - it "displays libelle, link and renders title" do + it "displays libelle, link, tag and renders title" do expect(subject).to have_text(libelle) expect(subject).not_to have_text('Pièce jointe au message') expect(subject).to have_link(filename) + expect(subject).to have_text('Dossier usager') expect(component.title).to eq("#{libelle} -- #{filename}") end @@ -42,10 +44,20 @@ RSpec.describe Attachment::GalleryItemComponent, type: :component do let(:commentaire) { create(:commentaire, :with_file, dossier: dossier) } let(:attachment) { commentaire.piece_jointe.first } - it "displays a generic libelle, link and renders title" do - expect(subject).to have_text('Pièce jointe au message') - expect(subject).to have_link(filename) - expect(component.title).to eq("Pièce jointe au message -- #{filename}") + context 'from an usager' do + it "displays a generic libelle, link, tag and renders title" do + expect(subject).to have_text('Pièce jointe au message') + expect(subject).to have_link(filename) + expect(subject).to have_text('Messagerie (usager)') + expect(component.title).to eq("Pièce jointe au message -- #{filename}") + end + end + + context 'from an instructeur' do + before { commentaire.update!(instructeur:) } + it "displays the right tag" do + expect(subject).to have_text('Messagerie (instructeur)') + end end end end