@@ -10,11 +10,18 @@ module OAI::Provider
10
10
#
11
11
class ActiveRecordWrapper < Model
12
12
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.
15
21
def initialize ( model , options = { } )
16
22
@model = model
17
23
@timestamp_field = options . delete ( :timestamp_field ) || 'updated_at'
24
+ @identifier_field = options . delete ( :identifier_field ) || model . primary_key || "id"
18
25
@limit = options . delete ( :limit ) || 100
19
26
20
27
unless options . empty?
@@ -54,7 +61,7 @@ def find(selector, options={})
54
61
find_scope . where ( conditions )
55
62
end
56
63
else
57
- find_scope . where ( conditions ) . find ( selector )
64
+ find_scope . where ( conditions ) . find_by! ( identifier_field => selector )
58
65
end
59
66
end
60
67
@@ -129,7 +136,7 @@ def next_set(find_scope, token_string)
129
136
else # end of result set
130
137
find_scope . where ( token_conditions ( token ) )
131
138
. limit ( @limit )
132
- . order ( "#{ model . primary_key } asc" )
139
+ . order ( "#{ identifier_field } asc" )
133
140
end
134
141
end
135
142
@@ -138,9 +145,9 @@ def next_set(find_scope, token_string)
138
145
def select_partial ( find_scope , token )
139
146
records = find_scope . where ( token_conditions ( token ) )
140
147
. limit ( @limit )
141
- . order ( "#{ model . primary_key } asc" )
148
+ . order ( "#{ identifier_field } asc" )
142
149
raise OAI ::ResumptionTokenException . new unless records
143
- offset = records . last . send ( model . primary_key . to_sym )
150
+ offset = records . last . send ( identifier_field )
144
151
145
152
PartialResult . new ( records , token . next ( offset ) )
146
153
end
@@ -157,7 +164,7 @@ def token_conditions(token)
157
164
158
165
return sql if 0 == last
159
166
# Now add last id constraint
160
- sql . first << " AND #{ model . primary_key } > :id"
167
+ sql . first << " AND #{ identifier_field } > :id"
161
168
sql . last [ :id ] = last
162
169
163
170
return sql
0 commit comments