refactor(attachment): update Attachment::EditComponent everywhere
This commit is contained in:
parent
b13c5e56f6
commit
0a114270af
22 changed files with 109 additions and 65 deletions
|
@ -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
|
||||
}
|
||||
)
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue