Views and helpers refactor

This commit is contained in:
Senén Rodero Rodríguez
2017-08-25 12:08:46 +02:00
parent d67ad6d6d0
commit b2a6189cf5
6 changed files with 76 additions and 52 deletions

View File

@@ -34,4 +34,8 @@ module DocumentablesHelper
max_file_size: max_file_size(documentable)
end
def max_documents_allowed?(documentable)
documentable.documents.count >= documentable.class.max_documents_allowed
end
end

View File

@@ -36,4 +36,62 @@ module DocumentsHelper
"document_#{index}"
end
def render_destroy_document_link(document, index)
if document.persisted?
link_to t('documents.form.delete_button'),
document_path(document, index: index),
method: :delete,
remote: true,
data: { confirm: t('documents.actions.destroy.confirm') },
class: "delete float-right"
elsif !document.persisted? && document.cached_attachment.present?
link_to t('documents.form.delete_button'),
destroy_upload_documents_path(path: document.cached_attachment,
index: index,
documentable_type: document.documentable_type,
documentable_id: document.documentable_id),
method: :delete,
remote: true,
class: "delete float-right"
else
link_to t('documents.form.delete_button'),
"#",
class: "delete float-right remove-document"
end
end
def render_attachment(document, index)
html = file_field_tag :attachment,
accept: accepted_content_types_extensions(document.documentable_type.constantize),
class: 'document_ajax_attachment',
data: {
url: document_direct_upload_url(document),
cached_attachment_input_field: document_nested_field_id(document, index, :cached_attachment),
multiple: false,
index: index
},
name: document_nested_field_name(document, index, :attachment),
id: document_nested_field_id(document, index, :attachment)
if document.attachment.blank? && document.cached_attachment.blank?
klass = document.errors[:attachment].any? ? "error" : ""
html += label_tag document_nested_field_id(document, index, :attachment),
t("documents.form.attachment_label"),
class: "button hollow #{klass}"
if document.errors[:attachment].any?
html += content_tag :small, class: "error" do
errors_on_attachment(document)
end
end
end
html
end
def document_direct_upload_url(document)
upload_documents_url(
documentable_type: document.documentable_type,
documentable_id: document.documentable_id,
format: :js
)
end
end

View File

@@ -34,7 +34,7 @@
<% if @document.errors.has_key?(:attachment) %>
<div class="small-12 column source-option-file">
<div class="attachment-errors">
<small class="error"><%= errors_on_attachment(@document)%></small>
<small class="error"><%= errors_on_attachment(@document) %></small>
</div>
</div>
<% end %>

View File

@@ -20,49 +20,14 @@
class: "document-title" %>
<% if document.errors[:title].any? %>
<small class="error"><%= document.errors[:title].join(", ") %></small>
<br>
<% end %>
<%= file_field_tag :attachment,
accept: accepted_content_types_extensions(document.documentable_type.constantize),
class: 'document_ajax_attachment',
data: {
url: upload_documents_url(
documentable_type: document.documentable_type,
documentable_id: document.documentable_id,
format: :js
),
cached_attachment_input_field: "#{document.documentable.class.name.downcase}_documents_attributes_#{index}_cached_attachment",
multiple: false,
index: index
},
name: document_nested_field_name(document, index, :attachment),
id: document_nested_field_id(document, index, :attachment) %>
<%= render_attachment(document, index) %>
<% if document.persisted? %>
<%= link_to t('documents.form.delete_button'), document_path(document, index: index),
method: :delete,
remote: true,
data: { confirm: t('documents.actions.destroy.confirm') },
class: "delete float-right" if document.persisted? %>
<% elsif !document.persisted? && document.cached_attachment.present? %>
<%= link_to t('documents.form.delete_button'), destroy_upload_documents_path(path: document.cached_attachment, index: index, documentable_type: document.documentable_type, documentable_id: document.documentable_id),
method: :delete,
remote: true,
class: "delete float-right" %>
<% else %>
<%= label_tag document_nested_field_id(document, index, :attachment),
t("documents.form.attachment_label"),
class: "button hollow #{"error" if document.errors[:attachment].any?}" %>
<%= link_to t('documents.form.delete_button'), "#", class: "delete float-right remove-document" %>
<% if document.errors[:attachment].any? %>
<br>
<small class="error"><%= document.errors[:attachment].join(", ") %></small>
<br>
<% end %>
<% end %>
<%= render_destroy_document_link(document, index) %>
<div class="progress-bar-placeholder"><div class="loading-bar"></div></div>
<p class="file-name"><%= document_attachment_file_name(document) %></p>
<hr>
</div>

View File

@@ -1,25 +1,22 @@
<div class="documents-list">
<%= label_tag :documents, t("documents.form.title") %>
<p class="help-text"><%= documentables_note(resource) %></p>
<p class="help-text"><%= documentables_note(documentable) %></p>
<% resource.documents.each_with_index do |document, index| %>
<%= render 'documents/nested_document', document: document, index: index, resource: resource %>
<% documentable.documents.each_with_index do |document, index| %>
<%= render 'documents/nested_document', document: document, index: index, documentable: documentable %>
<% end %>
</div>
<% if resource.documents.count < resource.class.max_documents_allowed %>
<% unless max_documents_allowed?(documentable) %>
<%= link_to t("documents.form.add_new_document"),
new_nested_documents_path(documentable_type: resource.class.name, index: resource.documents.size),
new_nested_documents_path(documentable_type: documentable.class.name, index: documentable.documents.size),
remote: true,
id: "new_document_link",
class: "button hollow" %>
<div class="max-documents-notice callout warning text-center hide">
<%= t "documents.max_documents_allowed_reached_html" %>
</div>
<% else %>
<div class="max-documents-notice callout warning text-center">
<%= t "documents.max_documents_allowed_reached_html" %>
</div>
<% end %>
<div class="max-documents-notice callout warning text-center <%= "hide" unless max_documents_allowed?(documentable) %>">
<%= t "documents.max_documents_allowed_reached_html" %>
</div>
<hr>

View File

@@ -47,7 +47,7 @@
</div>
<div class="documents small-12 column" data-max-documents="<%= Proposal.max_documents_allowed %>">
<%= render 'documents/nested_documents', resource: @proposal %>
<%= render 'documents/nested_documents', documentable: @proposal %>
</div>
<div class="small-12 medium-6 column">