Skip to content

Commit

Permalink
Admin edits csv upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Feb 22, 2024
1 parent 591412e commit ed03df5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
20 changes: 17 additions & 3 deletions app/controllers/admin/csv_uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
7 changes: 7 additions & 0 deletions app/views/admin/csv_uploads/edit.html.haml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions app/views/admin/csv_uploads/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
36 changes: 36 additions & 0 deletions spec/system/csv_uploads/admin_edits_financial_account_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ed03df5

Please sign in to comment.