Skip to content

Commit 2962523

Browse files
committed
Merge pull request #37 from mispy/rails-4-compatibility
Rails 4 compatibility
2 parents ebe9263 + 8a2fa08 commit 2962523

12 files changed

+64
-71
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ gemspec
55
gem 'jruby-openssl', :platform => :jruby
66

77
group :test do
8-
gem 'activerecord', '~> 3.2.8'
8+
gem 'activerecord', '~> 4.0.0'
99
gem 'activerecord-jdbcsqlite3-adapter', :platform => [:jruby]
1010
gem 'libxml-ruby', :platform => [:ruby, :mswin]
1111
gem 'rake'

lib/oai/provider/model/activerecord_caching_wrapper.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module OAI::Provider
44

55
# ActiveRecord model class in support of the caching wrapper.
66
class OaiToken < ActiveRecord::Base
7-
has_many :entries, :class_name => 'OaiEntry',
8-
:order => "record_id", :dependent => :destroy
7+
has_many :entries, -> { order("record_id ASC") },
8+
:class_name => 'OaiEntry', :dependent => :destroy
99

1010
validates_uniqueness_of :token
1111

@@ -60,15 +60,15 @@ def find(selector, options={})
6060
conditions = sql_conditions(options)
6161

6262
if :all == selector
63-
total = model.count(:id, :conditions => conditions)
63+
total = model.where(conditions).count
6464
if @limit && total > @limit
6565
select_partial(
6666
ResumptionToken.new(options.merge({:last => 0})))
6767
else
68-
model.find(:all, :conditions => conditions)
68+
model.where(conditions)
6969
end
7070
else
71-
model.find(selector, :conditions => conditions)
71+
model.where(conditions).find(selector)
7272
end
7373
end
7474

@@ -78,7 +78,7 @@ def next_set(token_string)
7878
raise ResumptionTokenException.new unless @limit
7979

8080
token = ResumptionToken.parse(token_string)
81-
total = model.count(:id, :conditions => token_conditions(token))
81+
total = model.where(token_conditions(token)).count
8282

8383
if token.last * @limit + @limit < total
8484
select_partial(token)
@@ -91,7 +91,7 @@ def next_set(token_string)
9191
# resumption token to get the next subset
9292
def select_partial(token)
9393
if 0 == token.last
94-
oaitoken = OaiToken.find_or_create_by_token(token.to_s)
94+
oaitoken = OaiToken.find_or_create_by(token: token.to_s)
9595
if oaitoken.new_record_before_save?
9696
OaiToken.connection.execute("insert into " +
9797
"#{OaiEntry.table_name} (oai_token_id, record_id) " +
@@ -104,8 +104,9 @@ def select_partial(token)
104104
raise ResumptionTokenException.new unless oaitoken
105105

106106
PartialResult.new(
107-
hydrate_records(oaitoken.entries.find(:all, :limit => @limit,
108-
:offset => token.last * @limit)), token.next(token.last + 1)
107+
hydrate_records(
108+
oaitoken.entries.limit(@limit).offset(token.last * @limit)),
109+
token.next(token.last + 1)
109110
)
110111
end
111112

lib/oai/provider/model/activerecord_wrapper.rb

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def initialize(model, options={})
2525
end
2626

2727
def earliest
28-
earliest_obj = model.find(:first, :order => "#{timestamp_field} asc")
28+
earliest_obj = model.order("#{timestamp_field} asc").first
2929
earliest_obj.nil? ? Time.at(0) : earliest_obj.send(timestamp_field)
3030
end
3131

3232
def latest
33-
latest_obj = model.find(:first, :order => "#{timestamp_field} desc")
33+
latest_obj = model.order("#{timestamp_field} desc").first
3434
latest_obj.nil? ? Time.now : latest_obj.send(timestamp_field)
3535
end
3636
# A model class is expected to provide a method Model.sets that
@@ -46,15 +46,15 @@ def find(selector, options={})
4646
options[:resumption_token]) if options[:resumption_token]
4747
conditions = sql_conditions(options)
4848
if :all == selector
49-
total = find_scope.count(:id, :conditions => conditions)
49+
total = find_scope.where(conditions).count
5050
if @limit && total > @limit
5151
select_partial(find_scope,
5252
ResumptionToken.new(options.merge({:last => 0})))
5353
else
54-
find_scope.find(:all, :conditions => conditions)
54+
find_scope.where(conditions)
5555
end
5656
else
57-
find_scope.find(selector, :conditions => conditions)
57+
find_scope.where(conditions).find(selector)
5858
end
5959
end
6060

@@ -90,19 +90,19 @@ def find_scope(options)
9090

9191
# Find the set or return an empty scope
9292
set = find_set_by_spec(options[:set])
93-
return model.scoped(:limit => 0) if set.nil?
93+
return model.limit(0) if set.nil?
9494

9595
# If the set has a backward relationship, we'll use it
9696
if set.class.respond_to?(:reflect_on_all_associations)
9797
set.class.reflect_on_all_associations.each do |assoc|
98-
return set.send(assoc.name).scoped if assoc.klass == model
98+
return set.send(assoc.name) if assoc.klass == model
9999
end
100100
end
101101

102102
# Search the attributes for 'set'
103103
if model.column_names.include?('set')
104104
# Scope using the set attribute as the spec
105-
model.scoped(:conditions => {:set => options[:set]})
105+
model.where(set: options[:set])
106106
else
107107
# Default to empty set, as we've tried everything else
108108
model.scoped(:limit => 0)
@@ -122,24 +122,23 @@ def next_set(find_scope, token_string)
122122
raise OAI::ResumptionTokenException.new unless @limit
123123

124124
token = ResumptionToken.parse(token_string)
125-
total = find_scope.count(:id, :conditions => token_conditions(token))
125+
total = find_scope.where(token_conditions(token)).count
126126

127127
if @limit < total
128128
select_partial(find_scope, token)
129129
else # end of result set
130-
find_scope.find(:all,
131-
:conditions => token_conditions(token),
132-
:limit => @limit, :order => "#{model.primary_key} asc")
130+
find_scope.where(token_conditions(token))
131+
.limit(@limit)
132+
.order("#{model.primary_key} asc")
133133
end
134134
end
135135

136136
# select a subset of the result set, and return it with a
137137
# resumption token to get the next subset
138138
def select_partial(find_scope, token)
139-
records = find_scope.find(:all,
140-
:conditions => token_conditions(token),
141-
:limit => @limit,
142-
:order => "#{model.primary_key} asc")
139+
records = find_scope.where(token_conditions(token))
140+
.limit(@limit)
141+
.order("#{model.primary_key} asc")
143142
raise OAI::ResumptionTokenException.new unless records
144143
offset = records.last.send(model.primary_key.to_sym)
145144

ruby-oai.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
1111
s.bindir = 'bin'
1212
s.executables = 'oai'
1313

14-
s.add_dependency('builder', '>=2.0.0')
14+
s.add_dependency('builder', '>=3.1.0')
1515
s.add_dependency('faraday')
1616
s.add_dependency('faraday_middleware')
1717

test/activerecord_provider/database/0001_oaipmh_tables.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def self.up
4848
t.column :description, :string
4949
end
5050

51-
add_index :oai_tokens, [:token], :uniq => true
51+
add_index :oai_tokens, [:token], :unique => true
5252
add_index :oai_tokens, :created_at
5353
add_index :oai_entries, [:oai_token_id]
5454
add_index :dc_fields, :updated_at

test/activerecord_provider/helpers/set_provider.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class SetModel < OAI::Provider::ActiveRecordWrapper
33

44
# Return all available sets
55
def sets
6-
DCSet.scoped
6+
DCSet.all
77
end
88

99
end
@@ -21,4 +21,4 @@ class ARExclusiveSetProvider < OAI::Provider::Base
2121
record_prefix = 'oai:test'
2222
source_model OAI::Provider::ActiveRecordWrapper.new(
2323
ExclusiveSetDCField, :timestamp_field => 'date')
24-
end
24+
end

test/activerecord_provider/models/dc_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class DCField < ActiveRecord::Base
2-
inheritance_column = 'DONOTINHERIT'
2+
self.inheritance_column = 'DONOTINHERIT'
33
has_and_belongs_to_many :sets,
44
:join_table => "dc_fields_dc_sets",
55
:foreign_key => "dc_field_id",

test/activerecord_provider/models/exclusive_set_dc_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class ExclusiveSetDCField < ActiveRecord::Base
2-
inheritance_column = 'DONOTINHERIT'
2+
self.inheritance_column = 'DONOTINHERIT'
33

44
def self.sets
55
klass = Struct.new(:name, :spec)

test/activerecord_provider/tc_ar_provider.rb

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_metadata_formats
1313
end
1414

1515
def test_metadata_formats_for_record
16-
record_id = DCField.find(:first).id
16+
record_id = DCField.first.id
1717
assert_nothing_raised { REXML::Document.new(@provider.list_metadata_formats(:identifier => "oai:test/#{record_id}")) }
1818
doc = REXML::Document.new(@provider.list_metadata_formats)
1919
assert doc.elements['/OAI-PMH/ListMetadataFormats/metadataFormat/metadataPrefix'].text == 'oai_dc'
@@ -35,7 +35,7 @@ def test_list_identifiers
3535
end
3636

3737
def test_get_record
38-
record_id = DCField.find(:first).id
38+
record_id = DCField.first.id
3939
assert_nothing_raised do
4040
REXML::Document.new(@provider.get_record(
4141
:identifier => "oai:test/#{record_id}", :metadata_prefix => 'oai_dc'))
@@ -46,7 +46,7 @@ def test_get_record
4646
end
4747

4848
def test_deleted
49-
record = DCField.find(:first)
49+
record = DCField.first
5050
record.deleted = true;
5151
record.save
5252
doc = REXML::Document.new(@provider.get_record(
@@ -56,19 +56,19 @@ def test_deleted
5656
end
5757

5858
def test_from
59-
first_id = DCField.find(:first, :order => "id asc").id
60-
DCField.update_all(['updated_at = ?', Time.parse("January 1 2005")],
61-
"id < #{first_id + 90}")
62-
DCField.update_all(['updated_at = ?', Time.parse("June 1 2005")],
63-
"id < #{first_id + 10}")
59+
first_id = DCField.order("id asc").first.id
60+
DCField.where("id < #{first_id + 90}").update_all(updated_at: Time.parse("January 1 2005"))
61+
62+
DCField.where("id < #{first_id + 10}").update_all(updated_at: Time.parse("June 1 2005"))
63+
6464

6565
from_param = Time.parse("January 1 2006")
6666

6767
doc = REXML::Document.new(
6868
@provider.list_records(
6969
:metadata_prefix => 'oai_dc', :from => from_param)
7070
)
71-
assert_equal DCField.find(:all, :conditions => ["updated_at >= ?", from_param]).size,
71+
assert_equal DCField.where(["updated_at >= ?", from_param]).size,
7272
doc.elements['OAI-PMH/ListRecords'].size
7373

7474
doc = REXML::Document.new(
@@ -79,9 +79,8 @@ def test_from
7979
end
8080

8181
def test_until
82-
first_id = DCField.find(:first, :order => "id asc").id
83-
DCField.update_all(['updated_at = ?', Time.parse("June 1 2005")],
84-
"id < #{first_id + 10}")
82+
first_id = DCField.order("id asc").first.id
83+
DCField.where("id < #{first_id + 10}").update_all(updated_at: Time.parse("June 1 2005"))
8584

8685
doc = REXML::Document.new(
8786
@provider.list_records(
@@ -91,12 +90,10 @@ def test_until
9190
end
9291

9392
def test_from_and_until
94-
first_id = DCField.find(:first, :order => "id asc").id
95-
DCField.update_all(['updated_at = ?', Time.parse("June 1 2005")])
96-
DCField.update_all(['updated_at = ?', Time.parse("June 15 2005")],
97-
"id < #{first_id + 50}")
98-
DCField.update_all(['updated_at = ?', Time.parse("June 30 2005")],
99-
"id < #{first_id + 10}")
93+
first_id = DCField.order("id asc").first.id
94+
DCField.update_all(updated_at: Time.parse("June 1 2005"))
95+
DCField.where("id < #{first_id + 50}").update_all(updated_at: Time.parse("June 15 2005"))
96+
DCField.where("id < #{first_id + 10}").update_all(updated_at: Time.parse("June 30 2005"))
10097

10198
doc = REXML::Document.new(
10299
@provider.list_records(

test/activerecord_provider/tc_ar_sets_provider.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_set_ab
2828
end
2929

3030
def test_record_with_multiple_sets
31-
record = DCSet.find(:first, :conditions => "spec = 'C'").dc_fields.first
31+
record = DCSet.where("spec = 'C'").first.dc_fields.first
3232
assert_equal 2, record.sets.size
3333
end
3434

@@ -51,22 +51,22 @@ def define_sets
5151
set_ab = DCSet.create(:name => "Set A:B", :spec => "A:B")
5252

5353
next_id = 0
54-
DCField.find(:all, :limit => 10, :order => "id asc").each do |record|
54+
DCField.limit(10).order("id asc").each do |record|
5555
set_a.dc_fields << record
5656
next_id = record.id
5757
end
5858

59-
DCField.find(:all, :limit => 10, :order => "id asc", :conditions => "id > #{next_id}").each do |record|
59+
DCField.where("id > #{next_id}").limit(10).order("id asc").each do |record|
6060
set_b.dc_fields << record
6161
next_id = record.id
6262
end
6363

64-
DCField.find(:all, :limit => 10, :order => "id asc", :conditions => "id > #{next_id}").each do |record|
64+
DCField.where("id > #{next_id}").limit(10).order("id asc").each do |record|
6565
set_ab.dc_fields << record
6666
next_id = record.id
6767
end
6868

69-
DCField.find(:all, :limit => 10, :order => "id asc", :conditions => "id > #{next_id}").each do |record|
69+
DCField.where("id > #{next_id}").limit(10).order("id asc").each do |record|
7070
set_a.dc_fields << record
7171
set_c.dc_fields << record
7272
next_id = record.id
@@ -117,25 +117,25 @@ def setup
117117
def define_sets
118118
next_id = 0
119119

120-
ExclusiveSetDCField.find(:all, :limit => 10, :order => "id asc").each do |record|
120+
ExclusiveSetDCField.limit(10).order("id asc").each do |record|
121121
record.set = "A"
122122
record.save!
123123
next_id = record.id
124124
end
125125

126-
ExclusiveSetDCField.find(:all, :limit => 10, :order => "id asc", :conditions => "id > #{next_id}").each do |record|
126+
ExclusiveSetDCField.where("id > #{next_id}").limit(10).order("id asc").each do |record|
127127
record.set = "B"
128128
record.save!
129129
next_id = record.id
130130
end
131131

132-
ExclusiveSetDCField.find(:all, :limit => 10, :order => "id asc", :conditions => "id > #{next_id}").each do |record|
132+
ExclusiveSetDCField.where("id > #{next_id}").limit(10).order("id asc").each do |record|
133133
record.set = "A:B"
134134
record.save!
135135
next_id = record.id
136136
end
137137

138-
ExclusiveSetDCField.find(:all, :limit => 10, :order => "id asc", :conditions => "id > #{next_id}").each do |record|
138+
ExclusiveSetDCField.where("id > #{next_id}").limit(10).order("id asc").each do |record|
139139
record.set = "A"
140140
record.save!
141141
next_id = record.id
@@ -155,4 +155,4 @@ def load_fixtures
155155
end
156156
end
157157

158-
end
158+
end

0 commit comments

Comments
 (0)