Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully implement crud pages for csv uploads #170

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions app/controllers/admin/csv_uploads_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
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

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"
redirect_to admin_csv_uploads_path
end

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"
4 changes: 4 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,10 @@

%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 }

%h2 Data
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 index new show]
resources :csv_uploads
resources :financial_accounts
resources :gift_ideas
resources :hooks, only: %i[create edit index]
Expand Down
44 changes: 33 additions & 11 deletions spec/system/csv_uploads/admin_creates_csv_upload_spec.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions spec/system/csv_uploads/admin_deletes_csv_upload_spec.rb
Original file line number Diff line number Diff line change
@@ -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
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
13 changes: 10 additions & 3 deletions spec/system/csv_uploads/admin_views_csv_upload_spec.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
23 changes: 21 additions & 2 deletions spec/system/csv_uploads/admin_views_csv_uploads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/system/gift_ideas/admin_creates_gift_idea_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down