Skip to content

Commit

Permalink
🎁 Make headers with spaces acceptable
Browse files Browse the repository at this point in the history
This commit will adjust the Bulkrax csv parser fields to allow spaces in
the csv headers.  Previously, if you had `rights statement`, this header
was not accepted.  With this commit it will be.
  • Loading branch information
kirkkwang committed Mar 15, 2024
1 parent c6e1db8 commit 2357d3e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/parsers/bulkrax/csv_parser_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def missing_fields_for(record)
# any parser_mappings fields terms from `config/initializers/bulkrax.rb`
# or any keys that has sequential numbers like creator_1
(record[field] ||
mapped_from(field).map { |f| record[f] }.first ||
mapped_from(field).map { |f| record[f] }.any? ||
handle_keys_with_numbers(field, record)).blank?
end
end
Expand Down
14 changes: 11 additions & 3 deletions config/initializers/bulkrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'bagit'

# rubocop:disable Metrics/BlockLength
Bulkrax.setup do |config|
# Add or remove local parsers
config.parsers -= [
Expand Down Expand Up @@ -114,12 +115,18 @@
'subject' => { from: ['subject'], split: '\|' },
'table_of_contents' => { from: ['table_of_contents'], split: '\|' },
'title' => { from: ['title'], split: '\|' },
'video_embed' => { from: ['video_embed'] }
}

# currently Bulkrax does not support headers with spaces
# here we add the key but with the underscore turned into a space to accommodate
parser_mappings.each do |key, value|
value[:from] += ([key.tr('_', ' ')] + value[:from].map { |f| f.tr('_', ' ') })
value[:from].uniq!
end

config.field_mappings['Bulkrax::BagitParser'] = parser_mappings
config.field_mappings['Bulkrax::CsvParser'] = parser_mappings.merge(
{'video_embed' => { from: ['video_embed'] }}
)
config.field_mappings['Bulkrax::CsvParser'] = parser_mappings

# Add to, or change existing mappings as follows
# e.g. to exclude date
Expand All @@ -137,3 +144,4 @@
Hyrax::DashboardController.sidebar_partials[:repository_content] << "hyrax/dashboard/sidebar/bulkrax_sidebar_additions"
end
end
# rubocop:enable Metrics/BlockLength
11 changes: 11 additions & 0 deletions spec/parsers/bulkrax/csv_parser_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@
end
end

context 'when the csv headers do not have underscores' do
let(:generic_work_record_with_no_underscores) do
generic_work_record.transform_keys { |key| key.to_s.gsub('_', ' ') }
end
let(:records) { [generic_work_record_with_no_underscores] }

it 'still returns true' do
expect(subject.valid_import?).to be true
end
end

context 'when the csv header is the parser_mappings value' do
let(:generic_work_record_with_type_instead_of_resource_type) do
generic_work_record.transform_keys! { |key| key == :resource_type ? :type : key }
Expand Down

0 comments on commit 2357d3e

Please sign in to comment.