Add _destroy parameter to nested image

This commit is contained in:
Senén Rodero Rodríguez
2017-09-23 18:33:30 +02:00
parent 77f63a4c44
commit d98d1312ca
6 changed files with 21 additions and 35 deletions

View File

@@ -1,23 +1,22 @@
App.Imageable =
initialize: ->
$('#nested-image').on 'cocoon:after-insert', (e, nested_image) ->
input = $(nested_image).find('.js-image-attachment')
App.Imageable.initializeDirectUploadInput(input)
inputFiles = $('.js-image-attachment')
$.each inputFiles, (index, input) ->
App.Imageable.initializeDirectUploadInput(input)
$("#new_image_link").on 'click', ->
$(this).addClass('hide')
$('#nested-image').on 'cocoon:after-remove', (e, item) ->
$("#new_image_link").removeClass('hide')
$('#nested-image').on 'cocoon:after-insert', (e, nested_image) ->
$("#new_image_link").addClass('hide')
input = $(nested_image).find('.js-image-attachment')
App.Imageable.initializeDirectUploadInput(input)
initializeDirectUploadInput: (input) ->
inputData = @buildData([], input)
@initializeRemoveImageLink(input)
@initializeRemoveCachedImageLink(input, inputData)
$(input).fileupload
@@ -142,18 +141,13 @@ App.Imageable =
$('#new_image_link').removeClass('hide')
$(data.wrapper).find(".attachment-actions").addClass('small-12').removeClass('small-6 float-right')
$(data.wrapper).find(".attachment-actions .action-remove").addClass('small-3').removeClass('small-12')
if $(data.input).data('nested-image') == true
$(data.wrapper).remove()
else
$(data.destroyAttachmentLinkContainer).find('a.delete').remove()
initializeRemoveImageLink: (input) ->
wrapper = $(input).closest(".direct-upload")
remove_image_link = $(wrapper).find('a.remove-nested-field')
$(remove_image_link).on 'click', (e) ->
e.preventDefault()
$(wrapper).remove()
$('#new_image_link').removeClass('hide')
$(data.wrapper).find('a.remove-cached-attachment').remove()
initializeRemoveCachedImageLink: (input, data) ->
wrapper = $(input).closest(".direct-upload")
@@ -163,6 +157,6 @@ App.Imageable =
e.stopPropagation()
App.Imageable.doDeleteCachedAttachmentRequest(this.href, data)
destroyNestedImage: (id, notice) ->
removeImage: (id) ->
$('#' + id).remove()
$("#new_image_link").removeClass('hide')

View File

@@ -110,7 +110,7 @@ module Budgets
params.require(:budget_investment)
.permit(:title, :description, :external_url, :heading_id, :tag_list,
:organization_name, :location, :terms_of_service,
image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id],
image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy],
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy])
end

View File

@@ -80,7 +80,7 @@ class ProposalsController < ApplicationController
def proposal_params
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],
image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy],
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy] )
end

View File

@@ -35,15 +35,8 @@ module ImagesHelper
image.persisted? ? "persisted-image" : "cached-image"
end
def render_destroy_image_link(image)
if image.persisted?
link_to t('images.form.delete_button'),
image_path(image, nested_image: true),
method: :delete,
remote: true,
data: { confirm: t('images.actions.destroy.confirm') },
class: "delete remove-image"
elsif !image.persisted? && image.cached_attachment.present?
def render_destroy_image_link(builder, image)
if !image.persisted? && image.cached_attachment.present?
link_to t('images.form.delete_button'),
direct_upload_destroy_url("direct_upload[resource_type]": image.imageable_type,
"direct_upload[resource_id]": image.imageable_id,
@@ -53,9 +46,7 @@ module ImagesHelper
remote: true,
class: "delete remove-cached-attachment"
else
link_to t('images.form.delete_button'),
"#",
class: "delete remove-nested-field"
link_to_remove_association t('images.form.delete_button'), builder, class: "delete remove-image"
end
end

View File

@@ -1,7 +1,8 @@
<div id="<%= dom_id(f.object) %>" class="image direct-upload">
<div id="<%= dom_id(f.object) %>" class="image direct-upload nested-fields">
<%= f.hidden_field :id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :cached_attachment %>
<%= f.hidden_field :_destroy %>
<div class="small-12 column title">
<%= f.text_field :title, placeholder: t("images.new.form.title_placeholder") %>
@@ -14,7 +15,7 @@
<%= render_image_attachment(f, imageable, f.object) %>
</div>
<div class="small-3 column action-remove text-right">
<%= render_destroy_image_link(f.object) %>
<%= render_destroy_image_link(f, f.object) %>
</div>
</div>

View File

@@ -1 +1 @@
App.Imageable.destroyNestedImage("<%= dom_id(@image) %>")
App.Imageable.removeImage("<%= dom_id(@image) %>")