Extract methods to get objects in attachment forms

This commit is contained in:
Javi Martín
2021-06-20 13:43:30 +02:00
parent 810814486c
commit 5cf96ba03d
4 changed files with 27 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
<div id="<%= dom_id(f.object) %>" class="document direct-upload document-fields nested-fields"> <div id="<%= dom_id(document) %>" class="document direct-upload document-fields nested-fields">
<%= f.hidden_field :id %> <%= f.hidden_field :id %>
<%= f.hidden_field :user_id, value: current_user.id %> <%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :cached_attachment %> <%= f.hidden_field :cached_attachment %>
@@ -9,16 +9,16 @@
<div class="small-12 column attachment-actions"> <div class="small-12 column attachment-actions">
<div class="small-9 column action-add attachment-errors document-attachment"> <div class="small-9 column action-add attachment-errors document-attachment">
<%= render_attachment(f.object) %> <%= render_attachment %>
</div> </div>
<div class="small-3 column action-remove text-right"> <div class="small-3 column action-remove text-right">
<%= render_destroy_document_link(f.object) %> <%= render_destroy_document_link %>
</div> </div>
</div> </div>
<div class="small-6 column"> <div class="small-6 column">
<p class="file-name"> <p class="file-name">
<%= document_attachment_file_name(f.object) %> <%= document_attachment_file_name %>
</p> </p>
</div> </div>

View File

@@ -8,11 +8,15 @@ class Documents::FieldsComponent < ApplicationComponent
private private
def document_attachment_file_name(document) def document
f.object
end
def document_attachment_file_name
document.attachment_file_name document.attachment_file_name
end end
def render_destroy_document_link(document) def render_destroy_document_link
if !document.persisted? && document.cached_attachment.present? if !document.persisted? && document.cached_attachment.present?
link_to t("documents.form.delete_button"), link_to t("documents.form.delete_button"),
direct_upload_destroy_path( direct_upload_destroy_path(
@@ -29,25 +33,25 @@ class Documents::FieldsComponent < ApplicationComponent
end end
end end
def render_attachment(document) def render_attachment
klass = document.persisted? || document.cached_attachment.present? ? " hide" : "" klass = document.persisted? || document.cached_attachment.present? ? " hide" : ""
f.file_field :attachment, f.file_field :attachment,
label_options: { class: "button hollow #{klass}" }, label_options: { class: "button hollow #{klass}" },
accept: accepted_content_types_extensions(document.documentable_type.constantize), accept: accepted_content_types_extensions,
class: "js-document-attachment", class: "js-document-attachment",
data: { data: {
url: document_direct_upload_path(document), url: document_direct_upload_path,
nested_document: true nested_document: true
} }
end end
def document_direct_upload_path(document) def document_direct_upload_path
direct_uploads_path("direct_upload[resource_type]": document.documentable_type, direct_uploads_path("direct_upload[resource_type]": document.documentable_type,
"direct_upload[resource_id]": document.documentable_id, "direct_upload[resource_id]": document.documentable_id,
"direct_upload[resource_relation]": "documents") "direct_upload[resource_relation]": "documents")
end end
def accepted_content_types_extensions(documentable_class) def accepted_content_types_extensions
Setting.accepted_content_types_for("documents").map { |content_type| ".#{content_type}" }.join(",") Setting.accepted_content_types_for("documents").map { |content_type| ".#{content_type}" }.join(",")
end end
end end

View File

@@ -1,4 +1,4 @@
<div id="<%= dom_id(f.object) %>" class="image direct-upload nested-fields"> <div id="<%= dom_id(image) %>" class="image direct-upload nested-fields">
<%= f.hidden_field :id %> <%= f.hidden_field :id %>
<%= f.hidden_field :user_id, value: current_user.id %> <%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :cached_attachment %> <%= f.hidden_field :cached_attachment %>
@@ -7,20 +7,20 @@
<%= f.text_field :title, placeholder: t("images.form.title_placeholder") %> <%= f.text_field :title, placeholder: t("images.form.title_placeholder") %>
</div> </div>
<%= render_image(f.object, :thumb, false) if f.object.attachment.exists? %> <%= render_image(image, :thumb, false) if image.attachment.exists? %>
<div class="small-12 column attachment-actions"> <div class="small-12 column attachment-actions">
<div class="small-9 column action-add attachment-errors image-attachment"> <div class="small-9 column action-add attachment-errors image-attachment">
<%= render_image_attachment(f.object) %> <%= render_image_attachment %>
</div> </div>
<div class="small-3 column action-remove text-right"> <div class="small-3 column action-remove text-right">
<%= render_destroy_image_link(f.object) %> <%= render_destroy_image_link %>
</div> </div>
</div> </div>
<div class="small-6 column"> <div class="small-6 column">
<p class="file-name"> <p class="file-name">
<%= image_attachment_file_name(f.object) %> <%= image_attachment_file_name %>
</p> </p>
</div> </div>

View File

@@ -9,11 +9,15 @@ class Images::FieldsComponent < ApplicationComponent
private private
def image_attachment_file_name(image) def image
f.object
end
def image_attachment_file_name
image.attachment_file_name image.attachment_file_name
end end
def render_destroy_image_link(image) def render_destroy_image_link
if !image.persisted? && image.cached_attachment.present? if !image.persisted? && image.cached_attachment.present?
link_to t("images.form.delete_button"), link_to t("images.form.delete_button"),
direct_upload_destroy_path( direct_upload_destroy_path(
@@ -30,7 +34,7 @@ class Images::FieldsComponent < ApplicationComponent
end end
end end
def render_image_attachment(image) def render_image_attachment
klass = image.persisted? || image.cached_attachment.present? ? " hide" : "" klass = image.persisted? || image.cached_attachment.present? ? " hide" : ""
f.file_field :attachment, f.file_field :attachment,
label_options: { class: "button hollow #{klass}" }, label_options: { class: "button hollow #{klass}" },