@@ -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
0 commit comments