Move method 'recover_image_from_cache' from imageable controllers to image.

This commit is contained in:
Senén Rodero Rodríguez
2017-09-23 23:09:31 +02:00
parent c22637c349
commit 0a86727c6a
8 changed files with 8 additions and 14 deletions

View File

@@ -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')

View File

@@ -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

View File

@@ -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

View File

@@ -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')

View File

@@ -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}"

View File

@@ -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}"

View File

@@ -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

View File

@@ -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 %>
<div class="small-12 column title">
<%= f.text_field :title, placeholder: t("images.new.form.title_placeholder") %>