diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index f642f7730..185c5cc71 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -22,7 +22,6 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController def create @question.author = @question.proposal.try(:author) || current_user - recover_documents_from_cache(@question) if @question.save redirect_to admin_question_path(@question) @@ -39,10 +38,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController end def update - @question.assign_attributes(question_params) - recover_documents_from_cache(@question) - - if @question.save + if @question.update(question_params) redirect_to admin_question_path(@question), notice: t("flash.actions.save_changes.notice") else render :edit diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 884c2fdab..53ab65e61 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_documents_from_cache(@investment) recover_image_from_cache(@investment) if @investment.save diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index e7c115a7a..44c98e760 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -58,7 +58,6 @@ module CommentableActions def update resource.assign_attributes(strong_params) - recover_documents_from_cache(resource) recover_image_from_cache(resource) if resource.save @@ -113,13 +112,6 @@ module CommentableActions nil end - def recover_documents_from_cache(resource) - return false unless resource.try(:documents) - resource.documents = resource.documents.each do |document| - document.set_attachment_from_cached_attachment if document.cached_attachment.present? - end - end - def recover_image_from_cache(resource) return false unless resource.try(:image) diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 7d90feea5..73e90eb68 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -10,8 +10,6 @@ class DocumentsController < ApplicationController end def create - recover_attachments_from_cache - if @document.save flash[:notice] = t "documents.actions.create.notice" redirect_to params[:from] @@ -62,10 +60,4 @@ class DocumentsController < ApplicationController @document.user = current_user end - def recover_attachments_from_cache - if @document.attachment.blank? && @document.cached_attachment.present? - @document.set_attachment_from_cached_attachment - end - end - end diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index 5d693e218..536ee7e8d 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_documents_from_cache(@proposal) recover_image_from_cache(@proposal) if @proposal.save diff --git a/app/models/document.rb b/app/models/document.rb index ff19cf390..d46d9e30a 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -21,6 +21,8 @@ class Document < ActiveRecord::Base validates :documentable_id, presence: true, if: -> { persisted? } validates :documentable_type, presence: true, if: -> { persisted? } + 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