2022-10-25 14:14:24 +02:00
|
|
|
# Display a widget for uploading, editing and deleting a file attachment
|
|
|
|
class Attachment::MultipleComponent < ApplicationComponent
|
2022-11-22 17:12:16 +01:00
|
|
|
DEFAULT_MAX_ATTACHMENTS = 10
|
|
|
|
|
2022-10-25 14:14:24 +02:00
|
|
|
renders_one :template
|
|
|
|
|
|
|
|
attr_reader :attached_file
|
2022-11-23 12:54:09 +01:00
|
|
|
attr_reader :attachments
|
|
|
|
attr_reader :champ
|
2022-11-28 11:38:28 +01:00
|
|
|
attr_reader :form_object_name
|
2022-11-07 18:30:57 +01:00
|
|
|
attr_reader :max
|
2022-12-08 00:15:05 +01:00
|
|
|
attr_reader :view_as
|
2022-11-23 12:54:09 +01:00
|
|
|
attr_reader :user_can_destroy
|
2022-12-06 11:42:12 +01:00
|
|
|
alias user_can_destroy? user_can_destroy
|
2022-10-25 14:14:24 +02:00
|
|
|
|
|
|
|
delegate :count, :empty?, to: :attachments, prefix: true
|
|
|
|
|
2024-03-25 15:13:14 +01:00
|
|
|
def initialize(champ:, attached_file:, form_object_name: nil, view_as: :link, user_can_destroy: true, max: nil)
|
2022-11-22 17:12:16 +01:00
|
|
|
@champ = champ
|
2022-10-25 14:14:24 +02:00
|
|
|
@attached_file = attached_file
|
2022-11-28 11:38:28 +01:00
|
|
|
@form_object_name = form_object_name
|
2022-12-08 00:15:05 +01:00
|
|
|
@view_as = view_as
|
2022-10-25 14:14:24 +02:00
|
|
|
@user_can_destroy = user_can_destroy
|
2022-11-22 17:12:16 +01:00
|
|
|
@max = max || DEFAULT_MAX_ATTACHMENTS
|
2022-10-25 14:14:24 +02:00
|
|
|
|
|
|
|
@attachments = attached_file.attachments || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_attachment(&block)
|
|
|
|
@attachments.each_with_index(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_attach_next?
|
2022-11-07 18:30:57 +01:00
|
|
|
@attachments.count < @max
|
2022-10-25 14:14:24 +02:00
|
|
|
end
|
|
|
|
|
2022-11-09 12:33:20 +01:00
|
|
|
def empty_component_id
|
2024-03-21 12:01:55 +01:00
|
|
|
"attachment-multiple-empty-#{champ.public_id}"
|
2022-11-09 12:33:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def auto_attach_url
|
2022-11-22 17:12:16 +01:00
|
|
|
helpers.auto_attach_url(champ)
|
2022-10-25 14:14:24 +02:00
|
|
|
end
|
2022-11-23 12:54:09 +01:00
|
|
|
alias poll_url auto_attach_url
|
2022-12-07 18:36:03 +01:00
|
|
|
|
|
|
|
def poll_context
|
|
|
|
return :dossier if champ.present?
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2022-10-25 14:14:24 +02:00
|
|
|
end
|