Skip to content

Commit

Permalink
Fix missing headers when exporting Project data
Browse files Browse the repository at this point in the history
Project::Export uses the first request in the project to generate the
CSV headers. However, if the request hasn't had data extracted, the
headers for the dataset columns will be blank, even though other
requests in the project may have had a dataset extraction.

This commit ensures that headers are always present by looking up the
project's key set, and iterating through that to fetch the relevant
submission value for that key, or otherwise assigning a nil value.

This has the benefit of ensuring that the keys/values exported are
always in sync with the current project key set.

Fixes #8141.
  • Loading branch information
garethrees committed Mar 11, 2024
1 parent 08aa218 commit f7549bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/models/project/export/info_request.rb
Expand Up @@ -24,7 +24,7 @@ def data
latest_status_contributor: status_contributor,
status: described_state,
dataset_contributor: dataset_contributor
}.merge(extracted_values_as_hash)
}.merge(dataset_values)
end

private
Expand Down Expand Up @@ -53,6 +53,12 @@ def dataset_contributor
extraction_submission.user.name
end

def dataset_values
project.key_set.keys.pluck(:title).each_with_object({}) do |key, memo|
memo[key] = extracted_values_as_hash[key]
end
end

def extracted_values
return unless extraction_submission

Expand Down
1 change: 1 addition & 0 deletions doc/CHANGES.md
Expand Up @@ -2,6 +2,7 @@

## Highlighted Features

* Fix missing headers when exporting Project data (Gareth Rees)
* Reduce amount of storage related background jobs (Graeme Porteous)
* Add automatic parsing of emails contain Excel spreadsheets (Graeme Porteous)
* Improve rendering of admin hidden request prominence and explanations (Graeme
Expand Down
7 changes: 6 additions & 1 deletion spec/models/project/export/info_request_spec.rb
Expand Up @@ -6,6 +6,10 @@
include LinkToHelper

let(:project) { FactoryBot.build(:project) }

let(:key_set) { FactoryBot.build(:dataset_key_set, resource: project) }
let!(:dataset_key) { FactoryBot.create(:dataset_key, key_set: key_set) }

let(:contributor) { FactoryBot.build(:user) }

let(:public_body) { FactoryBot.build(:public_body) }
Expand Down Expand Up @@ -71,7 +75,8 @@
request_owner: info_request.user.name,
latest_status_contributor: contributor.name,
status: info_request.described_state,
dataset_contributor: nil
:dataset_contributor => nil,
'Were there any errors?' => nil
)
end
end
Expand Down

0 comments on commit f7549bb

Please sign in to comment.