Code adaptation to work with nested documents and plain documents.

This commit is contained in:
Senén Rodero Rodríguez
2017-08-25 16:06:02 +02:00
parent b2a6189cf5
commit 796e6f9ce1
12 changed files with 401 additions and 281 deletions

View File

@@ -13,13 +13,15 @@ App.Documentable =
formData: null formData: null
add: (e, data) -> add: (e, data) ->
wrapper = $(e.target).parent() wrapper = $(e.target).closest('.document')
index = $(e.target).data('index') index = $(e.target).data('index')
is_nested_document = $(e.target).data('nested-document')
$(wrapper).find('.progress-bar-placeholder').empty() $(wrapper).find('.progress-bar-placeholder').empty()
data.progressBar = $(wrapper).find('.progress-bar-placeholder').html('<div class="progress-bar"><div class="loading-bar uploading"></div></div>') data.progressBar = $(wrapper).find('.progress-bar-placeholder').html('<div class="progress-bar"><div class="loading-bar uploading"></div></div>')
data.formData = { data.formData = {
"document[title]": $(wrapper).find('input.document-title').val() || data.files[0].name "document[title]": $(wrapper).find('input.document-title').val() || data.files[0].name
"index": index "index": index,
"nested_document": is_nested_document
} }
data.submit() data.submit()
@@ -49,22 +51,35 @@ App.Documentable =
$('#new_document_link').show() $('#new_document_link').show()
$('.max-documents-notice').hide() $('.max-documents-notice').hide()
upload: (id, nested_document, result) -> uploadNestedDocument: (id, nested_document, result) ->
$('#' + id).replaceWith(nested_document) $('#' + id).replaceWith(nested_document)
@updateLoadingBar(id, result)
@initialize()
uploadPlainDocument: (id, nested_document, result) ->
$('#' + id).replaceWith(nested_document)
@updateLoadingBar(id, result)
@initialize()
updateLoadingBar: (id, result) ->
if result if result
$('#' + id).find('.loading-bar').addClass 'complete' $('#' + id).find('.loading-bar').addClass 'complete'
else else
$('#' + id).find('.loading-bar').addClass 'errors' $('#' + id).find('.loading-bar').addClass 'errors'
@initialize()
new: (nested_fields) -> new: (nested_fields) ->
$(".documents-list").append(nested_fields) $(".documents-list").append(nested_fields)
@initialize() @initialize()
destroy: (id, notice) -> destroyNestedDocument: (id, notice) ->
$('#' + id).remove() $('#' + id).remove()
@updateNotice(notice) @updateNotice(notice)
replacePlainDocument: (id, notice, plain_document) ->
$('#' + id).replaceWith(plain_document)
@updateNotice(notice)
@initialize()
updateNotice: (notice) -> updateNotice: (notice) ->
if $('[data-alert]').length > 0 if $('[data-alert]').length > 0
$('[data-alert]').replaceWith(notice) $('[data-alert]').replaceWith(notice)

View File

@@ -39,7 +39,7 @@ module DocumentsHelper
def render_destroy_document_link(document, index) def render_destroy_document_link(document, index)
if document.persisted? if document.persisted?
link_to t('documents.form.delete_button'), link_to t('documents.form.delete_button'),
document_path(document, index: index), document_path(document, index: index, nested_document: true),
method: :delete, method: :delete,
remote: true, remote: true,
data: { confirm: t('documents.actions.destroy.confirm') }, data: { confirm: t('documents.actions.destroy.confirm') },
@@ -47,6 +47,7 @@ module DocumentsHelper
elsif !document.persisted? && document.cached_attachment.present? elsif !document.persisted? && document.cached_attachment.present?
link_to t('documents.form.delete_button'), link_to t('documents.form.delete_button'),
destroy_upload_documents_path(path: document.cached_attachment, destroy_upload_documents_path(path: document.cached_attachment,
nested_document: true,
index: index, index: index,
documentable_type: document.documentable_type, documentable_type: document.documentable_type,
documentable_id: document.documentable_id), documentable_id: document.documentable_id),
@@ -68,7 +69,8 @@ module DocumentsHelper
url: document_direct_upload_url(document), url: document_direct_upload_url(document),
cached_attachment_input_field: document_nested_field_id(document, index, :cached_attachment), cached_attachment_input_field: document_nested_field_id(document, index, :cached_attachment),
multiple: false, multiple: false,
index: index index: index,
nested_document: true
}, },
name: document_nested_field_name(document, index, :attachment), name: document_nested_field_name(document, index, :attachment),
id: document_nested_field_id(document, index, :attachment) id: document_nested_field_id(document, index, :attachment)

View File

@@ -22,7 +22,7 @@
</div> </div>
<div class="documents small-12 column" data-max-documents="<%= Budget::Investment.max_documents_allowed %>"> <div class="documents small-12 column" data-max-documents="<%= Budget::Investment.max_documents_allowed %>">
<%= render 'documents/nested_documents', resource: @investment %> <%= render 'documents/nested_documents', documentable: @investment %>
</div> </div>
<div class="small-12 column"> <div class="small-12 column">

View File

@@ -11,33 +11,7 @@
<div class="row"> <div class="row">
<div class="small-12 column"> <%= render 'plain_fields', document: @document %>
<%= f.text_field :title %>
</div>
<div class="small-12 column">
<%= f.hidden_field :cached_attachment %>
<%= f.file_field :attachment,
accept: accepted_content_types_extensions(@document.documentable.class),
label: false,
class: 'document_ajax_attachment',
data: {
url: upload_documents_url(documentable_type: @document.documentable_type, documentable_id: @document.documentable_id),
cached_attachment_input_field: "document_cached_attachment",
multiple: false
} %>
<%= f.label :attachment, t("documents.form.attachment_label"), class: 'button hollow' %>
<div class="progress-bar-placeholder"></div>
<p class="file-name"><%= document_attachment_file_name(@document) %></p>
</div>
<% if @document.errors.has_key?(:attachment) %>
<div class="small-12 column source-option-file">
<div class="attachment-errors">
<small class="error"><%= errors_on_attachment(@document) %></small>
</div>
</div>
<% end %>
<div class="actions small-12 medium-6 large-4 end column"> <div class="actions small-12 medium-6 large-4 end column">
<%= f.submit(t("documents.form.submit_button"), class: "button expanded") %> <%= f.submit(t("documents.form.submit_button"), class: "button expanded") %>

View File

@@ -3,7 +3,7 @@
<p class="help-text"><%= documentables_note(documentable) %></p> <p class="help-text"><%= documentables_note(documentable) %></p>
<% documentable.documents.each_with_index do |document, index| %> <% documentable.documents.each_with_index do |document, index| %>
<%= render 'documents/nested_document', document: document, index: index, documentable: documentable %> <%= render 'documents/nested_fields', document: document, index: index, documentable: documentable %>
<% end %> <% end %>
</div> </div>

View File

@@ -0,0 +1,50 @@
<div id="plain_document_fields" class="document">
<div class="small-12 column">
<%= label_tag :document_title, t("activerecord.attributes.document.title") %>
<%= text_field_tag :document_title, document.title, name: "document[title]", class: "document-title" %>
<% if document.errors.has_key?(:title) %>
<small class="error"><%= document.errors[:title].join(", ") %></small>
<% end %>
</div>
<div class="small-12 column">
<%= hidden_field_tag :cached_attachment, document.cached_attachment, name: "document[cached_attachment]" %>
<%= file_field_tag :attachment,
accept: accepted_content_types_extensions(document.documentable.class),
label: false,
class: 'document_ajax_attachment',
data: {
url: upload_documents_url(documentable_type: document.documentable_type, documentable_id: document.documentable_id),
cached_attachment_input_field: "document_cached_attachment",
multiple: false,
nested_document: false
},
id: "document_attachment",
name: "document[attachment]" %>
<% if document.cached_attachment.blank? %>
<%= label_tag :document_attachment, t("documents.form.attachment_label"), class: 'button hollow' %>
<% else %>
<%= link_to t('documents.form.delete_button'),
destroy_upload_documents_path(path: document.cached_attachment,
nested_document: false,
documentable_type: document.documentable_type,
documentable_id: document.documentable_id),
method: :delete,
remote: true,
class: "delete float-right" %>
<% end %>
<% if document.errors.has_key?(:attachment) %>
<div class="small-12 column source-option-file">
<div class="attachment-errors">
<small class="error"><%= errors_on_attachment(document) %></small>
</div>
</div>
<% end %>
<div class="progress-bar-placeholder"><div class="loading-bar"></div></div>
<p class="file-name"><%= document_attachment_file_name(document) %></p>
</div>
</div>

View File

@@ -1,7 +1,17 @@
App.Documentable.destroy("<%= document_nested_field_wrapper_id(params[:index]) %>", "<%= j render('layouts/flash') %>") <% if params[:nested_document] == "true" %>
<% new_document_link = link_to t("documents.form.add_new_document"),
App.Documentable.destroyNestedDocument("<%= document_nested_field_wrapper_id(params[:index]) %>", "<%= j render('layouts/flash') %>")
<% new_document_link = link_to t("documents.form.add_new_document"),
new_nested_documents_path(documentable_type: @document.documentable_type, index: params[:index]), new_nested_documents_path(documentable_type: @document.documentable_type, index: params[:index]),
remote: true, remote: true,
id: "new_document_link", id: "new_document_link",
class: "button hollow" %> class: "button hollow" %>
App.Documentable.updateNewDocumentButton("<%= j new_document_link %>") App.Documentable.updateNewDocumentButton("<%= j new_document_link %>")
<% else %>
App.Documentable.replacePlainDocument("plain_document_fields",
"<%= j render('layouts/flash') %>",
"<%= j render('plain_fields', document: @document) %>")
<% end %>

View File

@@ -1,10 +1,9 @@
<% <%
nested_fields = render 'documents/nested_document', document: @document, index: params[:index]
new_document_link = link_to t("documents.form.add_new_document"), new_document_link = link_to t("documents.form.add_new_document"),
new_nested_documents_path(documentable_type: params[:documentable_type], index: params[:index].to_i + 1), new_nested_documents_path(documentable_type: params[:documentable_type], index: params[:index].to_i + 1),
remote: true, remote: true,
id: "new_document_link", id: "new_document_link",
class: "button hollow" class: "button hollow"
%> %>
App.Documentable.new("<%= j nested_fields %>") App.Documentable.new("<%= j render('documents/nested_fields', document: @document, index: params[:index]) %>")
App.Documentable.updateNewDocumentButton("<%= j new_document_link %>") App.Documentable.updateNewDocumentButton("<%= j new_document_link %>")

View File

@@ -1,9 +1,12 @@
<% <% if params[:nested_document] == "true" %>
nested_fields = render 'documents/nested_document', document: @document, index: params[:index]
%>
<% if @document.cached_attachment.present? %> App.Documentable.uploadNestedDocument("<%= document_nested_field_wrapper_id(params[:index]) %>",
App.Documentable.upload("<%= document_nested_field_wrapper_id(params[:index]) %>", "<%= j nested_fields %>", true) "<%= j render('documents/nested_fields', document: @document, index: params[:index]) %>",
<%= @document.cached_attachment.present? %>)
<% else %> <% else %>
App.Documentable.upload("<%= document_nested_field_wrapper_id(params[:index]) %>", "<%= j nested_fields %>", false)
App.Documentable.uploadPlainDocument("plain_document_fields",
"<%= j render('documents/plain_fields', document: @document) %>",
<%= @document.cached_attachment.present? %>)
<% end %> <% end %>

View File

@@ -155,7 +155,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
expect(page).to have_content("You must sign in or register to continue.") expect(page).to have_content("You must sign in or register to continue.")
end end
scenario "Should be able for other users" do scenario "Should not be able for other users" do
login_as create(:user) login_as create(:user)
visit new_document_path(documentable_type: documentable.class.name, visit new_document_path(documentable_type: documentable.class.name,
@@ -173,9 +173,97 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
expect(page).to have_selector("h1", text: "Upload document") expect(page).to have_selector("h1", text: "Upload document")
end end
scenario "Should display file name after file selection", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/empty.pdf", make_visible: true
sleep 1
expect(page).to have_content "empty.pdf"
end
scenario "Should not display file name after file selection", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/logo_header.png", make_visible: true
sleep 1
expect(page).not_to have_content "logo_header.jpg"
end
scenario "Should update loading bar style after valid file upload", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/empty.pdf", make_visible: true
sleep 1
expect(page).to have_selector ".loading-bar.complete"
end
scenario "Should update loading bar style after unvalid file upload", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/logo_header.png", make_visible: true
sleep 1
expect(page).to have_selector ".loading-bar.errors"
end
scenario "Should update document title with attachment original file name after file selection if no title defined by user", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/empty.pdf", make_visible: true
sleep 1
expect(find("input[name='document[title]']").value).to eq("empty.pdf")
end
scenario "Should not update document title with attachment original file name after file selection when title already defined by user", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
fill_in :document_title, with: "My custom title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf", make_visible: true
sleep 1
expect(find("input[name='document[title]']").value).to eq("My custom title")
end
scenario "Should update document cached_attachment field after valid file upload", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/empty.pdf", make_visible: true
sleep 1
expect(find("input[name='document[cached_attachment]']", visible: false).value).to include("empty.pdf")
end
scenario "Should not update document cached_attachment field after unvalid file upload", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/logo_header.png", make_visible: true
sleep 1
expect(find("input[name='document[cached_attachment]']", visible: false).value).to eq ""
end
scenario "Should show documentable custom recomentations" do scenario "Should show documentable custom recomentations" do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id, documentable_id: documentable.id,
from: send(documentable_path, arguments)) from: send(documentable_path, arguments))
@@ -191,23 +279,13 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
scenario "Should show validation errors" do scenario "Should show validation errors" do
login_as documentable.author login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name, visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id) documentable_id: documentable.id)
click_on "Upload document" click_on "Upload document"
expect(page).to have_content "2 errors prevented this Document from being saved: " expect(page).to have_content "2 errors prevented this Document from being saved: "
expect(page).to have_selector "small.error", text: "can't be blank", count: 3 expect(page).to have_selector "small.error", text: "can't be blank", count: 2
end
scenario "Should display file name after file selection", :js do
login_as documentable.author
visit new_document_path(documentable_type: documentable.class.name,
documentable_id: documentable.id)
attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
expect(page).to have_content "empty.pdf"
end end
scenario "Should show error notice after unsuccessfull document upload" do scenario "Should show error notice after unsuccessfull document upload" do
@@ -217,6 +295,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
documentable_id: documentable.id, documentable_id: documentable.id,
from: send(documentable_path, arguments)) from: send(documentable_path, arguments))
attach_file :document_attachment, "spec/fixtures/files/empty.pdf" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
sleep 1
click_on "Upload document" click_on "Upload document"
expect(page).to have_content "Cannot create document. Check form errors and try again." expect(page).to have_content "Cannot create document. Check form errors and try again."
@@ -230,6 +309,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
from: send(documentable_path, arguments)) from: send(documentable_path, arguments))
fill_in :document_title, with: "Document title" fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
sleep 1
click_on "Upload document" click_on "Upload document"
expect(page).to have_content "Document was created successfully." expect(page).to have_content "Document was created successfully."
@@ -243,6 +323,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
from: send(documentable_path, arguments)) from: send(documentable_path, arguments))
fill_in :document_title, with: "Document title" fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
sleep 1
click_on "Upload document" click_on "Upload document"
within "##{dom_id(documentable)}" do within "##{dom_id(documentable)}" do
@@ -258,6 +339,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
from: send(documentable_path, arguments)) from: send(documentable_path, arguments))
fill_in :document_title, with: "Document title" fill_in :document_title, with: "Document title"
attach_file :document_attachment, "spec/fixtures/files/empty.pdf" attach_file :document_attachment, "spec/fixtures/files/empty.pdf"
sleep 1
click_on "Upload document" click_on "Upload document"
expect(page).to have_link "Documents (1)" expect(page).to have_link "Documents (1)"

View File

@@ -18,6 +18,8 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
end end
end end
describe "at #{path}" do
scenario "Should show new document link when max documents allowed limit is not reached" do scenario "Should show new document link when max documents allowed limit is not reached" do
login_as user login_as user
visit send(path, arguments) visit send(path, arguments)
@@ -80,9 +82,7 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
login_as user login_as user
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf")
execute_script "$('input[type=\"file\"]').removeClass('show-for-sr');"
attach_file "#{documentable_factory_name}[documents_attributes][0][attachment]", "spec/fixtures/files/empty.pdf"
expect(page).to have_selector ".file-name", text: "empty.pdf" expect(page).to have_selector ".file-name", text: "empty.pdf"
end end
@@ -91,10 +91,7 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
login_as user login_as user
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf")
execute_script "$('input[type=\"file\"]').removeClass('show-for-sr');"
attach_file "#{documentable_factory_name}[documents_attributes][0][attachment]", "spec/fixtures/files/empty.pdf"
sleep 1
expect(find("##{documentable_factory_name}_documents_attributes_0_title").value).to eq "empty.pdf" expect(find("##{documentable_factory_name}_documents_attributes_0_title").value).to eq "empty.pdf"
end end
@@ -104,6 +101,7 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" click_link "Add new document"
sleep 1
fill_in "#{documentable_factory_name}[documents_attributes][0][title]", with: "Title" fill_in "#{documentable_factory_name}[documents_attributes][0][title]", with: "Title"
attach_file("#{documentable_factory_name}[documents_attributes][0][attachment]", "spec/fixtures/files/empty.pdf", make_visible: true) attach_file("#{documentable_factory_name}[documents_attributes][0][attachment]", "spec/fixtures/files/empty.pdf", make_visible: true)
sleep 1 sleep 1
@@ -115,11 +113,8 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
login_as user login_as user
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf")
fill_in "#{documentable_factory_name}[documents_attributes][0][title]", with: "Title" fill_in "#{documentable_factory_name}[documents_attributes][0][title]", with: "Title"
execute_script "$('input[type=\"file\"]').removeClass('show-for-sr');"
attach_file "#{documentable_factory_name}[documents_attributes][0][attachment]", "spec/fixtures/files/empty.pdf"
sleep 1
expect(page).to have_selector ".loading-bar.complete" expect(page).to have_selector ".loading-bar.complete"
end end
@@ -128,11 +123,7 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
login_as user login_as user
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/logo_header.png")
fill_in "#{documentable_factory_name}[documents_attributes][0][title]", with: "Title"
execute_script "$('input[type=\"file\"]').removeClass('show-for-sr');"
attach_file "#{documentable_factory_name}[documents_attributes][0][attachment]", "spec/fixtures/files/logo_header.png"
sleep 1
expect(page).to have_selector ".loading-bar.errors" expect(page).to have_selector ".loading-bar.errors"
end end
@@ -141,11 +132,7 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
login_as user login_as user
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf")
fill_in "#{documentable_factory_name}[documents_attributes][0][title]", with: "Title"
execute_script "$('input[type=\"file\"]').removeClass('show-for-sr');"
attach_file "#{documentable_factory_name}[documents_attributes][0][attachment]", "spec/fixtures/files/empty.pdf"
sleep 1
expect(find("input[name='#{documentable_factory_name}[documents_attributes][0][cached_attachment]']", visible: false).value).to include("empty.pdf") expect(find("input[name='#{documentable_factory_name}[documents_attributes][0][cached_attachment]']", visible: false).value).to include("empty.pdf")
end end
@@ -154,11 +141,7 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
login_as user login_as user
visit send(path, arguments) visit send(path, arguments)
click_link "Add new document" attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/logo_header.png")
fill_in "#{documentable_factory_name}[documents_attributes][0][title]", with: "Title"
execute_script "$('input[type=\"file\"]').removeClass('show-for-sr');"
attach_file "#{documentable_factory_name}[documents_attributes][0][attachment]", "spec/fixtures/files/logo_header.png"
sleep 1
expect(find("input[name='#{documentable_factory_name}[documents_attributes][0][cached_attachment]']", visible: false).value).to eq "" expect(find("input[name='#{documentable_factory_name}[documents_attributes][0][cached_attachment]']", visible: false).value).to eq ""
end end
@@ -248,6 +231,8 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum
expect(page).to have_content "Documents (#{documentable.class.max_documents_allowed})" expect(page).to have_content "Documents (#{documentable.class.max_documents_allowed})"
end end
end
end end
def redirected_to_resource_show_or_navigate_to def redirected_to_resource_show_or_navigate_to