Add nested documents to proposal form.

This commit is contained in:
Senén Rodero Rodríguez
2017-08-23 18:26:05 +02:00
parent 723173ae39
commit cc89907bff
25 changed files with 501 additions and 104 deletions

View File

@@ -1,6 +1,6 @@
class DocumentsController < ApplicationController
before_action :authenticate_user!
before_filter :find_documentable, except: :destroy
before_filter :find_documentable, except: [:destroy]
before_filter :prepare_new_document, only: :new
before_filter :prepare_document_for_creation, only: :create
@@ -22,37 +22,61 @@ class DocumentsController < ApplicationController
end
def destroy
if @document.destroy
flash[:notice] = t "documents.actions.destroy.notice"
else
flash[:alert] = t "documents.actions.destroy.alert"
respond_to do |format|
format.html do
if @document.destroy
flash[:notice] = t "documents.actions.destroy.notice"
else
flash[:alert] = t "documents.actions.destroy.alert"
end
redirect_to params[:from]
end
format.js do
if @document.destroy
flash.now[:notice] = t "documents.actions.destroy.notice"
else
flash.now[:alert] = t "documents.actions.destroy.alert"
end
end
end
redirect_to params[:from]
end
def destroy_upload
@document = Document.new(attachment: File.open(params[:path]))
@document.documentable = @documentable
if @document.attachment.destroy
flash.now[:notice] = t "documents.actions.destroy.notice"
else
flash.now[:alert] = t "documents.actions.destroy.alert"
end
render:destroy
end
def upload
attachment = params[:document][:attachment]
document = Document.new(documentable: @documentable, attachment: attachment.tempfile, title: "faketitle", user: current_user )
# We only are intested in attachment validators. We set some fake data to get attachment errors only
document.valid?
if document.errors[:attachment].empty?
# Move image from tmp to cache
msg = { status: 200, attachment: attachment.tempfile.path }
@document = Document.new(document_params.merge(user: current_user))
@document.documentable = @documentable
if @document.valid?
@document.attachment.save
@document.cached_attachment = @document.attachment.path
else
attachment.tempfile.delete
msg = { status: 422, msg: document.errors[:attachment].join(', ') }
@document.attachment.destroy
end
render :json => msg
end
private
def document_params
params.require(:document).permit(:title, :documentable_type, :documentable_id,
:attachment, :cached_attachment, :user_id)
end
def find_documentable
@documentable = params[:documentable_type].constantize.find_or_initialize_by(id: params[:documentable_id])
end
def prepare_new_document
@document = Document.new(documentable: @documentable, user_id: @documentable.author_id)
@document = Document.new(documentable: @documentable, user_id: current_user.id)
end
def prepare_document_for_creation
@@ -61,11 +85,6 @@ class DocumentsController < ApplicationController
@document.user = current_user
end
def document_params
params.require(:document).permit(:title, :documentable_type, :documentable_id,
:attachment, :cached_attachment)
end
def recover_attachment_from_cache
if @document.attachment.blank? && @document.cached_attachment.present?
@document.attachment = File.open(@document.cached_attachment)