Skip to content

Commit f99ef8e

Browse files
committed
ActiveRecordWrapper works with custom identifier_field
With limited test.
1 parent 795ade2 commit f99ef8e

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

lib/oai/provider/model/activerecord_wrapper.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ module OAI::Provider
1010
#
1111
class ActiveRecordWrapper < Model
1212

13-
attr_reader :model, :timestamp_field
14-
13+
attr_reader :model, :timestamp_field, :identifier_field
14+
15+
# If custom 'timestamp_field' is used, be aware this will be an ActiveRecord
16+
# attribute that we will limit on, so perhaps should be indexe appropriately.
17+
#
18+
# If custom `identifier_field` is used, be aware this will be an ActiveRecord
19+
# attribute that we will sort on, and use in WHERE clauses with `=` as well as
20+
# greater than/less than, so should be indexed appropriately.
1521
def initialize(model, options={})
1622
@model = model
1723
@timestamp_field = options.delete(:timestamp_field) || 'updated_at'
24+
@identifier_field = options.delete(:identifier_field) || model.primary_key || "id"
1825
@limit = options.delete(:limit) || 100
1926

2027
unless options.empty?
@@ -54,7 +61,7 @@ def find(selector, options={})
5461
find_scope.where(conditions)
5562
end
5663
else
57-
find_scope.where(conditions).find(selector)
64+
find_scope.where(conditions).find_by!(identifier_field => selector)
5865
end
5966
end
6067

@@ -129,7 +136,7 @@ def next_set(find_scope, token_string)
129136
else # end of result set
130137
find_scope.where(token_conditions(token))
131138
.limit(@limit)
132-
.order("#{model.primary_key} asc")
139+
.order("#{identifier_field} asc")
133140
end
134141
end
135142

@@ -138,9 +145,9 @@ def next_set(find_scope, token_string)
138145
def select_partial(find_scope, token)
139146
records = find_scope.where(token_conditions(token))
140147
.limit(@limit)
141-
.order("#{model.primary_key} asc")
148+
.order("#{identifier_field} asc")
142149
raise OAI::ResumptionTokenException.new unless records
143-
offset = records.last.send(model.primary_key.to_sym)
150+
offset = records.last.send(identifier_field)
144151

145152
PartialResult.new(records, token.next(offset))
146153
end
@@ -157,7 +164,7 @@ def token_conditions(token)
157164

158165
return sql if 0 == last
159166
# Now add last id constraint
160-
sql.first << " AND #{model.primary_key} > :id"
167+
sql.first << " AND #{identifier_field} > :id"
161168
sql.last[:id] = last
162169

163170
return sql

test/activerecord_provider/helpers/providers.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class ARProvider < OAI::Provider::Base
1212
source_model ActiveRecordWrapper.new(DCField)
1313
end
1414

15+
class ARProviderCustomIdentifierField < OAI::Provider::Base
16+
repository_name 'ActiveRecord Based Provider'
17+
repository_url 'http://localhost'
18+
record_prefix 'oai:test'
19+
source_model ActiveRecordWrapper.new(DCField, identifier_field: "source")
20+
end
21+
1522
class ARProviderWithScope < OAI::Provider::Base
1623
DATE_LESS_THAN_RESTRICTION = Time.parse("2007-03-12 19:30:22 UTC")
1724

test/activerecord_provider/tc_ar_provider.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ def test_list_records_scope
4040
assert_equal expected_count, doc.elements['OAI-PMH/ListRecords'].to_a.size
4141
end
4242

43+
44+
def test_get_record_alternate_identifier_column
45+
@provider = ARProviderCustomIdentifierField.new
46+
47+
record_id = DCField.first.send(@provider.class.model.identifier_field)
48+
49+
doc = REXML::Document.new(@provider.get_record(
50+
:identifier => "oai:test:#{record_id}", :metadata_prefix => 'oai_dc'))
51+
52+
assert_equal "oai:test:#{record_id}", doc.elements['OAI-PMH/GetRecord/record/header/identifier'].text
53+
end
54+
4355
def test_list_identifiers
4456
assert_nothing_raised { REXML::Document.new(@provider.list_identifiers) }
4557
doc = REXML::Document.new(@provider.list_identifiers)

0 commit comments

Comments
 (0)