Add _destroy parameter to nested documents
This commit is contained in:
@@ -1,23 +1,25 @@
|
||||
App.Documentable =
|
||||
|
||||
initialize: ->
|
||||
$('#nested-documents').on 'cocoon:after-insert', (e, nested_document) ->
|
||||
input = $(nested_document).find('.js-document-attachment')
|
||||
App.Documentable.initializeDirectUploadInput(input)
|
||||
|
||||
if $(nested_document).closest('#nested-documents').find('.document').length >= $('#nested-documents').data('max-documents-allowed')
|
||||
App.Documentable.lockUploads()
|
||||
|
||||
inputFiles = $('.js-document-attachment')
|
||||
$.each inputFiles, (index, input) ->
|
||||
App.Documentable.initializeDirectUploadInput(input)
|
||||
|
||||
$('#nested-documents').on 'cocoon:after-remove', (e, insertedItem) ->
|
||||
App.Documentable.unlockUploads()
|
||||
|
||||
$('#nested-documents').on 'cocoon:after-insert', (e, nested_document) ->
|
||||
input = $(nested_document).find('.js-document-attachment')
|
||||
App.Documentable.initializeDirectUploadInput(input)
|
||||
|
||||
if $(nested_document).closest('#nested-documents').find('.document:visible').length >= $('#nested-documents').data('max-documents-allowed')
|
||||
App.Documentable.lockUploads()
|
||||
|
||||
initializeDirectUploadInput: (input) ->
|
||||
|
||||
inputData = @buildData([], input)
|
||||
|
||||
@initializeRemoveDocumentLink(input)
|
||||
|
||||
@initializeRemoveCachedDocumentLink(input, inputData)
|
||||
|
||||
$(input).fileupload
|
||||
@@ -139,14 +141,8 @@ App.Documentable =
|
||||
|
||||
if $(data.input).data('nested-document') == true
|
||||
$(data.wrapper).remove()
|
||||
|
||||
initializeRemoveDocumentLink: (input) ->
|
||||
wrapper = $(input).closest(".direct-upload")
|
||||
remove_document_link = $(wrapper).find('a.remove-nested-field')
|
||||
$(remove_document_link).on 'click', (e) ->
|
||||
e.preventDefault()
|
||||
$(wrapper).remove()
|
||||
App.Documentable.unlockUploads()
|
||||
else
|
||||
$(data.wrapper).find('a.remove-cached-attachment').remove()
|
||||
|
||||
initializeRemoveCachedDocumentLink: (input, data) ->
|
||||
wrapper = $(input).closest(".direct-upload")
|
||||
@@ -156,7 +152,5 @@ App.Documentable =
|
||||
e.stopPropagation()
|
||||
App.Documentable.doDeleteCachedAttachmentRequest(this.href, data)
|
||||
|
||||
destroyNestedDocument: (id) ->
|
||||
removeDocument: (id) ->
|
||||
$('#' + id).remove()
|
||||
if $('#nested-documents .document').length < $('#nested-documents').data('max-documents-allowed')
|
||||
App.Documentable.unlockUploads()
|
||||
|
||||
@@ -317,7 +317,8 @@
|
||||
.budget-investment-new,
|
||||
.proposal-form,
|
||||
.proposal-edit,
|
||||
.image-form {
|
||||
.image-form,
|
||||
.document-form {
|
||||
@include upload-image-documents;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
|
||||
|
||||
def question_params
|
||||
params.require(:poll_question).permit(:poll_id, :title, :question, :description, :proposal_id, :valid_answers, :video_url,
|
||||
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id])
|
||||
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy])
|
||||
end
|
||||
|
||||
def search_params
|
||||
|
||||
@@ -111,7 +111,7 @@ module Budgets
|
||||
.permit(:title, :description, :external_url, :heading_id, :tag_list,
|
||||
:organization_name, :location, :terms_of_service,
|
||||
image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id],
|
||||
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id])
|
||||
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy])
|
||||
end
|
||||
|
||||
def load_ballot
|
||||
|
||||
@@ -81,7 +81,7 @@ class ProposalsController < ApplicationController
|
||||
params.require(:proposal).permit(:title, :question, :summary, :description, :external_url, :video_url,
|
||||
:responsible_name, :tag_list, :terms_of_service, :geozone_id,
|
||||
image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id],
|
||||
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id] )
|
||||
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy] )
|
||||
end
|
||||
|
||||
def retired_params
|
||||
|
||||
@@ -21,15 +21,8 @@ module DocumentsHelper
|
||||
"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?
|
||||
def render_destroy_document_link(builder, document)
|
||||
if !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,
|
||||
@@ -39,9 +32,7 @@ module DocumentsHelper
|
||||
remote: true,
|
||||
class: "delete remove-cached-attachment"
|
||||
else
|
||||
link_to t('documents.form.delete_button'),
|
||||
"#",
|
||||
class: "delete remove-nested-field"
|
||||
link_to_remove_association t('documents.form.delete_button'), builder, class: "delete remove-document"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
ckeditor: { language: I18n.locale } %>
|
||||
</div>
|
||||
|
||||
<div class="documents small-12" data-max-documents="<%= Poll::Question.max_documents_allowed %>">
|
||||
<%= render 'documents/nested_documents', documentable: @question %>
|
||||
<div class="documents small-12">
|
||||
<%= render 'documents/nested_documents', documentable: @question, f: f %>
|
||||
</div>
|
||||
|
||||
<div class="small-12">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<div id="<%= dom_id(f.object) %>" class="document direct-upload">
|
||||
<div id="<%= dom_id(f.object) %>" class="document direct-upload document-fields nested-fields">
|
||||
<%= f.hidden_field :id %>
|
||||
<%= f.hidden_field :user_id, value: current_user.id %>
|
||||
<%= f.hidden_field :cached_attachment %>
|
||||
@@ -12,7 +12,7 @@
|
||||
<%= render_attachment(f, f.object) %>
|
||||
</div>
|
||||
<div class="small-3 column action-remove text-right">
|
||||
<%= render_destroy_document_link(f.object) %>
|
||||
<%= render_destroy_document_link(f, f.object) %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
documentable_id: @document.documentable_id,
|
||||
from: params[:from]
|
||||
),
|
||||
html: { multipart: true, class: "documentable" } do |f| %>
|
||||
html: { multipart: true, class: "documentable document-form" } do |f| %>
|
||||
|
||||
<%= render 'shared/errors', resource: @document %>
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
App.Documentable.destroyNestedDocument("<%= dom_id(@document) %>")
|
||||
App.Documentable.removeDocument("<%= dom_id(@document) %>")
|
||||
|
||||
Reference in New Issue
Block a user