demarches-normaliennes/app/components/attachment/multiple_component.rb

50 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# Display a widget for uploading, editing and deleting a file attachment
class Attachment::MultipleComponent < ApplicationComponent
DEFAULT_MAX_ATTACHMENTS = 10
renders_one :template
attr_reader :attached_file
attr_reader :attachments
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
delegate :count, :empty?, to: :attachments, prefix: true
2024-02-13 08:03:42 +01:00
def initialize(champ: nil, attached_file:, form_object_name: nil, view_as: :link, user_can_destroy: true, user_can_replace: false, max: nil)
@champ = champ
@attached_file = attached_file
@form_object_name = form_object_name
@view_as = view_as
@user_can_destroy = user_can_destroy
@max = max || DEFAULT_MAX_ATTACHMENTS
@attachments = attached_file.attachments || []
end
def each_attachment(&block)
@attachments.each_with_index(&block)
end
def empty_component_id
2024-02-13 08:03:42 +01:00
champ.present? ? "attachment-multiple-empty-#{champ.public_id}" : "attachment-multiple-empty-generic"
end
def auto_attach_url
2024-02-13 08:03:42 +01:00
champ.present? ? helpers.auto_attach_url(champ) : '#'
end
alias poll_url auto_attach_url
def poll_context
return :dossier if champ.present?
nil
end
end