Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Multiple file upload #35

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ group :assets do
gem 'bootswatch-rails'
end

group :development, :test do
group :test do
gem 'database_cleaner'
gem 'factory_girl_rails'
gem 'fakeweb'
gem 'guard-test'
gem 'ruby-prof'
gem 'shoulda-context'
Expand All @@ -28,11 +29,14 @@ group :development do
gem 'capistrano'
gem 'capistrano-unicorn', :require => false
gem 'rvm-capistrano'

gem "better_errors"
gem 'debugger'
end

group :development, :test do
gem 'rb-inotify', '~> 0.9'
end

gem 'devise'
gem 'haml'
gem 'sass'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ GEM
factory_girl_rails (4.2.1)
factory_girl (~> 4.2.0)
railties (>= 3.0.0)
fakeweb (1.3.0)
ffi (1.4.0)
fssm (0.2.10)
guard (1.6.2)
listen (>= 0.6.0)
Expand Down Expand Up @@ -207,6 +209,8 @@ GEM
thor (>= 0.14.6, < 2.0)
raindrops (0.10.0)
rake (10.0.4)
rb-inotify (0.9.0)
ffi (>= 0.5.0)
rdoc (3.12.2)
json (~> 1.4)
redis (3.0.3)
Expand Down Expand Up @@ -297,6 +301,7 @@ DEPENDENCIES
debugger
devise
factory_girl_rails
fakeweb
guard-test
haml
haml-rails
Expand All @@ -308,6 +313,7 @@ DEPENDENCIES
oj
rabl
rails (~> 3.2.8)
rb-inotify (~> 0.9)
ruby-prof
rvm-capistrano
sass
Expand Down
23 changes: 23 additions & 0 deletions app/services/creator/drop_box_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Creator
class DropBoxDocument
def self.can_create?(document_params)
document_params.has_key? :files
end

def initialize(document_params)
@document_params = document_params
end

def document_links
@document_params[:files]
end

def valid?
true
end

def save
Resque.enqueue(DropBoxCreatorTask, document_links)
end
end
end
18 changes: 18 additions & 0 deletions app/services/creator/null_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Creator
class NullDocument
def self.can_create?(document_params)
true
end

def initialize(document_params)
end

def valid?
false
end

def save
false
end
end
end
25 changes: 25 additions & 0 deletions app/services/creator/single_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Creator
class SingleDocument
def self.can_create?(document_params)
document_params.has_key? :file
end

def initialize(document_params)
file = document_params.delete(:file)
@document = Document.new document_params
if file
@document.original_filename = file.original_filename
@document.file = file.path
end
end

def valid?
@document.valid?
end

def save
@document.save
@document.enqueue_process
end
end
end
26 changes: 26 additions & 0 deletions app/services/documents_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class DocumentsCreator
STRATEGIES = %w(SingleDocument DropBoxDocument NullDocument)
def initialize(document_params)
@document_params = document_params
@strategy = pick_strategy
end

def valid?
@strategy.valid?
end

def save
@strategy.save
end

private

def creator(klass)
"Creator::#{klass}".constantize
end

def pick_strategy
strategy_klass = STRATEGIES.find { |klass| creator(klass).can_create?(@document_params) }
creator(strategy_klass).new @document_params
end
end
7 changes: 7 additions & 0 deletions app/services/drop_box_creator_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DropBoxCreatorTask
@queue = :misc

def self.perform(documents_list)
raise "Not implemented yet"
end
end
31 changes: 31 additions & 0 deletions app/services/file_downloader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'uri'
require 'net/http'

class FileDownloader
attr_accessor :file

def initialize(url)
@uri = URI(url)
end

def download
@file = Tempfile.new(filename, encoding: "ascii-8bit")
Net::HTTP.start(host) do |http|
resp = http.get(path)
@file.write(resp.body)
end
@file
end

def filename
@filename ||= @uri.path.split("/").last
end

def path
@uri.path
end

def host
@uri.host
end
end
6 changes: 6 additions & 0 deletions app/views/documents/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@
= f.input :description
= f.input :category, :collection => %w(Alegato Articulo Fundamentos Pericia Sentencia Testimonio Veredicto otro)
= f.input :file, :as => :file
.input.string.optional
%label.string.optional
Or choose some files from your dropbox
%input.string.optional{name: "document[selected-files]", style: "visibility: hidden;", type: "dropbox-chooser", "data-multiselect" => true, "data-link-type" => "direct"}
= f.input :public, :as => :checked_boolean
= f.button :submit

%script#dropboxjs{"data-app-key" => "throly54gy6mhm9", :src => "https://www.dropbox.com/static/api/1/dropins.js", :type => "text/javascript"}
70 changes: 70 additions & 0 deletions test/services/documents_creator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'test_helper'

class DocumentsCreatorTest < ActiveSupport::TestCase
setup do
temp_file = Tempfile.new "document"
temp_file.write("document content")
uploaded_file = Rack::Test::UploadedFile.new(temp_file.path, "text/plain")

@normal_document_params = {
title: "Title",
description: "desciption",
file: uploaded_file
}
@invalid_params = {
title: "Title",
description: "desciption"
}
@multiple_documents_params = {
files: [
"https://dl.dropboxusercontent.com/1/view/q10jqqxrpfyxl4x/2011.pdf",
"https://dl.dropboxusercontent.com/1/view/q10jqqxrpfyxl4x/2012.pdf"
]
}
@single_document_params = {
files: [
"https://dl.dropboxusercontent.com/1/view/q10jqqxrpfyxl4x/2011.pdf"
]
}
end

should "Create a document uploading the file" do
documents_creator = DocumentsCreator.new @normal_document_params

assert documents_creator.valid?
assert_difference("Document.count", 1) do
documents_creator.save
end
end

should "Not create a document with invalid params" do
documents_creator = DocumentsCreator.new @invalid_params

assert_equal false, documents_creator.valid?
assert_equal false, documents_creator.save
end

should "Create a single dropbox document with valid params" do
documents_creator = DocumentsCreator.new @single_document_params

assert documents_creator.valid?
assert_difference(Document.count, 1) do
documents_creator.save
end
end

#should "Create a group of documents" do
#documents_creator = DocumentsCreator.new @multiple_documents_params
#assert documents_creator.valid?
#assert_difference(Document.count, 2) do
#documents_creator.save
#end
#end

#should "Not create document with invalid params" do
#documents_creator = DocumentsCreator.new @invalid_params
#assert_equal documents_creator.valid?, false
#assert_equal documents_creator.save, false
#assert_instance_of documents_creator.errors, ActiveModel::Errors
#end
end
22 changes: 22 additions & 0 deletions test/services/file_downloader_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'test_helper'

class FileDownladerTest < ActiveSupport::TestCase
setup do
@link = "http://examle.org/condenas_jujuy.pdf"
FakeWeb.register_uri = :get, @link, {
body: File.open("test/support/condenas_jujuy.pdf").read,
content_lenth: 1000,
content_type: "application/pdf"
}
end

should "provide filename" do
file_dowloader = FileDownloader.new @link
assert_equal file_dowloader.filename, "condenas_jujuy.pdf"
end

should "Download a file" do
file_dowloader = FileDownloader.new @link
assert_instance_of Tempfile, file_dowloader.download
end
end
Binary file added test/support/condenas_jujuy.pdf
Binary file not shown.