Browse Source

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
pull/442/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
e57c381188
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/controllers/case_logs_controller.rb
  2. 12
      app/models/case_log.rb
  3. 5
      app/views/case_logs/_log_list.html.erb
  4. 41
      spec/requests/case_logs_controller_spec.rb

5
app/controllers/case_logs_controller.rb

@ -8,6 +8,11 @@ class CaseLogsController < ApplicationController
def index def index
@pagy, @case_logs = pagy(current_user.case_logs) @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 end
def create def create

12
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 LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype:).soft_max
end 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 private
PIO = Postcodes::IO.new PIO = Postcodes::IO.new

5
app/views/case_logs/_log_list.html.erb

@ -1,6 +1,9 @@
<figure class="app-figure"> <figure class="app-figure">
<figcaption id="<%= title.dasherize %>" class="app-figure__caption"> <figcaption id="<%= title.dasherize %>" class="app-figure__caption">
<strong><%= pagy.count %></strong> total <%= title.downcase %> <span class="govuk-!-margin-right-4">
<strong><%= pagy.count %></strong> total <%= title.downcase %>
</span>
<a class="govuk-link" download href="/logs.csv" type="text/csv">Download (CSV)</a>
</figcaption> </figcaption>
<section class="app-table-group" tabindex="0" aria-labelledby="<%= title.dasherize %>"> <section class="app-table-group" tabindex="0" aria-labelledby="<%= title.dasherize %>">
<table class="govuk-table"> <table class="govuk-table">

41
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 it "does not have pagination in the title" do
expect(page).to have_title("Logs") expect(page).to have_title("Logs")
end end
it "shows the download csv link" do
expect(page).to have_link("Download (CSV)", href: "/logs.csv")
end
end end
context "when there are more than 20 logs" do context "when there are more than 20 logs" do
@ -396,6 +400,43 @@ RSpec.describe CaseLogsController, type: :request do
end end
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 describe "PATCH" do
let(:case_log) do let(:case_log) do
FactoryBot.create(:case_log, :in_progress, tenant_code: "Old Value", postcode_full: "M1 1AE") FactoryBot.create(:case_log, :in_progress, tenant_code: "Old Value", postcode_full: "M1 1AE")

Loading…
Cancel
Save