From e57c38118844f111cce42929f22592285121a35e Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Thu, 31 Mar 2022 14:22:25 +0100 Subject: [PATCH] CSV download (#441) * Link text * Controller response * Fix download link * Check CSV only downloads collection * Download value labels rather than values * Set mime type in link markup --- app/controllers/case_logs_controller.rb | 5 +++ app/models/case_log.rb | 12 +++++++ app/views/case_logs/_log_list.html.erb | 5 ++- spec/requests/case_logs_controller_spec.rb | 41 ++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 1f84d7a15..c828cebcd 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -8,6 +8,11 @@ class CaseLogsController < ApplicationController def index @pagy, @case_logs = pagy(current_user.case_logs) + + respond_to do |format| + format.html + format.csv { send_data @case_logs.to_csv, filename: "logs-#{Time.zone.now}.csv" } + end end def create diff --git a/app/models/case_log.rb b/app/models/case_log.rb index d3271083c..76c47297c 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -293,6 +293,18 @@ class CaseLog < ApplicationRecord LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype:).soft_max end + def self.to_csv + CSV.generate(headers: true) do |csv| + csv << attribute_names + + all.find_each do |record| + csv << record.attributes.map do |att, val| + record.form.get_question(att, record)&.label_from_value(val) || val + end + end + end + end + private PIO = Postcodes::IO.new diff --git a/app/views/case_logs/_log_list.html.erb b/app/views/case_logs/_log_list.html.erb index 3029e5e9c..e728a5385 100644 --- a/app/views/case_logs/_log_list.html.erb +++ b/app/views/case_logs/_log_list.html.erb @@ -1,6 +1,9 @@
- <%= pagy.count %> total <%= title.downcase %> + + <%= pagy.count %> total <%= title.downcase %> + + Download (CSV)
diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb index ab0282576..b3fa651eb 100644 --- a/spec/requests/case_logs_controller_spec.rb +++ b/spec/requests/case_logs_controller_spec.rb @@ -205,6 +205,10 @@ RSpec.describe CaseLogsController, type: :request do it "does not have pagination in the title" do expect(page).to have_title("Logs") end + + it "shows the download csv link" do + expect(page).to have_link("Download (CSV)", href: "/logs.csv") + end end context "when there are more than 20 logs" do @@ -396,6 +400,43 @@ RSpec.describe CaseLogsController, type: :request do end end + describe "CSV download" do + let(:headers) { { "Accept" => "text/csv" } } + let(:user) { FactoryBot.create(:user) } + let(:organisation) { user.organisation } + let(:other_organisation) { FactoryBot.create(:organisation) } + let!(:case_log) do + FactoryBot.create( + :case_log, + owning_organisation: organisation, + managing_organisation: organisation, + ecstat1: 1, + ) + end + + before do + sign_in user + FactoryBot.create(:case_log) + get "/logs", headers: headers, params: {} + end + + it "downloads a CSV file with headers" do + csv = CSV.parse(response.body) + expect(csv.first.first).to eq("id") + expect(csv.second.first).to eq(case_log.id.to_s) + end + + it "does not download other orgs logs" do + csv = CSV.parse(response.body) + expect(csv.count).to eq(2) + end + + it "downloads answer labels rather than values" do + csv = CSV.parse(response.body) + expect(csv.second[10]).to eq("Full-time – 30 hours or more") + end + end + describe "PATCH" do let(:case_log) do FactoryBot.create(:case_log, :in_progress, tenant_code: "Old Value", postcode_full: "M1 1AE")