From f42a1712d73a342e95c5083605acb481c985b717 Mon Sep 17 00:00:00 2001 From: bwatson78 Date: Tue, 5 Dec 2023 12:16:35 -0600 Subject: [PATCH] Moves the loading of Aspace repositories to the controller action (#2168). --- .../collections_controller_override.rb | 27 ++++++++++++++++++- .../hyrax/dashboard/collections/new.html.erb | 21 +++------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/app/controllers/hyrax/dashboard/collections_controller_override.rb b/app/controllers/hyrax/dashboard/collections_controller_override.rb index 67c0c912..8973bcf2 100644 --- a/app/controllers/hyrax/dashboard/collections_controller_override.rb +++ b/app/controllers/hyrax/dashboard/collections_controller_override.rb @@ -18,7 +18,7 @@ def add_new_banner(uploaded_file_ids) banner_info.save f.collection_banner_url end - # [Hyrax-overwrite-v3.0.0.pre.rc1] Restrict deletion to admins only + # [Hyrax-overwrite-v3.4.2] Restrict deletion to admins only def destroy if current_user.admin? && @collection.destroy after_destroy(params[:id]) @@ -26,6 +26,31 @@ def destroy after_destroy_error(params[:id]) end end + + def new + # Coming from the UI, a collection type id should always be present. Coming from the API, if a collection type id is not specified, + # use the default collection type (provides backward compatibility with versions < Hyrax 2.1.0) + @aspace_repositories = retrieve_aspace_repositories + collection_type_id = params[:collection_type_id].presence || default_collection_type.id + @collection.collection_type_gid = CollectionType.find(collection_type_id).to_global_id + add_breadcrumb t(:'hyrax.controls.home'), root_path + add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path + add_breadcrumb t('.header', type_title: collection_type.title), request.path + @collection.try(:apply_depositor_metadata, current_user.user_key) + form + end + + private + + def retrieve_aspace_repositories + service = Aspace::ApiService.new + formatter = Aspace::FormattingService.new + + service.authenticate! + + data = service.fetch_repositories + data.map { |r| formatter.format_repository(r) } || [] + end end end end diff --git a/app/views/hyrax/dashboard/collections/new.html.erb b/app/views/hyrax/dashboard/collections/new.html.erb index a313ac8c..d05f6820 100644 --- a/app/views/hyrax/dashboard/collections/new.html.erb +++ b/app/views/hyrax/dashboard/collections/new.html.erb @@ -20,7 +20,10 @@

Select an ArchivesSpace Repository.

- + <% repositories = @aspace_repositories.presence || [] %> + <%= select_tag('sel1', + options_for_select(repositories.collect{ |ar| [ar[:name], ar[:repository_id]] }), + class: 'form-control') %>
@@ -71,22 +74,6 @@ var result_success; var result_error; - $.ajax({ - url: '/aspace/repositories', - type: 'GET', - dataType: 'json', - async: false, - success: function(repositories) { - jQuery.each( repositories, function(i, val) { - var o = new Option(val.name, val.repository_id); - /// jquerify the DOM object 'o' so we can use the html method - $(o).html(val.name); - $("#sel1").append(o); - repository_id = val.repository_id; - }); - } - }); - $("#aspace-loader").click(function (e) { $("#aspace-error").hide(); $(".aspace-result-empty").empty();