diff --git a/app/assets/javascripts/documentable.js.coffee b/app/assets/javascripts/documentable.js.coffee index c6266d7a9..ba4154266 100644 --- a/app/assets/javascripts/documentable.js.coffee +++ b/app/assets/javascripts/documentable.js.coffee @@ -30,7 +30,7 @@ App.Documentable = if result.status == 200 $(data.progressBar).find('.loading-bar').removeClass 'uploading' $(data.progressBar).find('.loading-bar').addClass 'complete' - inputId = '#' + $(e.target).data('chached-attachment-input-field') + inputId = '#' + $(e.target).data('cached-attachment-input-field') $(inputId).val result.attachment else $(data.progressBar).find('.loading-bar').addClass 'errors' diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index dc8222b82..8a224c326 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -55,11 +55,12 @@ module CommentableActions end def edit + prepare_edit_resource_documents(resource) end def update resource.assign_attributes(strong_params) - resource = parse_documents(resource) + parse_documents(resource) if resource.save redirect_to resource, notice: t("flash.actions.update.#{resource_name.underscore}") else @@ -114,12 +115,20 @@ module CommentableActions def prepare_new_resource_documents if @resource.class == Proposal || @resource.class == Budget::Investment - (0..@resource.class.max_documents_allowed - 1).each do + (1..@resource.class.max_documents_allowed).each do @resource.documents.build end end end + def prepare_edit_resource_documents(resource) + if resource.class == Proposal || resource.class == Budget::Investment + (resource.documents.count + 1 .. resource.class.max_documents_allowed).each do + resource.documents.build + end + end + end + def parse_documents(resource) resource.documents.each do |document| document.user = current_user @@ -127,7 +136,6 @@ module CommentableActions resource.documents = resource.documents.select{|document| document.valid? }.each do |document| document.attachment = File.open(document.cached_attachment) end - resource end end diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index ce2e1715e..f94f7b1e5 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -25,7 +25,7 @@ class ProposalsController < ApplicationController def create @proposal = Proposal.new(proposal_params.merge(author: current_user)) - @proposal = parse_documents(@proposal) + parse_documents(@proposal) if @proposal.save redirect_to share_proposal_path(@proposal), notice: I18n.t('flash.actions.create.proposal') @@ -78,7 +78,7 @@ 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, - documents_attributes: [:cached_attachment, :title] ) + documents_attributes: [:id, :title, :attachment, :cached_attachment ] ) end def retired_params diff --git a/app/models/proposal.rb b/app/models/proposal.rb index ee0ba5784..000c2d42d 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -13,7 +13,7 @@ class Proposal < ActiveRecord::Base documentable max_documents_allowed: 3, max_file_size: 3.megabytes, accepted_content_types: [ "application/pdf" ] - accepts_nested_attributes_for :documents + accepts_nested_attributes_for :documents, allow_destroy: true acts_as_votable acts_as_paranoid column: :hidden_at diff --git a/app/views/documents/_form.html.erb b/app/views/documents/_form.html.erb index c57920a37..2e648a3da 100644 --- a/app/views/documents/_form.html.erb +++ b/app/views/documents/_form.html.erb @@ -23,7 +23,7 @@ class: 'document_ajax_attachment show-for-sr', data: { url: upload_documents_url(documentable_type: @document.documentable_type, documentable_id: @document.documentable_id), - chached_attachment_input_field: "document_cached_attachment", + cached_attachment_input_field: "document_cached_attachment", multiple: false } %> <%= f.label :attachment, t("documents.form.attachment_label"), class: 'button hollow' %> diff --git a/app/views/proposals/_form.html.erb b/app/views/proposals/_form.html.erb index e3d650512..9b1c01ed9 100644 --- a/app/views/proposals/_form.html.erb +++ b/app/views/proposals/_form.html.erb @@ -64,7 +64,7 @@ documentable_type: document_fields.object.documentable_type, documentable_id: document_fields.object.documentable_id ), - chached_attachment_input_field: "proposal_documents_attributes_#{index}_cached_attachment", + cached_attachment_input_field: "proposal_documents_attributes_#{index}_cached_attachment", multiple: false } %> <%= document_fields.label :attachment, t("documents.form.attachment_label"), class: 'button hollow' %>