76 lines
2.8 KiB
Ruby
76 lines
2.8 KiB
Ruby
module DocumentsHelper
|
|
|
|
def document_note(document)
|
|
t "documents.new.#{document.documentable.class.name.parameterize.underscore}.note",
|
|
title: document.documentable.title
|
|
end
|
|
|
|
def document_attachment_file_name(document)
|
|
document.attachment_file_name
|
|
end
|
|
|
|
def document_errors_on_attachment(document)
|
|
document.errors[:attachment].join(', ') if document.errors.key?(:attachment)
|
|
end
|
|
|
|
def bytes_to_mega(bytes)
|
|
bytes / Numeric::MEGABYTE
|
|
end
|
|
|
|
def document_nested_field_wrapper_id(index)
|
|
"document_#{index}"
|
|
end
|
|
|
|
def render_destroy_document_link(document)
|
|
if document.persisted?
|
|
link_to t('documents.form.delete_button'),
|
|
document_path(document, nested_document: true),
|
|
method: :delete,
|
|
remote: true,
|
|
data: { confirm: t('documents.actions.destroy.confirm') },
|
|
class: "delete remove-document"
|
|
elsif !document.persisted? && document.cached_attachment.present?
|
|
link_to t('documents.form.delete_button'),
|
|
direct_upload_destroy_url("direct_upload[resource_type]": document.documentable_type,
|
|
"direct_upload[resource_id]": document.documentable_id,
|
|
"direct_upload[resource_relation]": "documents",
|
|
"direct_upload[cached_attachment]": document.cached_attachment),
|
|
method: :delete,
|
|
remote: true,
|
|
class: "delete remove-cached-attachment"
|
|
else
|
|
link_to t('documents.form.delete_button'),
|
|
"#",
|
|
class: "delete remove-nested-field"
|
|
end
|
|
end
|
|
|
|
def render_attachment(builder, document)
|
|
html = builder.file_field :attachment,
|
|
label: false,
|
|
accept: accepted_content_types_extensions(document.documentable_type.constantize),
|
|
class: 'js-document-attachment',
|
|
data: {
|
|
url: document_direct_upload_url(document),
|
|
nested_document: true
|
|
}
|
|
if document.attachment.blank? && document.cached_attachment.blank?
|
|
klass = document.errors[:attachment].any? ? "error" : ""
|
|
html += builder.label :attachment, t("documents.upload_document"), class: "button hollow"
|
|
if document.errors[:attachment].any?
|
|
html += content_tag :small, class: "error" do
|
|
document_errors_on_attachment(document)
|
|
end
|
|
end
|
|
end
|
|
html
|
|
end
|
|
|
|
def document_direct_upload_url(document)
|
|
direct_uploads_url("direct_upload[resource_type]": document.documentable_type,
|
|
"direct_upload[resource_id]": document.documentable_id,
|
|
"direct_upload[resource_relation]": "documents")
|
|
end
|
|
|
|
end
|