diff --git a/app/assets/javascripts/documentable.js.coffee b/app/assets/javascripts/documentable.js.coffee index 040229181..c6266d7a9 100644 --- a/app/assets/javascripts/documentable.js.coffee +++ b/app/assets/javascripts/documentable.js.coffee @@ -4,10 +4,14 @@ App.Documentable = $('input.document_ajax_attachment[type=file]').fileupload + paramName : "document[attachment]" + + formData: null + add: (e, data) -> wrapper = $(e.target).parent() $(wrapper).find('.progress-bar-placeholder').empty() - data.progressBar = $('.progress-bar-placeholder').html('
') + data.progressBar = $(wrapper).find('.progress-bar-placeholder').html('') data.submit() change: (e, data) -> @@ -26,7 +30,8 @@ App.Documentable = if result.status == 200 $(data.progressBar).find('.loading-bar').removeClass 'uploading' $(data.progressBar).find('.loading-bar').addClass 'complete' - $('#document_cached_attachment').val result.attachment + inputId = '#' + $(e.target).data('chached-attachment-input-field') + $(inputId).val result.attachment else $(data.progressBar).find('.loading-bar').addClass 'errors' $(data.progressBar).prepend("" + result.msg + "") diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index 62bcdaaac..dc8222b82 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -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 diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 5e0aa1229..2558b3310 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -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 diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index a3d722e92..ce2e1715e 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -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 diff --git a/app/helpers/documentables_helper.rb b/app/helpers/documentables_helper.rb index a9da06506..c6804f20a 100644 --- a/app/helpers/documentables_helper.rb +++ b/app/helpers/documentables_helper.rb @@ -20,7 +20,7 @@ module DocumentablesHelper documentable.class.accepted_content_types .collect{ |content_type| ".#{content_type.split("/").last}" } .join(",") - end + end def humanized_accepted_content_types(documentable) documentable.class.accepted_content_types diff --git a/app/helpers/documents_helper.rb b/app/helpers/documents_helper.rb index 7b681eec5..60c486d52 100644 --- a/app/helpers/documents_helper.rb +++ b/app/helpers/documents_helper.rb @@ -1,7 +1,7 @@ module DocumentsHelper def document_attachment_file_name(document) - document.attachment.attachment_file_name if document.attachment.exists? + document.attachment_file_name if document.attachment.exists? end def errors_on_attachment(document) diff --git a/app/models/document.rb b/app/models/document.rb index a061695cf..abe9c6643 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -17,8 +17,8 @@ class Document < ActiveRecord::Base validate :validate_attachment_size, if: -> { attachment.present? } validates :title, presence: true validates :user, presence: true - validates :documentable_id, presence: true - validates :documentable_type, presence: true + # validates :documentable_id, presence: true + # validates :documentable_type, presence: true def validate_attachment_size if documentable.present? && diff --git a/app/models/proposal.rb b/app/models/proposal.rb index e780dd21b..ee0ba5784 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -13,6 +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 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 313b8a4ab..c57920a37 100644 --- a/app/views/documents/_form.html.erb +++ b/app/views/documents/_form.html.erb @@ -22,10 +22,8 @@ label: false, class: 'document_ajax_attachment show-for-sr', data: { - url: upload_documents_url( - documentable_type: @document.documentable_type, - documentable_id: @document.documentable_id - ), + url: upload_documents_url(documentable_type: @document.documentable_type, documentable_id: @document.documentable_id), + chached_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 232f65ac1..e3d650512 100644 --- a/app/views/proposals/_form.html.erb +++ b/app/views/proposals/_form.html.erb @@ -47,6 +47,35 @@ <%= f.text_field :external_url, placeholder: t("proposals.form.proposal_external_url"), label: false %> +Aquí puedes añadir hasta 3 doucmentos en formato PDF
+ <% @proposal.documents.each_with_index do |document, index| %> +<%= document_attachment_file_name(document) %>
+ <% end %> +