diff --git a/app/assets/javascripts/documentable.js.coffee b/app/assets/javascripts/documentable.js.coffee index b4c9139f9..f86d68433 100644 --- a/app/assets/javascripts/documentable.js.coffee +++ b/app/assets/javascripts/documentable.js.coffee @@ -13,13 +13,15 @@ App.Documentable = formData: null add: (e, data) -> - wrapper = $(e.target).parent() + wrapper = $(e.target).closest('.document') index = $(e.target).data('index') + is_nested_document = $(e.target).data('nested-document') $(wrapper).find('.progress-bar-placeholder').empty() data.progressBar = $(wrapper).find('.progress-bar-placeholder').html('
') data.formData = { "document[title]": $(wrapper).find('input.document-title').val() || data.files[0].name - "index": index + "index": index, + "nested_document": is_nested_document } data.submit() @@ -49,22 +51,35 @@ App.Documentable = $('#new_document_link').show() $('.max-documents-notice').hide() - upload: (id, nested_document, result) -> + uploadNestedDocument: (id, nested_document, result) -> $('#' + 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 $('#' + id).find('.loading-bar').addClass 'complete' else $('#' + id).find('.loading-bar').addClass 'errors' - @initialize() new: (nested_fields) -> $(".documents-list").append(nested_fields) @initialize() - destroy: (id, notice) -> + destroyNestedDocument: (id, notice) -> $('#' + id).remove() @updateNotice(notice) + replacePlainDocument: (id, notice, plain_document) -> + $('#' + id).replaceWith(plain_document) + @updateNotice(notice) + @initialize() + updateNotice: (notice) -> if $('[data-alert]').length > 0 $('[data-alert]').replaceWith(notice) diff --git a/app/helpers/documents_helper.rb b/app/helpers/documents_helper.rb index c77b07db4..cf80b0261 100644 --- a/app/helpers/documents_helper.rb +++ b/app/helpers/documents_helper.rb @@ -39,7 +39,7 @@ module DocumentsHelper def render_destroy_document_link(document, index) if document.persisted? link_to t('documents.form.delete_button'), - document_path(document, index: index), + document_path(document, index: index, nested_document: true), method: :delete, remote: true, data: { confirm: t('documents.actions.destroy.confirm') }, @@ -47,6 +47,7 @@ module DocumentsHelper elsif !document.persisted? && document.cached_attachment.present? link_to t('documents.form.delete_button'), destroy_upload_documents_path(path: document.cached_attachment, + nested_document: true, index: index, documentable_type: document.documentable_type, documentable_id: document.documentable_id), @@ -68,7 +69,8 @@ module DocumentsHelper url: document_direct_upload_url(document), cached_attachment_input_field: document_nested_field_id(document, index, :cached_attachment), multiple: false, - index: index + index: index, + nested_document: true }, name: document_nested_field_name(document, index, :attachment), id: document_nested_field_id(document, index, :attachment) diff --git a/app/views/budgets/investments/_form.html.erb b/app/views/budgets/investments/_form.html.erb index 40685dff7..7cf2bb1e2 100644 --- a/app/views/budgets/investments/_form.html.erb +++ b/app/views/budgets/investments/_form.html.erb @@ -22,7 +22,7 @@
- <%= render 'documents/nested_documents', resource: @investment %> + <%= render 'documents/nested_documents', documentable: @investment %>
diff --git a/app/views/documents/_form.html.erb b/app/views/documents/_form.html.erb index 0cb6c5838..591503758 100644 --- a/app/views/documents/_form.html.erb +++ b/app/views/documents/_form.html.erb @@ -11,33 +11,7 @@
-
- <%= f.text_field :title %> -
- -
- <%= 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' %> -
-

<%= document_attachment_file_name(@document) %>

-
- - <% if @document.errors.has_key?(:attachment) %> -
-
- <%= errors_on_attachment(@document) %> -
-
- <% end %> + <%= render 'plain_fields', document: @document %>
<%= f.submit(t("documents.form.submit_button"), class: "button expanded") %> diff --git a/app/views/documents/_nested_documents.html.erb b/app/views/documents/_nested_documents.html.erb index 69bc0a02f..5aded04af 100644 --- a/app/views/documents/_nested_documents.html.erb +++ b/app/views/documents/_nested_documents.html.erb @@ -3,7 +3,7 @@

<%= documentables_note(documentable) %>

<% 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 %>
diff --git a/app/views/documents/_nested_document.html.erb b/app/views/documents/_nested_fields.html.erb similarity index 100% rename from app/views/documents/_nested_document.html.erb rename to app/views/documents/_nested_fields.html.erb diff --git a/app/views/documents/_plain_fields.html.erb b/app/views/documents/_plain_fields.html.erb new file mode 100644 index 000000000..510fffce9 --- /dev/null +++ b/app/views/documents/_plain_fields.html.erb @@ -0,0 +1,50 @@ +
+ +
+ <%= 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) %> + <%= document.errors[:title].join(", ") %> + <% end %> +
+ +
+ <%= 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) %> +
+
+ <%= errors_on_attachment(document) %> +
+
+ <% end %> +
+

<%= document_attachment_file_name(document) %>

+
+ +
\ No newline at end of file diff --git a/app/views/documents/destroy.js.erb b/app/views/documents/destroy.js.erb index cfccee9db..953619b9e 100644 --- a/app/views/documents/destroy.js.erb +++ b/app/views/documents/destroy.js.erb @@ -1,7 +1,17 @@ -App.Documentable.destroy("<%= 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]), - remote: true, - id: "new_document_link", - class: "button hollow" %> -App.Documentable.updateNewDocumentButton("<%= j new_document_link %>") +<% if params[:nested_document] == "true" %> + + 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]), + remote: true, + id: "new_document_link", + class: "button hollow" %> + 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 %> diff --git a/app/views/documents/new_nested.js.erb b/app/views/documents/new_nested.js.erb index a2716f1dd..99dc404d8 100644 --- a/app/views/documents/new_nested.js.erb +++ b/app/views/documents/new_nested.js.erb @@ -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_nested_documents_path(documentable_type: params[:documentable_type], index: params[:index].to_i + 1), remote: true, id: "new_document_link", 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 %>") diff --git a/app/views/documents/upload.js.erb b/app/views/documents/upload.js.erb index 106004e9a..912f79199 100644 --- a/app/views/documents/upload.js.erb +++ b/app/views/documents/upload.js.erb @@ -1,9 +1,12 @@ -<% - nested_fields = render 'documents/nested_document', document: @document, index: params[:index] -%> +<% if params[:nested_document] == "true" %> -<% if @document.cached_attachment.present? %> - App.Documentable.upload("<%= document_nested_field_wrapper_id(params[:index]) %>", "<%= j nested_fields %>", true) + App.Documentable.uploadNestedDocument("<%= document_nested_field_wrapper_id(params[:index]) %>", + "<%= j render('documents/nested_fields', document: @document, index: params[:index]) %>", + <%= @document.cached_attachment.present? %>) <% 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 %> diff --git a/spec/shared/features/documentable.rb b/spec/shared/features/documentable.rb index 72a90cb7b..b8b5ebe1c 100644 --- a/spec/shared/features/documentable.rb +++ b/spec/shared/features/documentable.rb @@ -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.") end - scenario "Should be able for other users" do + scenario "Should not be able for other users" do login_as create(:user) 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") 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 login_as documentable.author - visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id, from: send(documentable_path, arguments)) @@ -191,23 +279,13 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path, scenario "Should show validation errors" do login_as documentable.author - visit new_document_path(documentable_type: documentable.class.name, documentable_id: documentable.id) + click_on "Upload document" 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 - 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" + expect(page).to have_selector "small.error", text: "can't be blank", count: 2 end 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, from: send(documentable_path, arguments)) attach_file :document_attachment, "spec/fixtures/files/empty.pdf" + sleep 1 click_on "Upload document" 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)) fill_in :document_title, with: "Document title" attach_file :document_attachment, "spec/fixtures/files/empty.pdf" + sleep 1 click_on "Upload document" 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)) fill_in :document_title, with: "Document title" attach_file :document_attachment, "spec/fixtures/files/empty.pdf" + sleep 1 click_on "Upload document" within "##{dom_id(documentable)}" do @@ -258,6 +339,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path, from: send(documentable_path, arguments)) fill_in :document_title, with: "Document title" attach_file :document_attachment, "spec/fixtures/files/empty.pdf" + sleep 1 click_on "Upload document" expect(page).to have_link "Documents (1)" diff --git a/spec/shared/features/nested_documentable.rb b/spec/shared/features/nested_documentable.rb index c5c9928f6..d03841d36 100644 --- a/spec/shared/features/nested_documentable.rb +++ b/spec/shared/features/nested_documentable.rb @@ -18,234 +18,219 @@ shared_examples "nested documentable" do |documentable_factory_name, path, docum end end - scenario "Should show new document link when max documents allowed limit is not reached" do - login_as user - visit send(path, arguments) + describe "at #{path}" do - expect(page).to have_selector "#new_document_link", visible: true - end + scenario "Should show new document link when max documents allowed limit is not reached" do + login_as user + visit send(path, arguments) - scenario "Should not show new document link when documentable max documents allowed limit is reached", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - sleep 1 - click_link "Add new document" - sleep 1 - click_link "Add new document" - - expect(page).to have_selector "#new_document_link", visible: false - end - - scenario "Should not show max documents warning when no documents added", :js do - login_as user - visit send(path, arguments) - - expect(page).to have_selector ".max-documents-notice", visible: false - end - - scenario "Should show max documents warning when max documents allowed limit is reached", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - sleep 1 - click_link "Add new document" - sleep 1 - click_link "Add new document" - - expect(page).to have_selector ".max-documents-notice", visible: true - end - - scenario "Should hide max documents warning after any document removal", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - sleep 1 - click_link "Add new document" - sleep 1 - click_link "Add new document" - sleep 1 - within "#document_0" do - find("a", text: "Remove document").click - end - sleep 1 - - expect(page).to have_selector ".max-documents-notice", visible: false - end - - scenario "Should update nested document file name after choosing a file", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - 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" - end - - scenario "Should update nested document file title with file name after choosing a file when no title defined", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - 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" - end - - scenario "Should not update nested document file title with file name after choosing a file when title already defined", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - 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) - sleep 1 - - expect(find("##{documentable_factory_name}_documents_attributes_0_title").value).to eq "Title" - end - - scenario "Should update loading bar style after valid file upload", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - 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" - end - - scenario "Should update loading bar style after unvalid file upload", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - 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" - end - - scenario "Should update document cached_attachment field after valid file upload", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - 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") - end - - scenario "Should not update document cached_attachment field after unvalid file upload", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - 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 "" - end - - scenario "Should show document errors after unvalid file upload", :js do - login_as user - visit send(path, arguments) - - click_link "Add new document" - sleep 1 - click_on submit_button - - within "#document_0" do - expect(page).to have_content("can't be blank", count: 2) - end - end - - scenario "Should delete document after valid file upload and click on remove button", :js do - login_as user - visit send(path, arguments) - - attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") - within "#document_0" do - click_link "Remove document" + expect(page).to have_selector "#new_document_link", visible: true end - expect(page).not_to have_selector("#document_0") - end + scenario "Should not show new document link when documentable max documents allowed limit is reached", :js do + login_as user + visit send(path, arguments) - scenario "Should delete document after valid file upload and click on remove button", :js do - login_as user - visit send(path, arguments) + click_link "Add new document" + sleep 1 + click_link "Add new document" + sleep 1 + click_link "Add new document" - attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") - within "#document_0" do - click_link "Remove document" + expect(page).to have_selector "#new_document_link", visible: false end - expect(page).to have_content "Document was deleted successfully." - end + scenario "Should not show max documents warning when no documents added", :js do + login_as user + visit send(path, arguments) - scenario "Should show successful notice when resource filled correctly without any nested documents", :js do - login_as user - visit send(path, arguments) - send(fill_resource_method_name) if fill_resource_method_name - - click_on submit_button - expect(page).to have_content documentable_success_notice - end - - scenario "Should show successful notice when resource filled correctly and after valid file uploads", :js do - login_as user - visit send(path, arguments) - send(fill_resource_method_name) if fill_resource_method_name - - attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") - - click_on submit_button - expect(page).to have_content documentable_success_notice - end - - scenario "Should show new document after successful creation with one uploaded file", :js do - login_as user - visit send(path, arguments) - send(fill_resource_method_name) if fill_resource_method_name - - attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") - - click_on submit_button - redirected_to_resource_show_or_navigate_to - - expect(page).to have_content "Documents (1)" - end - - scenario "Should show resource with new document after successful creation with maximum allowed uploaded files", :js do - login_as user - visit send(path, arguments) - send(fill_resource_method_name) if fill_resource_method_name - - documentable.class.max_documents_allowed.times.each do |index| - attach_new_file(documentable_factory_name, index , "spec/fixtures/files/empty.pdf") + expect(page).to have_selector ".max-documents-notice", visible: false end - click_on submit_button - redirected_to_resource_show_or_navigate_to + scenario "Should show max documents warning when max documents allowed limit is reached", :js do + login_as user + visit send(path, arguments) + + click_link "Add new document" + sleep 1 + click_link "Add new document" + sleep 1 + click_link "Add new document" + + expect(page).to have_selector ".max-documents-notice", visible: true + end + + scenario "Should hide max documents warning after any document removal", :js do + login_as user + visit send(path, arguments) + + click_link "Add new document" + sleep 1 + click_link "Add new document" + sleep 1 + click_link "Add new document" + sleep 1 + within "#document_0" do + find("a", text: "Remove document").click + end + sleep 1 + + expect(page).to have_selector ".max-documents-notice", visible: false + end + + scenario "Should update nested document file name after choosing a file", :js do + login_as user + visit send(path, arguments) + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") + + expect(page).to have_selector ".file-name", text: "empty.pdf" + end + + scenario "Should update nested document file title with file name after choosing a file when no title defined", :js do + login_as user + visit send(path, arguments) + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") + + expect(find("##{documentable_factory_name}_documents_attributes_0_title").value).to eq "empty.pdf" + end + + scenario "Should not update nested document file title with file name after choosing a file when title already defined", :js do + login_as user + visit send(path, arguments) + + click_link "Add new document" + sleep 1 + 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) + sleep 1 + + expect(find("##{documentable_factory_name}_documents_attributes_0_title").value).to eq "Title" + end + + scenario "Should update loading bar style after valid file upload", :js do + login_as user + visit send(path, arguments) + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") + fill_in "#{documentable_factory_name}[documents_attributes][0][title]", with: "Title" + + expect(page).to have_selector ".loading-bar.complete" + end + + scenario "Should update loading bar style after unvalid file upload", :js do + login_as user + visit send(path, arguments) + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/logo_header.png") + + expect(page).to have_selector ".loading-bar.errors" + end + + scenario "Should update document cached_attachment field after valid file upload", :js do + login_as user + visit send(path, arguments) + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") + + expect(find("input[name='#{documentable_factory_name}[documents_attributes][0][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 user + visit send(path, arguments) + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/logo_header.png") + + expect(find("input[name='#{documentable_factory_name}[documents_attributes][0][cached_attachment]']", visible: false).value).to eq "" + end + + scenario "Should show document errors after unvalid file upload", :js do + login_as user + visit send(path, arguments) + + click_link "Add new document" + sleep 1 + click_on submit_button + + within "#document_0" do + expect(page).to have_content("can't be blank", count: 2) + end + end + + scenario "Should delete document after valid file upload and click on remove button", :js do + login_as user + visit send(path, arguments) + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") + within "#document_0" do + click_link "Remove document" + end + + expect(page).not_to have_selector("#document_0") + end + + scenario "Should delete document after valid file upload and click on remove button", :js do + login_as user + visit send(path, arguments) + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") + within "#document_0" do + click_link "Remove document" + end + + expect(page).to have_content "Document was deleted successfully." + end + + scenario "Should show successful notice when resource filled correctly without any nested documents", :js do + login_as user + visit send(path, arguments) + send(fill_resource_method_name) if fill_resource_method_name + + click_on submit_button + expect(page).to have_content documentable_success_notice + end + + scenario "Should show successful notice when resource filled correctly and after valid file uploads", :js do + login_as user + visit send(path, arguments) + send(fill_resource_method_name) if fill_resource_method_name + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") + + click_on submit_button + expect(page).to have_content documentable_success_notice + end + + scenario "Should show new document after successful creation with one uploaded file", :js do + login_as user + visit send(path, arguments) + send(fill_resource_method_name) if fill_resource_method_name + + attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") + + click_on submit_button + redirected_to_resource_show_or_navigate_to + + expect(page).to have_content "Documents (1)" + end + + scenario "Should show resource with new document after successful creation with maximum allowed uploaded files", :js do + login_as user + visit send(path, arguments) + send(fill_resource_method_name) if fill_resource_method_name + + documentable.class.max_documents_allowed.times.each do |index| + attach_new_file(documentable_factory_name, index , "spec/fixtures/files/empty.pdf") + end + + click_on submit_button + redirected_to_resource_show_or_navigate_to + + expect(page).to have_content "Documents (#{documentable.class.max_documents_allowed})" + end - expect(page).to have_content "Documents (#{documentable.class.max_documents_allowed})" end end