Skip to content

Commit 904a5ad

Browse files
Csv template admin set context (#1119)
* Support context-aware CSV template and validation via admin set context - Pass admin_set_id to CSV template generation and validation so the downloaded template and validation use the admin set's metadata context (HYRAX_FLEXIBLE) - Use Hyrax.schema_for(klass:, admin_set_id:) so context-gated properties (e.g. dimensions for ImageResource in special_context) appear in the template and in validation - Update download template link via JS to include selected admin set - ValkyrieObjectFactory and SchemaAnalyzer use keyword args for schema_for Co-authored-by: Cursor <cursoragent@cursor.com> * Address PR review: spec backward compatibility and link text - In sample_csv_file spec, expect generate_template to be called with admin_set_id: nil when param is omitted - Restore "a" in download link text: "Download a CSV template for your tenant" --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 6ada077 commit 904a5ad

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

app/assets/javascripts/bulkrax/importers_stepper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134

135135
bindEvents()
136136
initAdminSetState()
137+
updateDownloadTemplateLink()
137138
updateStepperUI()
138139
initVisibilityCards()
139140
setDefaultImportName()
@@ -335,6 +336,7 @@
335336
StepperState.adminSetId = $(this).val()
336337
StepperState.settings.adminSetName = $(this).find('option:selected').text()
337338
updateStepNavigation()
339+
updateDownloadTemplateLink()
338340
// Update validate button state since admin set is required for validation
339341
if (StepperState.uploadMode === 'file_path' || StepperState.uploadedFiles.length > 0) {
340342
updateValidateButtonState()
@@ -1638,6 +1640,19 @@
16381640
}
16391641
}
16401642

1643+
function updateDownloadTemplateLink() {
1644+
var $link = $('#download-csv-template-link')
1645+
if (!$link.length) return
1646+
var baseUrl = $link.data('sample-csv-url') || $link.attr('href')
1647+
var adminSetId = $('#importer-admin-set').val()
1648+
var href = baseUrl
1649+
if (adminSetId && adminSetId.trim() !== '') {
1650+
var sep = baseUrl.indexOf('?') >= 0 ? '&' : '?'
1651+
href = baseUrl + sep + 'admin_set_id=' + encodeURIComponent(adminSetId.trim())
1652+
}
1653+
$link.attr('href', href)
1654+
}
1655+
16411656
// Navigate to step
16421657
function goToStep(stepNum) {
16431658
StepperState.currentStep = stepNum

app/controllers/bulkrax/importers_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def new
6969

7070
# GET /importers/sample_csv_file
7171
def sample_csv_file
72-
sample = Bulkrax::CsvValidationService.generate_template(models: 'all', output: 'file')
72+
admin_set_id = params[:admin_set_id].presence
73+
sample = Bulkrax::CsvValidationService.generate_template(models: 'all', output: 'file', admin_set_id: admin_set_id)
7374
send_file sample, filename: File.basename(sample), type: 'text/csv', disposition: 'attachment'
7475
rescue StandardError => e
7576
flash[:error] = "Unable to generate sample CSV file: #{e.message}"

app/views/bulkrax/importers/new_v2.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
<!-- Download Template Link -->
195195
<div class="download-template-link">
196196
<span class="fa fa-download text-primary"></span>
197-
<a href="<%= sample_csv_file_importers_path %>" class="text-primary" download>Download a CSV template for your tenant</a>
197+
<a id="download-csv-template-link" href="<%= sample_csv_file_importers_path %>" class="text-primary" download data-sample-csv-url="<%= sample_csv_file_importers_path %>">Download a CSV template for your tenant</a>
198198
</div>
199199

200200
<!-- Validate Button -->

spec/controllers/bulkrax/importers_controller_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ def current_user
262262
get :sample_csv_file, session: valid_session
263263

264264
expect(response.headers['Content-Type']).to include('text/csv')
265+
expect(Bulkrax::CsvValidationService).to have_received(:generate_template)
266+
.with(models: 'all', output: 'file', admin_set_id: nil)
267+
end
268+
269+
it 'passes admin_set_id to the service for context-aware template when provided' do
270+
allow(Bulkrax::CsvValidationService).to receive(:generate_template).and_return(sample_csv_path)
271+
272+
get :sample_csv_file, params: { admin_set_id: 'admin-set-123' }, session: valid_session
273+
274+
expect(Bulkrax::CsvValidationService).to have_received(:generate_template)
275+
.with(models: 'all', output: 'file', admin_set_id: 'admin-set-123')
265276
end
266277
end
267278

0 commit comments

Comments
 (0)