diff --git a/app/helpers/documentables_helper.rb b/app/helpers/documentables_helper.rb index 90b185152..4fd737908 100644 --- a/app/helpers/documentables_helper.rb +++ b/app/helpers/documentables_helper.rb @@ -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 \ No newline at end of file diff --git a/app/helpers/documents_helper.rb b/app/helpers/documents_helper.rb index 819d4a00f..c77b07db4 100644 --- a/app/helpers/documents_helper.rb +++ b/app/helpers/documents_helper.rb @@ -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 diff --git a/app/views/documents/_form.html.erb b/app/views/documents/_form.html.erb index 1d63aaef1..0cb6c5838 100644 --- a/app/views/documents/_form.html.erb +++ b/app/views/documents/_form.html.erb @@ -34,7 +34,7 @@ <% if @document.errors.has_key?(:attachment) %>
- <%= errors_on_attachment(@document)%> + <%= errors_on_attachment(@document) %>
<% end %> diff --git a/app/views/documents/_nested_document.html.erb b/app/views/documents/_nested_document.html.erb index d77e73c35..e9332534b 100644 --- a/app/views/documents/_nested_document.html.erb +++ b/app/views/documents/_nested_document.html.erb @@ -20,49 +20,14 @@ class: "document-title" %> <% if document.errors[:title].any? %> <%= document.errors[:title].join(", ") %> -
<% 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? %> -
- <%= document.errors[:attachment].join(", ") %> -
- <% end %> - <% end %> + <%= render_destroy_document_link(document, index) %>

<%= document_attachment_file_name(document) %>

+
diff --git a/app/views/documents/_nested_documents.html.erb b/app/views/documents/_nested_documents.html.erb index 37f9bb006..69bc0a02f 100644 --- a/app/views/documents/_nested_documents.html.erb +++ b/app/views/documents/_nested_documents.html.erb @@ -1,25 +1,22 @@
<%= label_tag :documents, t("documents.form.title") %> -

<%= documentables_note(resource) %>

+

<%= documentables_note(documentable) %>

- <% 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 %>
-<% 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" %> -
- <%= t "documents.max_documents_allowed_reached_html" %> -
-<% else %> -
- <%= t "documents.max_documents_allowed_reached_html" %> -
<% end %> +
"> + <%= t "documents.max_documents_allowed_reached_html" %> +
+
diff --git a/app/views/proposals/_form.html.erb b/app/views/proposals/_form.html.erb index d1ed7f3b2..73b39dfdb 100644 --- a/app/views/proposals/_form.html.erb +++ b/app/views/proposals/_form.html.erb @@ -47,7 +47,7 @@
- <%= render 'documents/nested_documents', resource: @proposal %> + <%= render 'documents/nested_documents', documentable: @proposal %>