Merge branch 'fix/documentable_multiple_docs'

This commit is contained in:
Bertocq
2018-01-03 18:03:20 +01:00
7 changed files with 49 additions and 35 deletions

View File

@@ -22,11 +22,11 @@ class DirectUpload
# Refactor
@relation = if @resource.respond_to?(:images) &&
((@attachment.present? && !@attachment.content_type.match(/pdf/)) || @cached_attachment.present?)
@resource.images.send("build", relation_attributtes)
@resource.images.send("build", relation_attributtes)
elsif @resource.class.reflections[@resource_relation].macro == :has_one
@resource.send("build_#{resource_relation}", relation_attributtes)
@resource.send("build_#{resource_relation}", relation_attributtes)
else
@resource.send(@resource_relation).build(relation_attributtes)
@resource.send(@resource_relation).build(relation_attributtes)
end
@relation.user = user
@@ -63,4 +63,4 @@ class DirectUpload
}
end
end
end

View File

@@ -2,10 +2,10 @@ class Document < ActiveRecord::Base
include DocumentsHelper
include DocumentablesHelper
has_attached_file :attachment, url: "/system/:class/:prefix/:style/:hash.:extension",
hash_data: ":class/:style",
hash_data: ":class/:style/:custom_hash_data",
use_timestamp: false,
hash_secret: Rails.application.secrets.secret_key_base
attr_accessor :cached_attachment
attr_accessor :cached_attachment, :remove, :original_filename
belongs_to :user
belongs_to :documentable, polymorphic: true
@@ -44,6 +44,10 @@ class Document < ActiveRecord::Base
attachment.instance.prefix(attachment, style)
end
Paperclip.interpolates :custom_hash_data do |attachment, _style|
attachment.instance.custom_hash_data(attachment)
end
def prefix(attachment, _style)
if !attachment.instance.persisted?
"cached_attachments/user/#{attachment.instance.user_id}"
@@ -52,6 +56,17 @@ class Document < ActiveRecord::Base
end
end
def custom_hash_data(attachment)
original_filename = if !attachment.instance.persisted? && attachment.instance.remove
attachment.instance.original_filename
elsif !attachment.instance.persisted?
attachment.instance.attachment_file_name
else
attachment.instance.title
end
"#{attachment.instance.user_id}/#{original_filename}"
end
def humanized_content_type
attachment_content_type.split("/").last.upcase
end
@@ -89,7 +104,9 @@ class Document < ActiveRecord::Base
def remove_cached_attachment
document = Document.new(documentable: documentable,
cached_attachment: cached_attachment,
user: user)
user: user,
remove: true,
original_filename: title)
document.set_attachment_from_cached_attachment
document.attachment.destroy
end

View File

@@ -16,7 +16,7 @@ en:
alert: Cannot destroy document.
confirm: Are you sure you want to delete the document? This action cannot be undone!
buttons:
download_document: Dowload file
download_document: Download file
destroy_document: Destroy
errors:
messages:

BIN
spec/fixtures/files/clippy.pdf vendored Normal file

Binary file not shown.

BIN
spec/fixtures/files/logo.pdf vendored Normal file

Binary file not shown.

View File

@@ -22,7 +22,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
visit send(documentable_path, arguments)
within "#tab-documents" do
expect(page).to have_link("Dowload file")
expect(page).to have_link("Download file")
end
end
@@ -30,7 +30,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
visit send(documentable_path, arguments)
within "#tab-documents" do
expect(page).to have_selector("a[target=_blank]", text: "Dowload file")
expect(page).to have_selector("a[target=_blank]", text: "Download file")
end
end
@@ -38,7 +38,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
visit send(documentable_path, arguments)
within "#tab-documents" do
expect(page).to have_selector("a[rel=nofollow]", text: "Dowload file")
expect(page).to have_selector("a[rel=nofollow]", text: "Download file")
end
end

View File

@@ -85,7 +85,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
login_as user_to_login
visit send(path, arguments)
documentable_attach_new_file(0, "spec/fixtures/files/empty.pdf")
documentable_attach_new_file("spec/fixtures/files/empty.pdf")
expect_document_has_title(0, "empty.pdf")
end
@@ -109,7 +109,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
login_as user_to_login
visit send(path, arguments)
documentable_attach_new_file(0, "spec/fixtures/files/empty.pdf")
documentable_attach_new_file("spec/fixtures/files/empty.pdf")
expect(page).to have_css ".loading-bar.complete"
end
@@ -118,7 +118,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
login_as user_to_login
visit send(path, arguments)
documentable_attach_new_file(0, "spec/fixtures/files/logo_header.png", false)
documentable_attach_new_file("spec/fixtures/files/logo_header.png", false)
expect(page).to have_css ".loading-bar.errors"
end
@@ -127,7 +127,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
login_as user_to_login
visit send(path, arguments)
documentable_attach_new_file(0, "spec/fixtures/files/empty.pdf")
documentable_attach_new_file("spec/fixtures/files/empty.pdf")
expect_document_has_cached_attachment(0, ".pdf")
end
@@ -136,7 +136,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
login_as user_to_login
visit send(path, arguments)
documentable_attach_new_file(0, "spec/fixtures/files/logo_header.png", false)
documentable_attach_new_file("spec/fixtures/files/logo_header.png", false)
expect_document_has_cached_attachment(0, "")
end
@@ -157,7 +157,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
login_as user_to_login
visit send(path, arguments)
documentable_attach_new_file(0, "spec/fixtures/files/empty.pdf")
documentable_attach_new_file("spec/fixtures/files/empty.pdf")
click_link "Remove document"
expect(page).not_to have_css("#nested-documents .document")
@@ -178,7 +178,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
visit send(path, arguments)
send(fill_resource_method_name) if fill_resource_method_name
documentable_attach_new_file(0, "spec/fixtures/files/empty.pdf")
documentable_attach_new_file("spec/fixtures/files/empty.pdf")
click_on submit_button
expect(page).to have_content documentable_success_notice
@@ -189,7 +189,7 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
visit send(path, arguments)
send(fill_resource_method_name) if fill_resource_method_name
documentable_attach_new_file(0, "spec/fixtures/files/empty.pdf")
documentable_attach_new_file("spec/fixtures/files/empty.pdf")
click_on submit_button
documentable_redirected_to_resource_show_or_navigate_to
@@ -205,25 +205,13 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end
scenario "Should show resource with new document after successful creation with maximum allowed uploaded files", :js do
skip "due to weird behaviour"
page.driver.resize_window 1200, 2500
login_as user_to_login
visit send(path, arguments)
send(fill_resource_method_name) if fill_resource_method_name
documentable.class.max_documents_allowed.times.each do
click_link "Add new document"
end
documentable.class.max_documents_allowed.times { documentable_attach_new_file(cycle(Dir.glob('spec/fixtures/files/*.pdf'))) }
documents = all(".document")
documents.each_with_index do |document, index|
document_input = document.find("input[type=file]", visible: false)
attach_file(document_input[:id], "spec/fixtures/files/empty.pdf", make_visible: true)
within all(".document")[index] do
expect(page).to have_css ".loading-bar.complete"
end
end
click_on submit_button
documentable_redirected_to_resource_show_or_navigate_to
@@ -282,11 +270,20 @@ rescue
return
end
def documentable_attach_new_file(index, path, success = true)
def documentable_attach_new_file(path, success = true)
click_link "Add new document"
document = all(".document")[index]
document = all("#new_document").last
document_input = document.find("input[type=file]", visible: false)
attach_file(document_input[:id], path, make_visible: true)
page.execute_script("$('##{document_input[:id]}').css('display','block')")
attach_file(document_input[:id], path, visible: true)
page.execute_script("$('##{document_input[:id]}').css('display','none')")
# Poltergeist is not removing this attribute after file upload at
# https://github.com/teampoltergeist/poltergeist/blob/master/lib/capybara/poltergeist/client/browser.coffee#L187
# making https://github.com/teampoltergeist/poltergeist/blob/master/lib/capybara/poltergeist/client/browser.coffee#L186
# always choose the previous used input.
page.execute_script("$('##{document_input[:id]}').removeAttr('_poltergeist_selected')")
within document do
if success
expect(page).to have_css ".loading-bar.complete"