diff --git a/app/assets/javascripts/documentable.js.coffee b/app/assets/javascripts/documentable.js.coffee index e97754609..040229181 100644 --- a/app/assets/javascripts/documentable.js.coffee +++ b/app/assets/javascripts/documentable.js.coffee @@ -1,64 +1,34 @@ App.Documentable = initialize: -> - $('input#document_attachment[type=file]').fileupload + + $('input.document_ajax_attachment[type=file]').fileupload add: (e, data) -> - data.progressBar = $('
').insertAfter('#progress-bar') - options = - extension: data.files[0].name.match(/(\.\w+)?$/)[0] - _: Date.now() - direct_upload_url = $(this).closest('form').data('direct-upload-url') - console.log direct_upload_url + wrapper = $(e.target).parent() + $(wrapper).find('.progress-bar-placeholder').empty() + data.progressBar = $('.progress-bar-placeholder').html('') + data.submit() - fileData = new FormData(); - fileData.append('file', data.fileInput[0].files[0]); - - console.log direct_upload_url - - $.getJSON direct_upload_url, options, (result) -> - data.formData = result['fields'] - data.url = result['url'] - data.paramName = 'file' - data.submit() - return - - # $.ajax - # url: direct_upload_url - # type: 'POST' - # data: fileData - # cache: false - # contentType: false - # processData: false - # xhr: -> - # console.log 'Progress init' - # myXhr = $.ajaxSettings.xhr() - # if myXhr.upload - # # For handling the progress of the upload - # myXhr.upload.addEventListener 'progress', ((e) -> - # console.log 'progress listener called' - # ), false - # - # myXhr.upload.addEventListener 'complete', ((e) -> - # console.log 'complete listener called' - # return - # ), false - # myXhr + change: (e, data) -> + wrapper = $(e.target).parent() + $.each(data.files, (index, file)-> + $(wrapper).find('.file-name').text(file.name) + ) progress: (e, data) -> progress = parseInt(data.loaded / data.total * 100, 10) - $('.loading-bar').css 'width', progress + '%' + $(data.progressBar).find('.loading-bar').css 'width', progress + '%' return done: (e, data) -> - $('.loading-bar').removeClass 'uploading' - $('.loading-bar').addClass 'complete' - image = - id: data.formData.key - storage: 'cache' - metadata: - size: data.files[0].size - filename: data.files[0].name.match(/[^\/\\]*$/)[0] - mime_type: data.files[0].type - $('#cached_attachment_data').val JSON.stringify(image) - return \ No newline at end of file + result = data.response().result + if result.status == 200 + $(data.progressBar).find('.loading-bar').removeClass 'uploading' + $(data.progressBar).find('.loading-bar').addClass 'complete' + $('#document_cached_attachment').val result.attachment + else + $(data.progressBar).find('.loading-bar').addClass 'errors' + $(data.progressBar).prepend("" + result.msg + "") + return + diff --git a/app/assets/javascripts/forms.js.coffee b/app/assets/javascripts/forms.js.coffee index 58e0894b0..1c4cee919 100644 --- a/app/assets/javascripts/forms.js.coffee +++ b/app/assets/javascripts/forms.js.coffee @@ -28,8 +28,7 @@ App.Forms = i = 0 while i < element.length element[i].addEventListener 'change', -> - idButton = $(this) - idButton.closest('.file-name').find('p').text(@files[0].name) + $(element).parent().find('.file-name').text(@files[0].name) return i++ diff --git a/app/assets/stylesheets/documentable.scss b/app/assets/stylesheets/documentable.scss index 5c26f9a43..9015409b0 100644 --- a/app/assets/stylesheets/documentable.scss +++ b/app/assets/stylesheets/documentable.scss @@ -7,18 +7,21 @@ .loading-bar { height: 5px; - min-width: 10px; + width: 0px; z-index: 666000666; -webkit-transition:width 500ms ease-out, height 500ms ease-out; -moz-transition:width 500ms ease-out, height 500ms ease-out; -o-transition:width 500ms ease-out, height 500ms ease-out; transition:width 500ms ease-out, height 500ms ease-out; &.uploading{ - background-color: #f00; + background-color: #bbb; } &.complete{ background-color: #0f0; } + &.errors{ + background-color: #f00; + } } .loading-bar.no-transition { diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index cc482c695..5e0aa1229 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -15,6 +15,7 @@ class DocumentsController < ApplicationController end def create + recover_attachment_from_cache if @document.save flash[:notice] = t "documents.actions.create.notice" redirect_to params[:from] @@ -34,13 +35,13 @@ class DocumentsController < ApplicationController end def upload - document = Document.new(documentable: @documentable, attachment: params[:file].tempfile, title: "faketitle", user: current_user ) - debugger + document = Document.new(documentable: @documentable, attachment: params[:document][: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? - # TODO: Store file on tmp cache - msg = { status: 200, msg: "OK" } + # Move image from tmp to cache + msg = { status: 200, attachment: params[:document][:attachment].path } else + params[:document][:attachment].tempfile.delete msg = { status: 422, msg: document.errors[:attachment].join(', ') } end render :json => msg @@ -63,7 +64,14 @@ class DocumentsController < ApplicationController end def document_params - params.require(:document).permit(:title, :documentable_type, :documentable_id, :attachment) + 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) + end end def validate_upload diff --git a/app/models/document.rb b/app/models/document.rb index 3fcef364c..a061695cf 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -2,11 +2,14 @@ class Document < ActiveRecord::Base include DocumentsHelper include DocumentablesHelper has_attached_file :attachment + attr_accessor :cached_attachment belongs_to :user belongs_to :documentable, polymorphic: true - validates_attachment :attachment, presence: true + # validates_attachment :attachment, presence: true + validate :attachment_presence + # validates :attachment_prensence # Disable paperclip security validation due to polymorphic configuration # Paperclip do not allow to user Procs on valiations definition do_not_validate_attachment_file_type :attachment @@ -35,4 +38,10 @@ class Document < ActiveRecord::Base end end + def attachment_presence + if attachment.blank? && cached_attachment.blank? + errors[:attachment] = I18n.t("errors.messages.blank") + end + end + end diff --git a/app/views/documents/_form.html.erb b/app/views/documents/_form.html.erb index a4fefa694..313b8a4ab 100644 --- a/app/views/documents/_form.html.erb +++ b/app/views/documents/_form.html.erb @@ -16,12 +16,21 @@<%= document_attachment_file_name(@document) %>
+ +<%= document_attachment_file_name(@document) %>