45 lines
1 KiB
Ruby
45 lines
1 KiB
Ruby
# Display a widget for uploading, editing and deleting a file attachment
|
|
class Attachment::MultipleComponent < ApplicationComponent
|
|
renders_one :template
|
|
|
|
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)
|
|
@form = form
|
|
@attached_file = attached_file
|
|
@user_can_destroy = user_can_destroy
|
|
@direct_upload = direct_upload
|
|
@id = id
|
|
@max = max || 10
|
|
|
|
@attachments = attached_file.attachments || []
|
|
end
|
|
|
|
def each_attachment(&block)
|
|
@attachments.each_with_index(&block)
|
|
end
|
|
|
|
def can_attach_next?
|
|
return false if @attachments.empty?
|
|
return false if !@attachments.last.persisted?
|
|
|
|
@attachments.count < @max
|
|
end
|
|
|
|
def stimulus_controller_name
|
|
"attachment-multiple"
|
|
end
|
|
|
|
private
|
|
|
|
def attachments
|
|
@attachments
|
|
end
|
|
end
|