diff --git a/app/assets/javascripts/imageable.js.coffee b/app/assets/javascripts/imageable.js.coffee index dd43ede6d..e029dff52 100644 --- a/app/assets/javascripts/imageable.js.coffee +++ b/app/assets/javascripts/imageable.js.coffee @@ -8,6 +8,10 @@ App.Imageable = $('#nested-image').on 'cocoon:after-remove', (e, item) -> $("#new_image_link").removeClass('hide') + $('#nested-image').on 'cocoon:before-insert', (e, nested_image) -> + if $(".js-image-attachment").length > 0 + $(".js-image-attachment").closest('.image').remove() + $('#nested-image').on 'cocoon:after-insert', (e, nested_image) -> $("#new_image_link").addClass('hide') input = $(nested_image).find('.js-image-attachment') diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 53ab65e61..8af8e45a5 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -50,7 +50,6 @@ module Budgets def create @investment.author = current_user - recover_image_from_cache(@investment) if @investment.save Mailer.budget_investment_created(@investment).deliver_later diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index 5af4dcf60..1bfe6e8cd 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -10,8 +10,6 @@ class ImagesController < ApplicationController end def create - recover_attachments_from_cache - if @image.save flash[:notice] = t "images.actions.create.notice" redirect_to params[:from] @@ -62,10 +60,4 @@ class ImagesController < ApplicationController @image.user = current_user end - def recover_attachments_from_cache - if @image.attachment.blank? && @image.cached_attachment.present? - @image.set_attachment_from_cached_attachment - end - end - end diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index 536ee7e8d..ea8ff22a5 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -26,7 +26,6 @@ class ProposalsController < ApplicationController def create @proposal = Proposal.new(proposal_params.merge(author: current_user)) - recover_image_from_cache(@proposal) if @proposal.save redirect_to share_proposal_path(@proposal), notice: I18n.t('flash.actions.create.proposal') diff --git a/app/helpers/documents_helper.rb b/app/helpers/documents_helper.rb index aa879f4d8..228827516 100644 --- a/app/helpers/documents_helper.rb +++ b/app/helpers/documents_helper.rb @@ -38,7 +38,7 @@ module DocumentsHelper def render_attachment(builder, document) klass = document.errors[:attachment].any? ? "error" : "" - klass = document.persisted? ? " hide" : "" + klass = document.persisted? || document.cached_attachment.present? ? " hide" : "" html = builder.label :attachment, t("documents.upload_document"), class: "button hollow #{klass}" diff --git a/app/helpers/images_helper.rb b/app/helpers/images_helper.rb index 3bebb3957..79b6dbd1e 100644 --- a/app/helpers/images_helper.rb +++ b/app/helpers/images_helper.rb @@ -52,7 +52,7 @@ module ImagesHelper def render_image_attachment(builder, imageable, image) klass = image.errors[:attachment].any? ? "error" : "" - klass = image.persisted? ? " hide" : "" + klass = image.persisted? || image.cached_attachment.present? ? " hide" : "" html = builder.label :attachment, t("images.form.attachment_label"), class: "button hollow #{klass}" diff --git a/app/models/image.rb b/app/models/image.rb index da305957e..b50471795 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -27,9 +27,10 @@ class Image < ActiveRecord::Base validates :user_id, presence: true validates :imageable_id, presence: true, if: -> { persisted? } validates :imageable_type, presence: true, if: -> { persisted? } - validate :validate_image_dimensions, if: -> { attachment.present? && attachment.dirty? } + before_save :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? } + def set_cached_attachment_from_attachment(prefix) self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem attachment.path diff --git a/app/views/images/_image_fields.html.erb b/app/views/images/_image_fields.html.erb index 7a63dab39..d067961d7 100644 --- a/app/views/images/_image_fields.html.erb +++ b/app/views/images/_image_fields.html.erb @@ -2,7 +2,6 @@ <%= f.hidden_field :id %> <%= f.hidden_field :user_id, value: current_user.id %> <%= f.hidden_field :cached_attachment %> - <%= f.hidden_field :_destroy %>
<%= f.text_field :title, placeholder: t("images.new.form.title_placeholder") %>