From 9b726c2bcd222ea53edb037d31b59c917236ea45 Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Thu, 22 Feb 2024 08:33:00 -0600 Subject: [PATCH 1/3] Update existing pages to match better style --- .../admin/csv_uploads_controller.rb | 8 ++-- .../admin_creates_csv_upload_spec.rb | 44 ++++++++++++++----- .../admin_views_csv_upload_spec.rb | 13 ++++-- .../admin_views_csv_uploads_spec.rb | 23 +++++++++- .../admin_creates_gift_idea_spec.rb | 2 +- 5 files changed, 70 insertions(+), 20 deletions(-) diff --git a/app/controllers/admin/csv_uploads_controller.rb b/app/controllers/admin/csv_uploads_controller.rb index af4e2cf..263890f 100644 --- a/app/controllers/admin/csv_uploads_controller.rb +++ b/app/controllers/admin/csv_uploads_controller.rb @@ -1,14 +1,16 @@ class Admin::CsvUploadsController < ApplicationController expose(:csv_upload) - expose(:csv_uploads) { CsvUpload.order(created_at: :desc).page params[:page] } + expose(:csv_uploads) do + CsvUpload.order(created_at: :desc).page(params[:page]) + end def create if csv_upload.save ParseCsvUploadJob.perform_later(csv_upload.id) - flash.notice = "CSV Upload successfully created" + flash.notice = "CSV Upload created" redirect_to admin_csv_upload_path(csv_upload) else - flash.alert = csv_upload.errors.full_messages.join(",") + flash.alert = csv_upload.errors.full_messages.to_sentence redirect_to new_admin_csv_upload_path end end diff --git a/spec/system/csv_uploads/admin_creates_csv_upload_spec.rb b/spec/system/csv_uploads/admin_creates_csv_upload_spec.rb index b8bf8e5..b0fb6bf 100644 --- a/spec/system/csv_uploads/admin_creates_csv_upload_spec.rb +++ b/spec/system/csv_uploads/admin_creates_csv_upload_spec.rb @@ -1,44 +1,66 @@ require "rails_helper" -describe "Admin creates CsvUpload" do +describe "Admin creates csv upload" do include_context "admin password matches" - scenario "without a parser" do + scenario "from list page" do + visit "/admin/csv_uploads" + click_on "New CSV Upload" + expect(page).to have_css "h1", text: "New CSV Upload" + expect(page).to have_css "a", text: "CSV Upload List" + expect(current_path).to eq new_admin_csv_upload_path + end + + scenario "create with parser error" do visit "/admin/csv_uploads/new" click_on "create" - expect(CsvUpload.count).to eq 0 select = page.find("#csv_upload_parser_class_name") error_message = select.native.attribute("validationMessage") expect(error_message).to eq "Please select an item in the list." end - scenario "without a file" do + scenario "create with file error" do visit "/admin/csv_uploads/new" select "WellsFargoParser", from: "csv_upload_parser_class_name" click_on "create" - expect(CsvUpload.count).to eq 0 file_input = page.find("#file_picker") error_message = file_input.native.attribute("validationMessage") expect(error_message).to eq "Please select a file." end - scenario "with an empty file" do + scenario "create with empty file error" do visit "/admin/csv_uploads/new" select "WellsFargoParser", from: "csv_upload_parser_class_name" attach_file "file", "spec/csv_files/empty.csv" click_on "create" - expect(CsvUpload.count).to eq 0 expect(page).to have_content "Data can't be blank" + expect(page).to have_css ".alert", text: "Data can't be blank" + expect(current_path).to eq new_admin_csv_upload_path end - scenario "with valid financial transactions" do + scenario "create successfully" do visit "/admin/csv_uploads/new" select "WellsFargoParser", from: "csv_upload_parser_class_name" attach_file "file", "spec/csv_files/one_wf_transaction.csv" click_on "create" - expect(page).to have_content "CSV Upload successfully created" - csv_upload = CsvUpload.last - expect(page).to have_content "CSV Upload #{csv_upload.id}" + + expect(page).to have_css ".notice", text: "CSV Upload created" expect(ParseCsvUploadJob).to have_been_enqueued + + csv_upload = CsvUpload.last + expect(current_path).to eq admin_csv_upload_path(csv_upload) + + actual_values = page.all("tr").map do |table_row| + table_row.all("td").map(&:text) + end + + expect(actual_values).to eq( + [ + ["Parser Class Name", "WellsFargoParser"], + ["Original Filename", "one_wf_transaction.csv"], + ["Created At", csv_upload.created_at.to_formatted_s(:long)], + ["Updated At", csv_upload.updated_at.to_formatted_s(:long)] + ] + ) end end diff --git a/spec/system/csv_uploads/admin_views_csv_upload_spec.rb b/spec/system/csv_uploads/admin_views_csv_upload_spec.rb index 407e851..bc817ee 100644 --- a/spec/system/csv_uploads/admin_views_csv_upload_spec.rb +++ b/spec/system/csv_uploads/admin_views_csv_upload_spec.rb @@ -1,8 +1,17 @@ require "rails_helper" -describe "Admin views CsvUpload" do +describe "Admin views csv upload" do include_context "admin password matches" + scenario "from list page" do + csv_upload = FactoryBot.create(:csv_upload) + visit "/admin/csv_uploads" + click_on csv_upload.id.to_s + expect(page).to have_css "h1", text: "CSV Upload #{csv_upload.id}" + expect(page).to have_css "a", text: "CSV Upload List" + expect(current_path).to eq admin_csv_upload_path(csv_upload) + end + scenario "views CsvUpload" do csv_upload = FactoryBot.create( :csv_upload, @@ -13,8 +22,6 @@ visit "/admin/csv_uploads/#{csv_upload.id}" - expect(page).to have_content "CSV Upload #{csv_upload.id}" - actual_values = page.all("tr").map do |table_row| table_row.all("td").map(&:text) end diff --git a/spec/system/csv_uploads/admin_views_csv_uploads_spec.rb b/spec/system/csv_uploads/admin_views_csv_uploads_spec.rb index 3f04481..55b8b74 100644 --- a/spec/system/csv_uploads/admin_views_csv_uploads_spec.rb +++ b/spec/system/csv_uploads/admin_views_csv_uploads_spec.rb @@ -3,10 +3,29 @@ describe "Admin views CsvUploads" do include_context "admin password matches" - scenario "views CsvUploads" do + scenario "from dashboard" do + visit "/dashboard" + click_on "CSV Uploads" + expect(page).to have_css "h1", text: "CSV Uploads" + expect(current_path).to eq admin_csv_uploads_path + end + + scenario "with no records" do + visit "/admin/csv_uploads" + expect(page.all("tbody tr").count).to eq 0 + end + + scenario "with a page of records" do FactoryBot.create_list(:csv_upload, 3) visit "/admin/csv_uploads" - expect(page).to have_css "a", text: "New CSV Upload" expect(page.all("tbody tr").count).to eq 3 + expect(page).to_not have_css "nav.pagination" + end + + scenario "with two pages of records" do + FactoryBot.create_list(:csv_upload, 4) + visit "/admin/csv_uploads" + expect(page.all("tbody tr").count).to eq 3 + expect(page).to have_css "nav.pagination" end end diff --git a/spec/system/gift_ideas/admin_creates_gift_idea_spec.rb b/spec/system/gift_ideas/admin_creates_gift_idea_spec.rb index 9575a2f..a9099e9 100644 --- a/spec/system/gift_ideas/admin_creates_gift_idea_spec.rb +++ b/spec/system/gift_ideas/admin_creates_gift_idea_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe "Admin creates GiftIdea" do +describe "Admin creates gift idea" do include_context "admin password matches" scenario "from list page" do From d47382621fd27f289d8b4482aa926de2011f7a21 Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Thu, 22 Feb 2024 08:38:52 -0600 Subject: [PATCH 2/3] Admin deletes csv upload --- .../admin/csv_uploads_controller.rb | 6 +++++ app/views/admin/csv_uploads/show.html.haml | 2 ++ config/routes.rb | 2 +- .../admin_deletes_csv_upload_spec.rb | 27 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 spec/system/csv_uploads/admin_deletes_csv_upload_spec.rb diff --git a/app/controllers/admin/csv_uploads_controller.rb b/app/controllers/admin/csv_uploads_controller.rb index 263890f..eb5fb10 100644 --- a/app/controllers/admin/csv_uploads_controller.rb +++ b/app/controllers/admin/csv_uploads_controller.rb @@ -15,6 +15,12 @@ def create end end + def destroy + csv_upload.destroy + flash.notice = "CSV Upload deleted" + redirect_to admin_csv_uploads_path + end + private def csv_upload_params diff --git a/app/views/admin/csv_uploads/show.html.haml b/app/views/admin/csv_uploads/show.html.haml index 909690d..3405df6 100644 --- a/app/views/admin/csv_uploads/show.html.haml +++ b/app/views/admin/csv_uploads/show.html.haml @@ -2,6 +2,8 @@ %p= link_to "CSV Upload List", admin_csv_uploads_path +%p= link_to "Delete CSV Upload", admin_csv_upload_path(csv_upload), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" } + = render partial: "attrs_table", locals: { attrs: csv_upload.table_attrs } %h2 Data diff --git a/config/routes.rb b/config/routes.rb index dbc68e5..6847ccc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,7 +26,7 @@ namespace :admin do resources :books, only: %i[create edit new update] - resources :csv_uploads, only: %i[create index new show] + resources :csv_uploads, only: %i[create destroy index new show] resources :financial_accounts resources :gift_ideas resources :hooks, only: %i[create edit index] diff --git a/spec/system/csv_uploads/admin_deletes_csv_upload_spec.rb b/spec/system/csv_uploads/admin_deletes_csv_upload_spec.rb new file mode 100644 index 0000000..d5be03f --- /dev/null +++ b/spec/system/csv_uploads/admin_deletes_csv_upload_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +describe "Admin deletes csv upload" do + include_context "admin password matches" + + let(:csv_upload) { FactoryBot.create(:csv_upload) } + + scenario "cancels delete" do + visit "/admin/csv_uploads/#{csv_upload.id}" + + dismiss_confirm { click_on "Delete CSV Upload" } + + expect(CsvUpload.count).to eq 1 + expect(current_path).to eq admin_csv_upload_path(csv_upload) + end + + scenario "confirms delete" do + visit "/admin/csv_uploads/#{csv_upload.id}" + + accept_confirm { click_on "Delete CSV Upload" } + + expect(page).to have_css ".notice", text: "CSV Upload deleted" + + expect(CsvUpload.count).to eq 0 + expect(current_path).to eq admin_csv_uploads_path + end +end From 1d8acbe621c02cb7818715b8bd38a7e3e13c7d84 Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Thu, 22 Feb 2024 08:54:58 -0600 Subject: [PATCH 3/3] Admin edits csv upload --- .../admin/csv_uploads_controller.rb | 20 +++++++++-- app/views/admin/csv_uploads/edit.html.haml | 7 ++++ app/views/admin/csv_uploads/show.html.haml | 2 ++ config/routes.rb | 2 +- .../admin_edits_financial_account_spec.rb | 36 +++++++++++++++++++ 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 app/views/admin/csv_uploads/edit.html.haml create mode 100644 spec/system/csv_uploads/admin_edits_financial_account_spec.rb diff --git a/app/controllers/admin/csv_uploads_controller.rb b/app/controllers/admin/csv_uploads_controller.rb index eb5fb10..8da880e 100644 --- a/app/controllers/admin/csv_uploads_controller.rb +++ b/app/controllers/admin/csv_uploads_controller.rb @@ -15,6 +15,16 @@ def create end end + def update + if csv_upload.update(csv_upload_params) + flash.notice = "CSV Upload updated" + redirect_to admin_csv_upload_path(csv_upload) + else + flash.alert = csv_upload.errors.full_messages.to_sentence + redirect_to edit_admin_csv_upload_path(csv_upload) + end + end + def destroy csv_upload.destroy flash.notice = "CSV Upload deleted" @@ -24,9 +34,13 @@ def destroy private def csv_upload_params - safe_params = params.require(:csv_upload).permit(:parser_class_name) - safe_params[:original_filename] = params[:file].original_filename - safe_params[:data] = params[:file].read + safe_params = params.require(:csv_upload).permit(:original_filename, :parser_class_name) + + if params[:file] + safe_params[:original_filename] = params[:file].original_filename + safe_params[:data] = params[:file].read + end + safe_params end end diff --git a/app/views/admin/csv_uploads/edit.html.haml b/app/views/admin/csv_uploads/edit.html.haml new file mode 100644 index 0000000..42c58da --- /dev/null +++ b/app/views/admin/csv_uploads/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Edit CSV Upload #{csv_upload.id} + +%p= link_to "Show CSV Upload", admin_csv_upload_path(csv_upload) + += form_with model: [:admin, csv_upload] do |form| + = form.text_field :original_filename, placeholder: "original filename" + = form.button "update" diff --git a/app/views/admin/csv_uploads/show.html.haml b/app/views/admin/csv_uploads/show.html.haml index 3405df6..067cada 100644 --- a/app/views/admin/csv_uploads/show.html.haml +++ b/app/views/admin/csv_uploads/show.html.haml @@ -2,6 +2,8 @@ %p= link_to "CSV Upload List", admin_csv_uploads_path +%p= link_to "Edit CSV Upload", edit_admin_csv_upload_path(csv_upload) + %p= link_to "Delete CSV Upload", admin_csv_upload_path(csv_upload), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" } = render partial: "attrs_table", locals: { attrs: csv_upload.table_attrs } diff --git a/config/routes.rb b/config/routes.rb index 6847ccc..49a64f4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,7 +26,7 @@ namespace :admin do resources :books, only: %i[create edit new update] - resources :csv_uploads, only: %i[create destroy index new show] + resources :csv_uploads resources :financial_accounts resources :gift_ideas resources :hooks, only: %i[create edit index] diff --git a/spec/system/csv_uploads/admin_edits_financial_account_spec.rb b/spec/system/csv_uploads/admin_edits_financial_account_spec.rb new file mode 100644 index 0000000..326c8ff --- /dev/null +++ b/spec/system/csv_uploads/admin_edits_financial_account_spec.rb @@ -0,0 +1,36 @@ +require "rails_helper" + +describe "Admin edits csv upload" do + include_context "admin password matches" + + scenario "from show page" do + csv_upload = FactoryBot.create(:csv_upload) + visit "/admin/csv_uploads/#{csv_upload.id}" + click_on "Edit CSV Upload" + expect(page).to have_css "h1", text: "Edit CSV Upload #{csv_upload.id}" + expect(page).to have_css "a", text: "Show CSV Upload" + expect(current_path).to eq edit_admin_csv_upload_path(csv_upload) + end + + scenario "edit with errors" do + csv_upload = FactoryBot.create(:csv_upload) + visit "/admin/csv_uploads/#{csv_upload.id}/edit" + fill_in "original filename", with: "" + click_on "update" + expect(page).to have_css ".alert", text: "Original filename can't be blank" + end + + scenario "edit successfully" do + csv_upload = FactoryBot.create( + :csv_upload, + original_filename: "some-gret-data.csl" + ) + visit "/admin/csv_uploads/#{csv_upload.id}/edit" + fill_in "original filename", with: "some-great-data.csv" + click_on "update" + + expect(page).to have_css ".notice", text: "CSV Upload updated" + expect(current_path).to eq admin_csv_upload_path(csv_upload) + expect(page).to have_css "td", text: "some-great-data.csv" + end +end