Skip to content

Commit

Permalink
Add CSV format for elearning index
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Dec 5, 2023
1 parent 0c8784a commit 277fa8c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions app/controllers/materials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions app/views/elearning_materials/index.csv.erb
Original file line number Diff line number Diff line change
@@ -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 %>
14 changes: 14 additions & 0 deletions test/controllers/materials_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 277fa8c

Please sign in to comment.