Skip to content

Commit

Permalink
Merge pull request #66 from wa0x6e/master
Browse files Browse the repository at this point in the history
Update for Mongoid 5
  • Loading branch information
thetron committed Nov 10, 2015
2 parents 50edba6 + 669bbb3 commit e58620a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -20,7 +20,13 @@ Into something more like this:

In your gemfile, add:

gem 'mongoid_token', '~> 2.0.0'
```ruby
# For mongoid < 5
gem 'mongoid_token', '~> 3.0.0'

# For mongoid >= 5
gem 'mongoid_token', '~> 4.0.0'
```

Then update your bundle

Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/token/collisions.rb
Expand Up @@ -5,7 +5,7 @@ def resolve_token_collisions(resolver)
retries = resolver.retry_count
begin
yield
rescue Moped::Errors::OperationFailure => e
rescue Mongo::Error::OperationFailure => e
if is_duplicate_token_error?(e, self, resolver.field_name)
if (retries -= 1) >= 0
resolver.create_new_token_for(self)
Expand All @@ -24,9 +24,9 @@ def raise_collision_retries_exceeded_error(field_name, retry_count)
end

def is_duplicate_token_error?(err, document, field_name)
[11000, 11001].include?(err.details['code']) &&
err.details['err'] =~ /dup key/ &&
err.details['err'] =~ /"#{document.send(field_name)}"/
err.message =~ /(11000|11001)/ &&
err.message =~ /dup key/ &&
err.message =~ /"#{document.send(field_name)}"/
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
@@ -1,3 +1,3 @@
module MongoidToken
VERSION = "3.0.0"
VERSION = "4.0.0"
end
2 changes: 1 addition & 1 deletion mongoid_token.gemspec
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |s|
s.description = %q{Mongoid token is a gem for creating random, unique tokens for mongoid documents. Highly configurable and great for making URLs a little more compact.}

s.rubyforge_project = "mongoid_token"
s.add_dependency 'mongoid', '~> 4.0.0'
s.add_dependency 'mongoid', '~> 5.0.0'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
10 changes: 5 additions & 5 deletions spec/mongoid/token/collisions_spec.rb
Expand Up @@ -17,7 +17,7 @@
it "should raise an error after the first try" do
resolver.stub(:retry_count).and_return(0)
attempts = 0
expect{document.resolve_token_collisions(resolver) { attempts += 1; raise Moped::Errors::OperationFailure.new("","") }}.to raise_error Mongoid::Token::CollisionRetriesExceeded
expect{document.resolve_token_collisions(resolver) { attempts += 1; raise Mongo::Error::OperationFailure }}.to raise_error Mongoid::Token::CollisionRetriesExceeded
expect(attempts).to eq 1
end
end
Expand All @@ -26,7 +26,7 @@
it "should raise an error after retrying once" do
resolver.stub(:retry_count).and_return(1)
attempts = 0
expect{document.resolve_token_collisions(resolver) { attempts += 1; raise Moped::Errors::OperationFailure.new("","") }}.to raise_error Mongoid::Token::CollisionRetriesExceeded
expect{document.resolve_token_collisions(resolver) { attempts += 1; raise Mongo::Error::OperationFailure }}.to raise_error Mongoid::Token::CollisionRetriesExceeded
expect(attempts).to eq 2
end
end
Expand All @@ -35,7 +35,7 @@
it "should raise an error after retrying" do
resolver.stub(:retry_count).and_return(3)
attempts = 0
expect{document.resolve_token_collisions(resolver) { attempts += 1; raise Moped::Errors::OperationFailure.new("","") }}.to raise_error Mongoid::Token::CollisionRetriesExceeded
expect{document.resolve_token_collisions(resolver) { attempts += 1; raise Mongo::Error::OperationFailure }}.to raise_error Mongoid::Token::CollisionRetriesExceeded
expect(attempts).to eq 4
end
end
Expand All @@ -44,7 +44,7 @@
it "should bubble the operation failure" do
document.stub(:is_duplicate_token_error?).and_return(false)
resolver.stub(:retry_count).and_return(3)
e = Moped::Errors::OperationFailure.new("command", {:details => "nope"})
e = Mongo::Error::OperationFailure.new("nope")
expect{document.resolve_token_collisions(resolver) { raise e }}.to raise_error(e)
end
end
Expand Down Expand Up @@ -84,7 +84,7 @@
context "when there is a duplicate key error" do
it "should return true" do
document.stub("token").and_return("tokenvalue123")
err = double("Moped::Errors::OperationFailure")
err = double("Mongo::Error::OperationFailure")
err.stub("details").and_return do
{
"err" => "E11000 duplicate key error index: mongoid_token_test.links.$token_1 dup key: { : \"tokenvalue123\" }",
Expand Down
2 changes: 1 addition & 1 deletion spec/mongoid/token_spec.rb
Expand Up @@ -255,7 +255,7 @@ class UntaintedDocument
it "should raise an operation failure" do
duplicate_name = "Got Duped."
document_class.create!(:name => duplicate_name)
expect{ document_class.create!(:name => duplicate_name) }.to raise_exception(Moped::Errors::OperationFailure)
expect{ document_class.create!(:name => duplicate_name) }.to raise_exception(Mongo::Error::OperationFailure)
end
end
end
Expand Down
10 changes: 3 additions & 7 deletions spec/spec_helper.rb
Expand Up @@ -11,6 +11,8 @@
ENV['MONGOID_ENV'] = "test"

RSpec.configure do |config|
Mongo::Logger.logger.level = Logger::ERROR

config.include Mongoid::Matchers
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
Expand All @@ -23,11 +25,5 @@
end

Mongoid.configure do |config|
config.sessions = {
default: {
database: "mongoid_token_test",
hosts: [ "localhost:#{ENV['BOXEN_MONGODB_PORT'] || 27017}" ],
options: {}
}
}
config.connect_to("mongoid_token_test", {})
end

0 comments on commit e58620a

Please sign in to comment.