From f589129ef5143d20e59e8ab5b937762a59005332 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 25 Mar 2024 15:13:14 +0100 Subject: [PATCH] chore(attachment): remove unused replace code --- app/components/attachment/edit_component.rb | 21 ++---------- .../edit_component/edit_component.html.haml | 4 +-- .../attachment/multiple_component.rb | 13 +------- .../multiple_component.html.haml | 6 ++-- .../piece_justificative_component.rb | 4 --- .../piece_justificative_component.html.haml | 2 +- .../titre_identite_component.rb | 4 --- .../titre_identite_component.html.haml | 2 +- .../champs/piece_justificative_controller.rb | 7 ---- .../replace_attachment_controller.ts | 30 ----------------- .../attachment/multiple_component_spec.rb | 12 ------- .../piece_justificative_controller_spec.rb | 32 +------------------ 12 files changed, 11 insertions(+), 126 deletions(-) delete mode 100644 app/javascript/controllers/replace_attachment_controller.ts diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index faee823c4..81803c113 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -4,14 +4,12 @@ class Attachment::EditComponent < ApplicationComponent attr_reader :attachment attr_reader :user_can_destroy alias user_can_destroy? user_can_destroy - attr_reader :user_can_replace - alias user_can_replace? user_can_replace 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, index: 0, as_multiple: false, view_as: :link, user_can_destroy: true, user_can_replace: false, **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 @@ -20,7 +18,6 @@ class Attachment::EditComponent < ApplicationComponent @index = index @view_as = view_as @user_can_destroy = user_can_destroy - @user_can_replace = user_can_replace # attachment passed by kwarg because we don't want a default (nil) value. @attachment = if kwargs.key?(:attachment) @@ -67,7 +64,7 @@ class Attachment::EditComponent < ApplicationComponent def file_field_options track_issue_with_missing_validators if missing_validators? { - class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true, "hidden": persisted?, "fr-mt-2w": user_can_replace?), + class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true, "hidden": persisted?), direct_upload: @direct_upload, id: input_id, aria: { describedby: champ&.describedby_id }, @@ -75,7 +72,6 @@ class Attachment::EditComponent < ApplicationComponent auto_attach_url:, turbo_force: :server }.merge(has_file_size_validator? ? { max_file_size: } : {}) - .merge(user_can_replace? ? { replace_attachment_target: "input" } : {}) }.merge(has_content_type_validator? ? { accept: accept_content_type } : {}) end @@ -118,14 +114,6 @@ class Attachment::EditComponent < ApplicationComponent } end - def replace_controller_attributes - return {} if !persisted? || !user_can_replace? || as_multiple? - - { - "data-controller": 'replace-attachment' - } - end - def retry_button_options { type: 'button', @@ -177,10 +165,7 @@ class Attachment::EditComponent < ApplicationComponent def auto_attach_url return @auto_attach_url if @auto_attach_url.present? - - params = { replace_attachment_id: @attachment.id } if user_can_replace? && @attachment.present? - - return helpers.auto_attach_url(@champ, params) if @champ.present? + return helpers.auto_attach_url(@champ) if @champ.present? nil end diff --git a/app/components/attachment/edit_component/edit_component.html.haml b/app/components/attachment/edit_component/edit_component.html.haml index 298972b2d..5845e8dad 100644 --- a/app/components/attachment/edit_component/edit_component.html.haml +++ b/app/components/attachment/edit_component/edit_component.html.haml @@ -1,12 +1,10 @@ -.attachment.fr-upload-group{ { id: attachment ? dom_id(attachment, :edit) : nil, class: class_names("fr-mb-1w": !(as_multiple? && downloadable?)) }.compact, **replace_controller_attributes } +.attachment.fr-upload-group{ { id: attachment ? dom_id(attachment, :edit) : nil, class: class_names("fr-mb-1w": !(as_multiple? && downloadable?)) }.compact } - if persisted? %div{ id: dom_id(attachment, :persisted_row) } .flex.flex-gap-2{ class: class_names("attachment-error": attachment.virus_scanner_error?) } - if user_can_destroy? = render NestedForms::OwnedButtonComponent.new(formaction: destroy_attachment_path, http_method: :delete, opt: {class: "fr-btn fr-btn--tertiary fr-btn--sm fr-icon-delete-line", title: t(".delete_file", filename: attachment.filename)}) do = t('.delete') - - elsif user_can_replace? - = button_tag t('.replace'), **replace_button_options, class: "fr-btn fr-btn--tertiary fr-btn--sm", title: t(".replace_file", filename: attachment.filename) - if downloadable? = render Dsfr::DownloadComponent.new(attachment:) diff --git a/app/components/attachment/multiple_component.rb b/app/components/attachment/multiple_component.rb index 2a1baa3d7..c0229bf90 100644 --- a/app/components/attachment/multiple_component.rb +++ b/app/components/attachment/multiple_component.rb @@ -12,18 +12,15 @@ class Attachment::MultipleComponent < ApplicationComponent attr_reader :view_as attr_reader :user_can_destroy alias user_can_destroy? user_can_destroy - attr_reader :user_can_replace - alias user_can_replace? user_can_replace delegate :count, :empty?, to: :attachments, prefix: true - def initialize(champ:, attached_file:, form_object_name: nil, view_as: :link, user_can_destroy: true, user_can_replace: false, 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 @view_as = view_as @user_can_destroy = user_can_destroy - @user_can_replace = user_can_replace @max = max || DEFAULT_MAX_ATTACHMENTS @attachments = attached_file.attachments || [] @@ -51,12 +48,4 @@ class Attachment::MultipleComponent < ApplicationComponent nil end - - def replace_controller_attributes - return {} unless user_can_replace? - - { - "data-controller": 'replace-attachment' - } - end end diff --git a/app/components/attachment/multiple_component/multiple_component.html.haml b/app/components/attachment/multiple_component/multiple_component.html.haml index 331bb7fd3..545387758 100644 --- a/app/components/attachment/multiple_component/multiple_component.html.haml +++ b/app/components/attachment/multiple_component/multiple_component.html.haml @@ -1,14 +1,14 @@ -.attachment-multiple{ class: class_names("fr-downloads-group": view_as == :download, "destroyable": user_can_destroy?), **replace_controller_attributes } +.attachment-multiple{ class: class_names("fr-downloads-group": view_as == :download, "destroyable": user_can_destroy?) } = template - if @attachments.size >= 1 %ul.fr-my-1v - each_attachment do |attachment, index| %li{ id: dom_id(attachment) } - = render Attachment::EditComponent.new(champ:, attached_file:, attachment:, index:, as_multiple: true, view_as:, user_can_destroy:, user_can_replace:, 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?), data: { turbo_force: :server } } - = render Attachment::EditComponent.new(champ:, attached_file:, attachment: nil, index: attachments_count, user_can_destroy:, user_can_replace:, form_object_name:) + = render Attachment::EditComponent.new(champ:, attached_file:, attachment: nil, index: attachments_count, user_can_destroy:, form_object_name:) // single poll and refresh message for all attachments = render Attachment::PendingPollComponent.new(attachments: attachments, poll_url:, context: poll_context) diff --git a/app/components/editable_champ/piece_justificative_component.rb b/app/components/editable_champ/piece_justificative_component.rb index a06afe293..95b3ff21a 100644 --- a/app/components/editable_champ/piece_justificative_component.rb +++ b/app/components/editable_champ/piece_justificative_component.rb @@ -15,10 +15,6 @@ class EditableChamp::PieceJustificativeComponent < EditableChamp::EditableChampB !@champ.mandatory? || @champ.dossier.brouillon? end - def user_can_replace? - @champ.mandatory? && @champ.dossier.en_construction? - end - def max [true, nil].include?(@champ.procedure&.piece_justificative_multiple?) ? Attachment::MultipleComponent::DEFAULT_MAX_ATTACHMENTS : 1 end diff --git a/app/components/editable_champ/piece_justificative_component/piece_justificative_component.html.haml b/app/components/editable_champ/piece_justificative_component/piece_justificative_component.html.haml index 322c029aa..4d549cb22 100644 --- a/app/components/editable_champ/piece_justificative_component/piece_justificative_component.html.haml +++ b/app/components/editable_champ/piece_justificative_component/piece_justificative_component.html.haml @@ -1,4 +1,4 @@ -= 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?, user_can_replace: user_can_replace?, 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 } diff --git a/app/components/editable_champ/titre_identite_component.rb b/app/components/editable_champ/titre_identite_component.rb index f29585ae2..c1b916517 100644 --- a/app/components/editable_champ/titre_identite_component.rb +++ b/app/components/editable_champ/titre_identite_component.rb @@ -6,8 +6,4 @@ class EditableChamp::TitreIdentiteComponent < EditableChamp::EditableChampBaseCo def user_can_destroy? !@champ.mandatory? || @champ.dossier.brouillon? end - - def user_can_replace? - @champ.mandatory? && @champ.dossier.en_construction? - end end diff --git a/app/components/editable_champ/titre_identite_component/titre_identite_component.html.haml b/app/components/editable_champ/titre_identite_component/titre_identite_component.html.haml index 22f93966c..cded40cc7 100644 --- a/app/components/editable_champ/titre_identite_component/titre_identite_component.html.haml +++ b/app/components/editable_champ/titre_identite_component/titre_identite_component.html.haml @@ -1,4 +1,4 @@ - if @champ.type_de_champ.piece_justificative_template&.attached? = render partial: "shared/piece_justificative_template", locals: { champ: @champ } = render Attachment::EditComponent.new(champ: @form.object, attached_file: @champ.piece_justificative_file, attachment: @champ.piece_justificative_file[0], form_object_name: @form.object_name, - user_can_destroy: user_can_destroy?, user_can_replace: user_can_replace?) + user_can_destroy: user_can_destroy?) diff --git a/app/controllers/champs/piece_justificative_controller.rb b/app/controllers/champs/piece_justificative_controller.rb index b75ed8f88..20d865492 100644 --- a/app/controllers/champs/piece_justificative_controller.rb +++ b/app/controllers/champs/piece_justificative_controller.rb @@ -31,14 +31,7 @@ class Champs::PieceJustificativeController < ApplicationController save_succeed = nil ActiveStorage::Attachment.transaction do - if params.key?(:replace_attachment_id) - @champ.piece_justificative_file.attachments.find do - _1.id == params[:replace_attachment_id].to_i - end&.destroy - end - @champ.piece_justificative_file.attach(params[:blob_signed_id]) - save_succeed = @champ.save end diff --git a/app/javascript/controllers/replace_attachment_controller.ts b/app/javascript/controllers/replace_attachment_controller.ts deleted file mode 100644 index cefe608cd..000000000 --- a/app/javascript/controllers/replace_attachment_controller.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { ApplicationController } from './application_controller'; -import { show } from '@utils'; - -export class ReplaceAttachmentController extends ApplicationController { - static targets = ['input']; - - declare readonly inputTarget: HTMLInputElement; - - open(event: Event) { - show(this.inputTarget); - this.inputTarget.click(); // opens input prompt - - const target = event.currentTarget as HTMLButtonElement; - - if (target.dataset.autoAttachUrl) { - // set the auto attach url specific to this button to replace the related attachment - this.inputTarget.dataset.originalAutoAttachUrl = - this.inputTarget.dataset.autoAttachUrl; - - this.inputTarget.dataset.autoAttachUrl = target.dataset.autoAttachUrl; - - // reset autoAttachUrl which would add an attachment - // when replace is not finalized - this.on(this.inputTarget, 'cancel', () => { - this.inputTarget.dataset.autoAttachUrl = - this.inputTarget.dataset.originalAutoAttachUrl; - }); - } - } -} diff --git a/spec/components/attachment/multiple_component_spec.rb b/spec/components/attachment/multiple_component_spec.rb index 23f585d55..e5f72fbe7 100644 --- a/spec/components/attachment/multiple_component_spec.rb +++ b/spec/components/attachment/multiple_component_spec.rb @@ -93,18 +93,6 @@ RSpec.describe Attachment::MultipleComponent, type: :component do end end - context 'when user can replace' do - let(:kwargs) { { user_can_replace: true } } - - before do - attach_to_champ(attached_file, champ) - end - - it 'setup controller' do - expect(subject).to have_selector('[data-controller=replace-attachment]').once - end - end - def attach_to_champ(attached_file, champ) attached_file.attach( io: StringIO.new("x" * 2), diff --git a/spec/controllers/champs/piece_justificative_controller_spec.rb b/spec/controllers/champs/piece_justificative_controller_spec.rb index 0799c4c29..26b1a2e21 100644 --- a/spec/controllers/champs/piece_justificative_controller_spec.rb +++ b/spec/controllers/champs/piece_justificative_controller_spec.rb @@ -5,8 +5,6 @@ describe Champs::PieceJustificativeController, type: :controller do let(:champ) { dossier.champs_public.first } describe '#update' do - let(:replace_attachment_id) { nil } - render_views before { sign_in user } @@ -14,8 +12,7 @@ describe Champs::PieceJustificativeController, type: :controller do put :update, params: { position: '1', champ_id: champ.id, - blob_signed_id: file, - replace_attachment_id: + blob_signed_id: file }.compact, format: :turbo_stream end @@ -68,33 +65,6 @@ describe Champs::PieceJustificativeController, type: :controller do expect(response.parsed_body).to eq({ 'errors' => ['La pièce justificative n’est pas d’un type accepté'] }) end end - - context 'replace an attachment' do - let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') } - before { subject } - - context "attachment associated to dossier" do - let(:champ) { create(:champ, :with_piece_justificative_file, dossier:) } - let(:replace_attachment_id) { champ.piece_justificative_file.first.id } - - it "replaces an existing attachment" do - champ.reload - - expect(champ.piece_justificative_file.attachments.count).to eq(1) - expect(champ.piece_justificative_file.attachments.first.filename).to eq("piece_justificative_0.pdf") - end - end - - context "attachment not associated to dossier" do - let(:other_champ) { create(:champ, :with_piece_justificative_file) } - let(:replace_attachment_id) { other_champ.piece_justificative_file.first.id } - - it "add attachment, don't replace attachment" do - expect(champ.reload.piece_justificative_file.attachments.count).to eq(1) - expect(other_champ.reload.piece_justificative_file.attachments.count).to eq(1) - end - end - end end describe '#template' do