From 277fa8ce5846ee606dbdb21461e98f62e253bfeb Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Tue, 5 Dec 2023 10:51:52 +0000 Subject: [PATCH] Add CSV format for elearning index --- app/controllers/application_controller.rb | 4 ++++ app/controllers/events_controller.rb | 4 ---- app/controllers/materials_controller.rb | 2 ++ app/views/elearning_materials/index.csv.erb | 20 +++++++++++++++++++ test/controllers/materials_controller_test.rb | 14 +++++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 app/views/elearning_materials/index.csv.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1e3aba368..c4b678037 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -127,4 +127,8 @@ def configure_permitted_parameters def allow_embedding response.headers.delete 'X-Frame-Options' end + + def disable_pagination + params[:per_page] = 2 ** 10 + end end diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index cbaabf34a..7c3c99ba3 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -243,8 +243,4 @@ def event_params def event_report_params params.require(:event).permit(:funding, :attendee_count, :applicant_count, :trainer_count, :feedback, :notes) end - - def disable_pagination - params[:per_page] = 2 ** 10 - end end diff --git a/app/controllers/materials_controller.rb b/app/controllers/materials_controller.rb index f3b3a2286..22202dfef 100644 --- a/app/controllers/materials_controller.rb +++ b/app/controllers/materials_controller.rb @@ -4,6 +4,7 @@ class MaterialsController < ApplicationController before_action :set_material, only: [:show, :edit, :update, :destroy, :update_collections, :clone, :add_term, :reject_term, :add_data, :reject_data] before_action :set_breadcrumbs + before_action :disable_pagination, only: :index, if: lambda { |controller| controller.request.format.csv? } include SearchableIndex include ActionView::Helpers::TextHelper @@ -21,6 +22,7 @@ def index respond_to do |format| format.html { render elearning ? 'elearning_materials/index' : 'index' } format.json + format.csv { render 'elearning_materials/index' } if elearning format.json_api { render({ json: @materials }.merge(api_collection_properties)) } end end diff --git a/app/views/elearning_materials/index.csv.erb b/app/views/elearning_materials/index.csv.erb new file mode 100644 index 000000000..ed78caea4 --- /dev/null +++ b/app/views/elearning_materials/index.csv.erb @@ -0,0 +1,20 @@ +<% + fields = [:id, :title, :url, :description, :keywords, :resource_type, :other_types, :scientific_topics, :operations, :fields, :external_resources, :doi, :licence, :version, :status, :contact, :contributors, :authors, :difficulty_level, :target_audience, :prerequisites, :syllabus, :learning_objectives, :subsets, :date_created, :date_modified, :date_published, :created_at, :updated_at].freeze +%> +<%= (['TeSS URL'] + fields.map { |f| Material.human_attribute_name(f) }).to_csv(row_sep: nil) %> +<% @index_resources.each do |material| %> +<%= ([material_url(material)] + fields.map do |f| + value = material.send(f) + value = value.map do |v| + case v + when OntologyTerm + v.label + when ExternalResource + v.url + else + v + end + end.join(', ') if value.respond_to?(:each) + value +end).to_csv(row_sep: nil).html_safe %> +<% end %> diff --git a/test/controllers/materials_controller_test.rb b/test/controllers/materials_controller_test.rb index 714a000d2..3a5cdaa44 100644 --- a/test/controllers/materials_controller_test.rb +++ b/test/controllers/materials_controller_test.rb @@ -1427,4 +1427,18 @@ class MaterialsControllerTest < ActionController::TestCase end end end + + test 'should get e-learning index as csv' do + with_settings(solr_enabled: true, feature: { elearning_materials: true }) do + Material.stub(:search_and_filter, MockSearch.new(Material.all)) do + get :index, params: { resource_type: 'e-learning', format: :csv } + + assert_response :success + end + end + + csv = CSV.parse(@response.body, headers: true) + assert_equal Material.count, csv.length + assert csv.any? { |row| row[0] == material_url(@material) } + end end