Accept nested attributes for documents on proposals. Adapt documentable js file to allow many input files at the same page.
This commit is contained in:
@@ -28,6 +28,7 @@ module CommentableActions
|
||||
|
||||
def new
|
||||
@resource = resource_model.new
|
||||
prepare_new_resource_documents
|
||||
set_geozone
|
||||
set_resource_instance
|
||||
end
|
||||
@@ -58,6 +59,7 @@ module CommentableActions
|
||||
|
||||
def update
|
||||
resource.assign_attributes(strong_params)
|
||||
resource = parse_documents(resource)
|
||||
if resource.save
|
||||
redirect_to resource, notice: t("flash.actions.update.#{resource_name.underscore}")
|
||||
else
|
||||
@@ -110,4 +112,22 @@ module CommentableActions
|
||||
nil
|
||||
end
|
||||
|
||||
def prepare_new_resource_documents
|
||||
if @resource.class == Proposal || @resource.class == Budget::Investment
|
||||
(0..@resource.class.max_documents_allowed - 1).each do
|
||||
@resource.documents.build
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_documents(resource)
|
||||
resource.documents.each do |document|
|
||||
document.user = current_user
|
||||
end
|
||||
resource.documents = resource.documents.select{|document| document.valid? }.each do |document|
|
||||
document.attachment = File.open(document.cached_attachment)
|
||||
end
|
||||
resource
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -4,13 +4,9 @@ class DocumentsController < ApplicationController
|
||||
before_filter :prepare_new_document, only: :new
|
||||
before_filter :prepare_document_for_creation, only: :create
|
||||
|
||||
before_filter :validate_upload, only: :upload
|
||||
load_and_authorize_resource :except => [:upload]
|
||||
skip_authorization_check :only => [:upload]
|
||||
|
||||
def preload
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
@@ -35,13 +31,15 @@ class DocumentsController < ApplicationController
|
||||
end
|
||||
|
||||
def upload
|
||||
document = Document.new(documentable: @documentable, attachment: params[:document][:attachment].tempfile, title: "faketitle", user: current_user )
|
||||
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
|
||||
if document.valid?
|
||||
document.valid?
|
||||
if document.errors[:attachment].empty?
|
||||
# Move image from tmp to cache
|
||||
msg = { status: 200, attachment: params[:document][:attachment].path }
|
||||
msg = { status: 200, attachment: attachment.tempfile.path }
|
||||
else
|
||||
params[:document][:attachment].tempfile.delete
|
||||
attachment.tempfile.delete
|
||||
msg = { status: 422, msg: document.errors[:attachment].join(', ') }
|
||||
end
|
||||
render :json => msg
|
||||
@@ -50,7 +48,7 @@ class DocumentsController < ApplicationController
|
||||
private
|
||||
|
||||
def find_documentable
|
||||
@documentable = params[:documentable_type].constantize.find(params[:documentable_id])
|
||||
@documentable = params[:documentable_type].constantize.find_or_initialize_by(id: params[:documentable_id])
|
||||
end
|
||||
|
||||
def prepare_new_document
|
||||
@@ -74,8 +72,4 @@ class DocumentsController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def validate_upload
|
||||
if @documentable.present?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,6 +25,7 @@ class ProposalsController < ApplicationController
|
||||
|
||||
def create
|
||||
@proposal = Proposal.new(proposal_params.merge(author: current_user))
|
||||
@proposal = parse_documents(@proposal)
|
||||
|
||||
if @proposal.save
|
||||
redirect_to share_proposal_path(@proposal), notice: I18n.t('flash.actions.create.proposal')
|
||||
@@ -76,7 +77,8 @@ 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)
|
||||
:responsible_name, :tag_list, :terms_of_service, :geozone_id,
|
||||
documents_attributes: [:cached_attachment, :title] )
|
||||
end
|
||||
|
||||
def retired_params
|
||||
@@ -122,4 +124,5 @@ class ProposalsController < ApplicationController
|
||||
def load_successful_proposals
|
||||
@proposal_successful_exists = Proposal.successful.exists?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user