Skip to content

Commit

Permalink
Added token retry exception
Browse files Browse the repository at this point in the history
  • Loading branch information
thetron committed Jun 7, 2011
1 parent 7c52bd5 commit 8783f2d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 16 additions & 0 deletions lib/mongoid/token/exceptions.rb
@@ -0,0 +1,16 @@
module Mongoid
module Token
class Error < StandardError; end

class CollisionRetriesExceeded < Error
def initialize(model, retries)
@model = model
@retries = retries
end

def to_s
"Failed to generate unique token for #{@model.to_s} after #{@retries} attempts."
end
end
end
end
5 changes: 2 additions & 3 deletions lib/mongoid_token.rb
Expand Up @@ -8,10 +8,10 @@ def token(*args)
options[:length] ||= 4
options[:retry] ||= 3
options[:contains] ||= :alphanumeric
options[:index] = true unless options.has_key?(:index)
options[:index] = true unless options.has_key?(:index) && options[:index] == false

self.field :token, :type => String
self.index :token, :unique => true if options[:index]
self.index :token, :unique => true# if options[:index]

set_callback(:create, :before) do |document|
document.create_token(options[:length], options[:contains])
Expand All @@ -35,7 +35,6 @@ def to_param
protected
def create_token(length, characters)
self.token = self.generate_token(length, characters)# while self.token.nil? || self.class.exists?(:conditions => {:token => self.token})
puts self.class.find_by_token(self.token)
end

def create_token_if_nil(length, characters)
Expand Down

0 comments on commit 8783f2d

Please sign in to comment.