File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed
test/activerecord_provider Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 213
213
# end
214
214
# end
215
215
# ```
216
+ # ### Scopes for restrictions or eager-loading
217
+ #
218
+ # Instead of passing in a Model class to OAI::Provider::ActiveRecordWrapper, you can actually
219
+ # pass in any scope (or ActiveRecord::Relation). This means you can use it for restrictions:
220
+ #
221
+ # OAI::Provider::ActiveRecordWrapper.new(Post.where(published: true))
222
+ #
223
+ # Or eager-loading an association you will need to create serialization, to avoid n+1 query
224
+ # performance problems:
225
+ #
226
+ # OAI::Provider::ActiveRecordWrapper.new(Post.includes(:categories))
227
+ #
228
+ # Or both of those in combination, or anything else that returns an ActiveRecord::Relation,
229
+ # including using custom scopes, etc.
230
+ #
216
231
# ### Sets?
217
232
#
218
233
# There is some code written to support oai-pmh "sets" in the ActiveRecord::Wrapper, but
Original file line number Diff line number Diff line change @@ -12,6 +12,15 @@ class ARProvider < OAI::Provider::Base
12
12
source_model ActiveRecordWrapper . new ( DCField )
13
13
end
14
14
15
+ class ARProviderWithScope < OAI ::Provider ::Base
16
+ DATE_LESS_THAN_RESTRICTION = Time . parse ( "2007-03-12 19:30:22 UTC" )
17
+
18
+ repository_name 'ActiveRecord Based Provider'
19
+ repository_url 'http://localhost'
20
+ record_prefix 'oai:test'
21
+ source_model ActiveRecordWrapper . new ( DCField . where ( "date < ?" , DATE_LESS_THAN_RESTRICTION ) . includes ( :sets ) )
22
+ end
23
+
15
24
class SimpleResumptionProvider < OAI ::Provider ::Base
16
25
repository_name 'ActiveRecord Resumption Provider'
17
26
repository_url 'http://localhost'
Original file line number Diff line number Diff line change @@ -28,6 +28,18 @@ def test_list_records
28
28
assert_equal 100 , doc . elements [ 'OAI-PMH/ListRecords' ] . to_a . size
29
29
end
30
30
31
+ def test_list_records_scope
32
+ @provider = ARProviderWithScope . new
33
+
34
+ doc = nil
35
+ assert_nothing_raised do
36
+ doc = REXML ::Document . new ( @provider . list_records ( :metadata_prefix => 'oai_dc' ) )
37
+ end
38
+
39
+ expected_count = DCField . where ( "date < ?" , ARProviderWithScope ::DATE_LESS_THAN_RESTRICTION ) . count
40
+ assert_equal expected_count , doc . elements [ 'OAI-PMH/ListRecords' ] . to_a . size
41
+ end
42
+
31
43
def test_list_identifiers
32
44
assert_nothing_raised { REXML ::Document . new ( @provider . list_identifiers ) }
33
45
doc = REXML ::Document . new ( @provider . list_identifiers )
You can’t perform that action at this time.
0 commit comments