From cb0462a404810f2924dc6b7fa44a3158533f8597 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Tue, 9 Nov 2021 15:51:15 +0000 Subject: [PATCH] Read file --- Gemfile | 2 ++ Gemfile.lock | 4 ++++ app/controllers/bulk_upload_controller.rb | 8 ++++++++ app/views/case_logs/bulk_upload.html.erb | 15 +++++++-------- config/routes.rb | 3 ++- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index fa743fa4a..ff0d1b56b 100644 --- a/Gemfile +++ b/Gemfile @@ -29,6 +29,8 @@ gem "discard" gem "activeadmin" # Admin charts gem "chartkick" +# Spreadsheet parsing +gem "roo" group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index fa3d1a27d..c180de55d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -292,6 +292,9 @@ GEM actionpack (>= 5.0) railties (>= 5.0) rexml (3.2.5) + roo (2.8.3) + nokogiri (~> 1) + rubyzip (>= 1.3.0, < 3.0.0) rubocop (1.21.0) parallel (~> 1.10) parser (>= 3.0.0.0) @@ -405,6 +408,7 @@ DEPENDENCIES puma (~> 5.0) rack-mini-profiler (~> 2.0) rails (~> 6.1.4) + roo rspec-core! rspec-expectations! rspec-mocks! diff --git a/app/controllers/bulk_upload_controller.rb b/app/controllers/bulk_upload_controller.rb index e25d6fc6f..12ad21547 100644 --- a/app/controllers/bulk_upload_controller.rb +++ b/app/controllers/bulk_upload_controller.rb @@ -1,5 +1,13 @@ class BulkUploadController < ApplicationController + XLS = "application/vnd.ms-excel".freeze + def show render "case_logs/bulk_upload" end + + def process_bulk_upload + if params["case_log_bulk_upload"].content_type == XLS + xlsx = Roo::Spreadsheet.open(params["case_log_bulk_upload"].tempfile, extension: :xlsx) + end + end end diff --git a/app/views/case_logs/bulk_upload.html.erb b/app/views/case_logs/bulk_upload.html.erb index 4e926e1a0..1a6ae4d91 100644 --- a/app/views/case_logs/bulk_upload.html.erb +++ b/app/views/case_logs/bulk_upload.html.erb @@ -1,10 +1,9 @@ -

- Bulk Upload -

-
- - + <%= form_with url: "/case_logs/bulk_upload", method: "post", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= f.govuk_file_field :case_log_bulk_upload, + label: { text: "Bulk Upload", size: "l" }, + hint: { text: "Upload a spreadsheet using the template" } + %> + <%= f.govuk_submit "Upload" %> + <% end %>
diff --git a/config/routes.rb b/config/routes.rb index c0f881995..f6cb3122c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,8 @@ Rails.application.routes.draw do root to: "test#index" get "about", to: "about#index" - post "/case_logs/:id", to: "case_logs#submit_form" + post "/case_logs/:id", to: "case_logs#submit_form", constraints: { id: /\d/ } + post "/case_logs/bulk_upload", to: "bulk_upload#process_bulk_upload" get "/case_logs/bulk_upload", to: "bulk_upload#show" form_handler = FormHandler.instance