Skip to content

Commit c784f2f

Browse files
authored
Merge pull request #1257 from pan-training/space_aware_oai_pmh
OAI-PMH endpoint now returns only materials from current space, Closes #1256
2 parents c55ac0f + 3b018d0 commit c784f2f

4 files changed

Lines changed: 33 additions & 10 deletions

File tree

app/controllers/oai_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class OaiController < ApplicationController
55

66
# GET /oai-pmh
77
def index
8-
provider = TrainingProvider.new
8+
provider = TrainingProvider.new({ provider_context: :instance_based })
9+
provider.model = OAI::Provider::ActiveRecordWrapper.new(Material.in_current_space.where(visible: true))
910
response = provider.process_request(oai_params.to_h)
1011

1112
# add XSLT prefix

config/initializers/oai_provider.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,3 @@ class TrainingProvider < OAI::Provider::Base
2121

2222
register_format(OAIRDF.instance)
2323
end
24-
25-
Rails.application.config.after_initialize do
26-
TrainingProvider.source_model OAI::Provider::ActiveRecordWrapper.new(Material.where(visible: true))
27-
rescue ActiveRecord::ActiveRecordError
28-
Rails.logger.debug 'There is no database yet or some other error, so the OAI-PMH endpoint is not configured.'
29-
end

test/controllers/oai_controller_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ class OaiControllerTest < ActionDispatch::IntegrationTest
5353
assert_includes identifiers, @material.doi
5454
end
5555

56+
test 'OAI-PMH endpoint respects current space' do
57+
get '/oai-pmh', params: { verb: 'ListRecords', metadataPrefix: 'oai_dc' }
58+
parsed = Nokogiri::XML(@response.body)
59+
titles = parsed.xpath('//dc:title', @ns).map(&:text)
60+
assert_includes titles, materials(:training_material).title
61+
assert_includes titles, materials(:plant_space_material).title
62+
63+
plant_space = spaces(:plants)
64+
with_host(plant_space.host) do
65+
get '/oai-pmh', params: { verb: 'ListRecords', metadataPrefix: 'oai_dc' }
66+
parsed = Nokogiri::XML(@response.body)
67+
titles = parsed.xpath('//dc:title', @ns).map(&:text)
68+
refute_includes titles, materials(:training_material).title
69+
assert_includes titles, materials(:plant_space_material).title
70+
end
71+
end
72+
5673
test 'OAI ListRecords returns material in rdf format' do
5774
get '/oai-pmh', params: { verb: 'ListRecords', metadataPrefix: 'rdf' }
5875
assert_response :success

test/test_helper.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,22 @@ def with_net_connection(&block)
9696
end
9797

9898
def with_host(host, &block)
99-
original_host = @request.host
100-
@request.host = host
99+
if respond_to?(:host=)
100+
# ActionDispatch::IntegrationTest
101+
original_host = self.host
102+
self.host = host
103+
else
104+
# ActionController::TestCase
105+
original_host = @request.host
106+
@request.host = host
107+
end
101108
block.call
102109
ensure
103-
@request.host = original_host
110+
if respond_to?(:host=)
111+
self.host = original_host
112+
else
113+
@request.host = original_host
114+
end
104115
end
105116

106117
# reset dictionaries to their default values

0 commit comments

Comments
 (0)