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