refactor(attachment): update Attachment::EditComponent everywhere

This commit is contained in:
Colin Darie 2022-11-17 19:18:58 +01:00
parent b13c5e56f6
commit 0a114270af
22 changed files with 109 additions and 65 deletions

View file

@ -29,10 +29,6 @@
float: right;
}
.attachment-link {
margin-top: $default-spacer;
}
.message-answer-button {
margin-left: auto;
}

View file

@ -2,15 +2,22 @@
class Attachment::EditComponent < ApplicationComponent
attr_reader :champ
attr_reader :attachment
attr_reader :user_can_download
alias user_can_download? user_can_download
attr_reader :as_multiple
alias as_multiple? as_multiple
EXTENSIONS_ORDER = ['jpeg', 'png', 'pdf', 'zip'].freeze
def initialize(champ: nil, auto_attach_url: nil, attached_file:, direct_upload: true, id: nil, index: 0, as_multiple: false, **kwargs)
@champ = champ
@auto_attach_url = auto_attach_url
def initialize(champ: nil, auto_attach_url: nil, field_name: nil, attached_file:, direct_upload: true, id: nil, index: 0, as_multiple: false, user_can_download: false, **kwargs)
@as_multiple = as_multiple
@attached_file = attached_file
@auto_attach_url = auto_attach_url
@champ = champ
@direct_upload = direct_upload
@id = id
@index = index
@user_can_download = user_can_download
# attachment passed by kwarg because we don't want a default (nil) value.
@attachment = if kwargs.key?(:attachment)
@ -21,16 +28,7 @@ class Attachment::EditComponent < ApplicationComponent
fail ArgumentError, "You must pass an `attachment` kwarg when not using as single attachment like in #{attached_file.name}. Set it to nil for a new attachment."
end
fail ArgumentError, "Unknown kwarg #{kwargs.keys.join(', ')}" unless kwargs.empty?
@direct_upload = direct_upload
@id = id
@index = index
@as_multiple = as_multiple
end
def object_name
@object.class.name.underscore
verify_initialization!(kwargs)
end
def first?
@ -100,11 +98,15 @@ class Attachment::EditComponent < ApplicationComponent
if champ.present?
auto_attach_url
else
attachment_path(user_can_edit: true, auto_attach_url: @auto_attach_url)
attachment_path(user_can_edit: true, user_can_download: @user_can_download, auto_attach_url: @auto_attach_url)
end
end
def file_field_name
def field_name
helpers.field_name(ActiveModel::Naming.param_key(@attached_file.record), attribute_name)
end
def attribute_name
@attached_file.name
end
@ -151,7 +153,7 @@ class Attachment::EditComponent < ApplicationComponent
return "#{champ.input_id}_#{attachment_id}"
end
file_field_name
helpers.field_id(@attached_file.record, attribute_name)
end
def auto_attach_url
@ -159,18 +161,18 @@ class Attachment::EditComponent < ApplicationComponent
return helpers.auto_attach_url(@champ) if @champ.present?
fail ArgumentError, "You must pass `auto_attach_url` when not using attachment for a Champ"
nil
end
def file_size_validator
@attached_file.record
._validators[file_field_name.to_sym]
._validators[attribute_name.to_sym]
.find { |validator| validator.class == ActiveStorageValidations::SizeValidator }
end
def content_type_validator
@attached_file.record
._validators[file_field_name.to_sym]
._validators[attribute_name.to_sym]
.find { |validator| validator.class == ActiveStorageValidations::ContentTypeValidator }
end
@ -204,12 +206,16 @@ class Attachment::EditComponent < ApplicationComponent
return false
end
def verify_initialization!(kwargs)
fail ArgumentError, "Unknown kwarg #{kwargs.keys.join(', ')}" unless kwargs.empty?
end
def track_issue_with_missing_validators
Sentry.capture_message(
"Strange case of missing validator",
extra: {
champ: champ,
file_field_name: file_field_name,
field_name: field_name,
attachment_id: attachment_id
}
)

View file

@ -5,7 +5,8 @@
= link_to('Supprimer', destroy_attachment_path, **remove_button_options, class: "fr-btn fr-btn--tertiary fr-btn--sm fr-icon-delete-line", title: "Supprimer le fichier #{attachment.filename}")
.fr-py-1v
%span.attachment-filename= attachment.filename.to_s
= link_to_if(user_can_download?, attachment.filename.to_s, attachment.url, class: "attachment-filename", download: "") do
%span.attachment-filename= attachment.filename.to_s
- if in_progress?
%p.fr-badge.fr-badge--info.fr-badge--sm.fr-badge--no-icon.fr-ml-1w
= progress_bar_label
@ -23,7 +24,7 @@
- if !as_multiple?
= file_field(champ, file_field_name, **file_field_options)
= file_field(champ, field_name, **file_field_options)
- if in_progress?
%div{ data: poll_controller_options }

View file

@ -4,18 +4,16 @@ class Attachment::MultipleComponent < ApplicationComponent
attr_reader :form
attr_reader :attached_file
attr_reader :direct_upload
attr_reader :id
attr_reader :user_can_destroy
attr_reader :max
delegate :count, :empty?, to: :attachments, prefix: true
def initialize(form:, attached_file:, user_can_destroy: false, direct_upload: true, id: nil, max: nil)
def initialize(form:, attached_file:, user_can_destroy: false, id: nil, max: nil)
@form = form
@attached_file = attached_file
@user_can_destroy = user_can_destroy
@direct_upload = direct_upload
@id = id
@max = max || 10

View file

@ -3,10 +3,10 @@
- each_attachment do |attachment, index|
%div{ id: dom_id(attachment) }
= render Attachment::EditComponent.new(champ:, attached_file:, attachment:, direct_upload:, id:, index:, as_multiple: true)
= render Attachment::EditComponent.new(champ:, attached_file:, attachment:, id:, index:, as_multiple: true)
%div{ id: empty_component_id, class: class_names("hidden": !can_attach_next?) }
= render Attachment::EditComponent.new(champ:, attached_file:, attachment: nil, direct_upload:, id:, index: attachments_count)
= render Attachment::EditComponent.new(champ:, attached_file:, attachment: nil, id:, index: attachments_count)
// single poll and refresh message for all attachments
- if in_progress?

View file

@ -1,4 +1,4 @@
.attachment-link{ id: dom_id(attachment, :show), class: class_names("attachment-error": error?, "fr-mb-2w": !should_display_link?) }
%div{ id: dom_id(attachment, :show), class: class_names("attachment-error": error?, "fr-mb-2w": !should_display_link?) }
- if should_display_link?
= render Dsfr::DownloadComponent.new(attachment: attachment) do |c|
- if !attachment.virus_scanner.started?

View file

@ -17,7 +17,7 @@
= t('.delete_button')
- if commentaire.piece_jointe.attached?
.attachment-link
.fr-ml-2w
= render Attachment::ShowComponent.new(attachment: commentaire.piece_jointe.attachment)
- if show_reply_button?

View file

@ -6,6 +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])
@auto_attach_url = params[:auto_attach_url]
respond_to do |format|

View file

@ -0,0 +1,39 @@
module FormTagHelper
# from Rails 7 ActionView::Helpers::FormTagHelper
# https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-field_id
# Should be removed when we upgrade to Rails 7
def field_id(object_name, method_name, *suffixes, index: nil, namespace: nil)
if object_name.respond_to?(:model_name)
object_name = object_name.model_name.singular
end
sanitized_object_name = object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").delete_suffix("_")
sanitized_method_name = method_name.to_s.delete_suffix("?")
[
namespace,
sanitized_object_name.presence,
(index unless sanitized_object_name.empty?),
sanitized_method_name,
*suffixes
].tap(&:compact!).join("_")
end
# from Rails 7 ActionView::Helpers::FormTagHelper
# https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-field_name
# Should be removed when we upgrade to Rails 7
def field_name(object_name, method_name, *method_names, multiple: false, index: nil)
names = method_names.map! { |name| "[#{name}]" }.join
# a little duplication to construct fewer strings
case
when object_name.blank?
"#{method_name}#{names}#{multiple ? "[]" : ""}"
when index
"#{object_name}[#{index}][#{method_name}]#{names}#{multiple ? "[]" : ""}"
else
"#{object_name}[#{method_name}]#{names}#{multiple ? "[]" : ""}"
end
end
end

View file

@ -25,7 +25,7 @@
= tag[:description]
%h3.header-subsection Logo de l'attestation
= render Attachment::EditComponent.new(form: f, attached_file: @attestation_template.logo, direct_upload: false, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: @attestation_template.logo, direct_upload: false)
%p.notice
Formats acceptés : JPG / JPEG / PNG.
@ -33,7 +33,7 @@
Dimensions conseillées : au minimum 500 px de largeur ou de hauteur, poids maximum : 0,5 Mo.
%h3.header-subsection Tampon de l'attestation
= render Attachment::EditComponent.new(form: f, attached_file: @attestation_template.signature, direct_upload: false, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: @attestation_template.signature, direct_upload: false)
%p.notice
Formats acceptés : JPG / JPEG / PNG.

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(form: f, attached_file: @procedure.logo, direct_upload: true, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: @procedure.logo, user_can_download: true)
%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(form: f, attached_file: @procedure.deliberation, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: @procedure.deliberation, user_can_download: true)
%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(form: f, attached_file: @procedure.notice, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: @procedure.notice, user_can_download: true)
- if !@procedure.locked?
%h3.header-subsection À qui sadresse ma démarche ?

View file

@ -8,8 +8,8 @@
.procedure-form__columns.container
= form_for @procedure,
url: url_for({ controller: 'administrateurs/procedures', action: :update, id: @procedure.id }),
multipart: true,
html: { class: 'form procedure-form__column--form' } do |f|
html: { class: 'form procedure-form__column--form',
multipart: true } do |f|
%h1.page-title Description

View file

@ -8,8 +8,7 @@
.procedure-form__columns.container
= form_for @procedure,
url: url_for({ controller: 'administrateurs/procedures', action: :create, id: @procedure.id }),
multipart: true,
html: { class: 'form procedure-form__column--form' } do |f|
html: { class: 'form procedure-form__column--form', multipart: true } do |f|
%h1.page-title Nouvelle 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)
= 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)
- else
= render Attachment::ShowComponent.new(attachment: @attachment)

View file

@ -15,9 +15,9 @@
= render Attachment::ShowComponent.new(attachment: @avis.introduction_file.attachment)
%br/
= form_for @avis, url: expert_avis_path(@avis.procedure, @avis), html: { class: 'form', data: { controller: 'persisted-form', persisted_form_key_value: dom_id(@avis) } } do |f|
= 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(form: f, attached_file: @avis.piece_justificative_file, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: @avis.piece_justificative_file, user_can_download: true)
.flex.justify-between.align-baseline
%p.confidentiel.flex

View file

@ -7,7 +7,7 @@
= f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true
%p.tab-title Ajouter une pièce jointe
.form-group
= render Attachment::EditComponent.new(form: f, attached_file: avis.introduction_file, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: avis.introduction_file)
- if linked_dossiers.present?
= f.check_box :invite_linked_dossiers, {}, true, false

View file

@ -3,7 +3,7 @@
%h1.tab-title Inviter des personnes à donner leur avis
%p.avis-notice Les invités pourront consulter le dossier, donner un avis et contribuer au fil de messagerie. Ils ne pourront pas modifier le dossier.
= form_for avis, url: url, html: { class: 'form', data: { controller: 'persisted-form', persisted_form_key_value: dom_id(@avis.dossier, :avis_by_expert) } } do |f|
= form_for avis, url: url, html: { class: 'form', multipart: true, data: { controller: 'persisted-form', persisted_form_key_value: dom_id(@avis.dossier, :avis_by_expert) } } do |f|
= hidden_field_tag 'avis[emails]', nil
= react_component("ComboMultiple",
options: [], selected: [], disabled: [],
@ -14,7 +14,7 @@
= f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true
%p.tab-title Ajouter une pièce jointe
.form-group
= render Attachment::EditComponent.new(form: f, attached_file: avis.introduction_file, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: avis.introduction_file)
- if linked_dossiers.present?
= f.check_box :invite_linked_dossiers, {}, true, false

View file

@ -17,7 +17,7 @@
= form_for @avis, url: instructeur_avis_path(@avis.procedure, @avis), html: { class: 'form' } do |f|
= f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true
= render Attachment::EditComponent.new(object: f.object, attached_file: @avis.piece_justificative_file, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: @avis.piece_justificative_file)
.flex.justify-between.align-baseline
%p.confidentiel.flex

View file

@ -8,7 +8,7 @@
%p#avis-emails-description.avis-notice
Entrez les adresses email des experts à qui vous souhaitez demander un avis
= form_for avis, url: url, html: { class: 'form', data: { controller: 'persisted-form', persisted_form_key_value: dom_id(@dossier, :avis_by_instructeur) } } do |f|
= form_for avis, url: url, html: { class: 'form', multipart: true, data: { controller: 'persisted-form', persisted_form_key_value: dom_id(@dossier, :avis_by_instructeur) } } do |f|
= hidden_field_tag 'avis[emails]', nil
= react_component("ComboMultiple",
options: @dossier.procedure.experts_require_administrateur_invitation ? @experts_emails : [],
@ -21,7 +21,7 @@
= f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true
%p.tab-title Ajouter une pièce jointe
.form-group
= render Attachment::EditComponent.new(object: f.object, attached_file: avis.introduction_file, user_can_destroy: true)
= render Attachment::EditComponent.new(attached_file: avis.introduction_file)
- if linked_dossiers.present?
= f.check_box :invite_linked_dossiers, {}, true, false

View file

@ -355,6 +355,7 @@
- champ = @dossier.champs_public.first
- tdc = @dossier.champs_public.find { _1.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) }.type_de_champ
- avis = Avis.new
- if attachment = ActiveStorage::Attachment.last
- attachment.update(created_at: 1.second.ago)
@ -378,9 +379,18 @@
- attachment.blob.metadata[:virus_scan_result] = ActiveStorage::VirusScanner::INFECTED
= render Attachment::EditComponent.new(champ:, attached_file: Champ.new.piece_justificative_file, attachment:)
%h3.fr-mt-4w New attachment not on Champ
%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 not on Champ
%h3.fr-mt-4w Existing attachment on TypeDeChamp
= 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 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)

View file

@ -1,4 +1,4 @@
= form_for(commentaire, url: form_url, html: { class: 'form', data: { controller: 'persisted-form', persisted_form_key_value: @dossier.present? ? dom_id(@dossier) : dom_id(@procedure, :bulk_message) } }) do |f|
= form_for(commentaire, url: form_url, html: { class: 'form', multipart: true, data: { controller: 'persisted-form', persisted_form_key_value: @dossier.present? ? dom_id(@dossier) : dom_id(@procedure, :bulk_message) } }) do |f|
- dossier = commentaire.dossier
- placeholder = t('views.shared.dossiers.messages.form.write_message_to_administration_placeholder')
- if instructeur_signed_in? || administrateur_signed_in? || expert_signed_in?
@ -8,7 +8,7 @@
- disable_piece_jointe = defined?(disable_piece_jointe) ? disable_piece_jointe : false
%div
- if !disable_piece_jointe
= render Attachment::EditComponent.new(object: f.object, attached_file: commentaire.piece_jointe)
= render Attachment::EditComponent.new(attached_file: commentaire.piece_jointe)
%div
= f.submit t('views.shared.dossiers.messages.form.send_message'), class: 'button primary send', data: { disable: true }

View file

@ -5,9 +5,7 @@ describe 'shared/attachment/_update.html.haml', type: :view do
let(:template) { nil }
subject do
form_for(champ.dossier) do |form|
view.render Attachment::EditComponent.new(form: form, attached_file: attached_file, attachment: attached_file[0], user_can_destroy: true, direct_upload: true)
end
view.render Attachment::EditComponent.new(champ: champ, attached_file: attached_file, attachment: attached_file[0])
end
context 'when there is no attached file' do
@ -52,13 +50,9 @@ describe 'shared/attachment/_update.html.haml', type: :view do
context 'when the user cannot destroy the attachment' do
subject do
form_for(champ.dossier) do |form|
render Attachment::EditComponent.new(form: form,
attached_file: attached_file,
attachment: attached_file[0],
user_can_destroy: user_can_destroy,
direct_upload: true)
end
render Attachment::EditComponent.new(champ: champ,
attached_file: attached_file,
attachment: attached_file[0])
end
it 'hides the Delete button' do