refactor(attachment): rename user_can_download -> explicit view_as

- link: shows a simple link to open attachment
- download: shows complete DSFR download UI
This commit is contained in:
Colin Darie 2022-12-08 00:15:05 +01:00
parent 7c5d27d8e9
commit 4824363879
12 changed files with 43 additions and 32 deletions

View file

@ -2,8 +2,6 @@
class Attachment::EditComponent < ApplicationComponent
attr_reader :champ
attr_reader :attachment
attr_reader :user_can_download
alias user_can_download? user_can_download
attr_reader :user_can_destroy
alias user_can_destroy? user_can_destroy
attr_reader :as_multiple
@ -11,14 +9,14 @@ class Attachment::EditComponent < ApplicationComponent
EXTENSIONS_ORDER = ['jpeg', 'png', 'pdf', 'zip'].freeze
def initialize(champ: nil, auto_attach_url: nil, attached_file:, direct_upload: true, index: 0, as_multiple: false, user_can_download: false, user_can_destroy: true, **kwargs)
def initialize(champ: nil, auto_attach_url: nil, attached_file:, direct_upload: true, index: 0, as_multiple: false, view_as: :link, user_can_destroy: true, **kwargs)
@as_multiple = as_multiple
@attached_file = attached_file
@auto_attach_url = auto_attach_url
@champ = champ
@direct_upload = direct_upload
@index = index
@user_can_download = user_can_download
@view_as = view_as
@user_can_destroy = user_can_destroy
# attachment passed by kwarg because we don't want a default (nil) value.
@ -80,7 +78,7 @@ class Attachment::EditComponent < ApplicationComponent
if champ.present?
auto_attach_url
else
attachment_path(user_can_edit: true, user_can_download: @user_can_download, auto_attach_url: @auto_attach_url)
attachment_path(user_can_edit: true, view_as: @view_as, auto_attach_url: @auto_attach_url)
end
end
@ -118,7 +116,7 @@ class Attachment::EditComponent < ApplicationComponent
end
def downloadable?
return false unless user_can_download?
return false unless @view_as == :download
viewable?
end
@ -206,6 +204,8 @@ class Attachment::EditComponent < ApplicationComponent
def verify_initialization!(kwargs)
fail ArgumentError, "Unknown kwarg #{kwargs.keys.join(', ')}" unless kwargs.empty?
fail ArgumentError, "Invalid view_as:#{@view_as}, must be :download or :link" if [:download, :link].exclude?(@view_as)
end
def track_issue_with_missing_validators

View file

@ -9,18 +9,17 @@ class Attachment::MultipleComponent < ApplicationComponent
attr_reader :champ
attr_reader :form_object_name
attr_reader :max
attr_reader :view_as
attr_reader :user_can_destroy
alias user_can_destroy? user_can_destroy
attr_reader :user_can_download
alias user_can_download? user_can_download
delegate :count, :empty?, to: :attachments, prefix: true
def initialize(champ:, attached_file:, form_object_name: nil, user_can_download: false, user_can_destroy: true, max: nil)
def initialize(champ:, attached_file:, form_object_name: nil, view_as: :link, user_can_destroy: true, max: nil)
@champ = champ
@attached_file = attached_file
@form_object_name = form_object_name
@user_can_download = user_can_download
@view_as = view_as
@user_can_destroy = user_can_destroy
@max = max || DEFAULT_MAX_ATTACHMENTS

View file

@ -1,10 +1,10 @@
.fr-mb-4w.attachment-multiple{ class: class_names("fr-downloads-group": user_can_download?, "destroyable": user_can_destroy?) }
.fr-mb-4w.attachment-multiple{ class: class_names("fr-downloads-group": view_as == :download, "destroyable": user_can_destroy?) }
= template
%ul
- each_attachment do |attachment, index|
%li{ id: dom_id(attachment) }
= render Attachment::EditComponent.new(champ:, attached_file:, attachment:, index:, as_multiple: true, user_can_destroy:, user_can_download:, form_object_name:)
= render Attachment::EditComponent.new(champ:, attached_file:, attachment:, index:, as_multiple: true, view_as:, user_can_destroy:, form_object_name:)
%div{ id: empty_component_id, class: class_names("hidden": !can_attach_next?) }
= render Attachment::EditComponent.new(champ:, attached_file:, attachment: nil, index: attachments_count, user_can_destroy:, form_object_name:)

View file

@ -1,2 +1,17 @@
class EditableChamp::PieceJustificativeComponent < EditableChamp::EditableChampBaseComponent
def view_as
if @champ.dossier.brouillon?
:link
else
:download
end
end
def user_can_destroy?
!@champ.mandatory? || @champ.dossier.brouillon?
end
def max
[true, nil].include?(@champ.procedure&.piece_justificative_multiple?) ? Attachment::MultipleComponent::DEFAULT_MAX_ATTACHMENTS : 1
end
end

View file

@ -1,7 +1,4 @@
- user_can_destroy = !@champ.mandatory? || @champ.dossier.brouillon?
- user_can_download = !@champ.dossier.brouillon?
- max = [true, nil].include?(@champ.procedure&.piece_justificative_multiple?) ? Attachment::MultipleComponent::DEFAULT_MAX_ATTACHMENTS : 1
= render Attachment::MultipleComponent.new(champ: @champ, attached_file: @champ.piece_justificative_file, form_object_name: @form.object_name, user_can_destroy:, user_can_download:, max:) do |c|
= render Attachment::MultipleComponent.new(champ: @champ, attached_file: @champ.piece_justificative_file, form_object_name: @form.object_name, view_as:, user_can_destroy: user_can_destroy?, max:) do |c|
- if @champ.type_de_champ.piece_justificative_template&.attached?
- c.with_template do
= render partial: "shared/piece_justificative_template", locals: { champ: @champ }

View file

@ -77,7 +77,7 @@ class TypesDeChampEditor::ChampComponent < ApplicationComponent
{
attached_file: type_de_champ.piece_justificative_template,
auto_attach_url: helpers.auto_attach_url(type_de_champ),
user_can_download: true
view_as: :download
}
end

View file

@ -6,7 +6,7 @@ class AttachmentsController < ApplicationController
@attachment = @blob.attachments.find(params[:id])
@user_can_edit = cast_bool(params[:user_can_edit])
@user_can_download = cast_bool(params[:user_can_download])
@view_as = params[:view_as]&.to_sym
@auto_attach_url = params[:auto_attach_url]
respond_to do |format|

View file

@ -14,7 +14,7 @@
= f.text_area :description, rows: '6', placeholder: 'Description de la démarche, destinataires, etc. ', class: 'form-control'
%h3.header-subsection Logo de la démarche
= render Attachment::EditComponent.new(attached_file: @procedure.logo, user_can_download: true)
= render Attachment::EditComponent.new(attached_file: @procedure.logo, view_as: :link)
%h3.header-subsection Conservation des données
= f.label :duree_conservation_dossiers_dans_ds do
@ -52,7 +52,7 @@
= f.text_field :cadre_juridique, class: 'form-control', placeholder: 'https://www.legifrance.gouv.fr/'
= f.label :deliberation, 'Importer le texte'
= render Attachment::EditComponent.new(attached_file: @procedure.deliberation, user_can_download: true)
= render Attachment::EditComponent.new(attached_file: @procedure.deliberation, view_as: :download)
%h3.header-subsection
RGPD
@ -81,7 +81,7 @@
= f.label :notice, 'Notice'
%p.notice
Formats acceptés : .doc, .odt, .pdf, .ppt, .pptx
= render Attachment::EditComponent.new(attached_file: @procedure.notice, user_can_download: true)
= render Attachment::EditComponent.new(attached_file: @procedure.notice, view_as: :download)
- if !@procedure.locked?
%h3.header-subsection À qui sadresse ma démarche ?

View file

@ -1,5 +1,5 @@
= turbo_stream.replace dom_id(@attachment, :edit) do
- if @user_can_edit
= render Attachment::EditComponent.new(attachment: @attachment, attached_file: @attachment.record.public_send(@attachment.name), auto_attach_url: @auto_attach_url, user_can_download: @user_can_download)
= render Attachment::EditComponent.new(attachment: @attachment, attached_file: @attachment.record.public_send(@attachment.name), auto_attach_url: @auto_attach_url, view_as: @view_as)
- else
= render Attachment::ShowComponent.new(attachment: @attachment)

View file

@ -17,7 +17,7 @@
= form_for @avis, url: expert_avis_path(@avis.procedure, @avis), html: { class: 'form', data: { controller: 'persisted-form', persisted_form_key_value: dom_id(@avis) }, multipart: true } do |f|
= f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true
= render Attachment::EditComponent.new(attached_file: @avis.piece_justificative_file, user_can_download: true)
= render Attachment::EditComponent.new(attached_file: @avis.piece_justificative_file, view_as: :download)
.flex.justify-between.align-baseline
%p.confidentiel.flex

View file

@ -386,15 +386,15 @@
%h3.fr-mt-4w New attachment on TypeDeChamp
= render Attachment::EditComponent.new(auto_attach_url: "/some-auto-attach-path", attached_file: tdc.piece_justificative_template, attachment: nil)
%h3.fr-mt-4w Existing attachment on TypeDeChamp
%h3.fr-mt-4w Existing attachment on TypeDeChamp, view as link
= render Attachment::EditComponent.new(auto_attach_url: "/some-auto-attach-path", attached_file: tdc.piece_justificative_template, attachment: attachment.reload)
%h3.fr-mt-4w Existing attachment on TypeDeChamp can download
= render Attachment::EditComponent.new(auto_attach_url: "/some-auto-attach-path", attached_file: tdc.piece_justificative_template, attachment: attachment.reload, user_can_download: true)
%h3.fr-mt-4w Existing attachment on TypeDeChamp view as download
= render Attachment::EditComponent.new(auto_attach_url: "/some-auto-attach-path", attached_file: tdc.piece_justificative_template, attachment: attachment.reload, view_as: :download)
%h3.fr-mt-4w New attachment on generic object
= render Attachment::EditComponent.new(attached_file: avis.introduction_file)
%h3.fr-mt-4w Existing attachment on generic object, can download
= render Attachment::EditComponent.new(attached_file: avis.introduction_file, attachment: attachment.reload, user_can_download: true)
%h3.fr-mt-4w Existing attachment on generic object, view as download
= render Attachment::EditComponent.new(attached_file: avis.introduction_file, attachment: attachment.reload, view_as: :download)

View file

@ -83,8 +83,8 @@ RSpec.describe Attachment::EditComponent, type: :component do
end
end
context 'when user can download' do
let(:kwargs) { { user_can_download: true } }
context 'when view as download' do
let(:kwargs) { { view_as: :download } }
context 'when watermarking is done' do
before do
@ -110,8 +110,8 @@ RSpec.describe Attachment::EditComponent, type: :component do
end
end
context 'when user cannot download' do
let(:kwargs) { { user_can_download: false } }
context 'when view as link' do
let(:kwargs) { { view_as: :link } }
context 'when watermarking is done' do
before do