From 8783f2d1dde9704bcce3dced24be1b6e0f833c2d Mon Sep 17 00:00:00 2001 From: Nicholas Bruning Date: Tue, 7 Jun 2011 18:24:09 +1000 Subject: [PATCH] Added token retry exception --- lib/mongoid/token/exceptions.rb | 16 ++++++++++++++++ lib/mongoid_token.rb | 5 ++--- 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 lib/mongoid/token/exceptions.rb diff --git a/lib/mongoid/token/exceptions.rb b/lib/mongoid/token/exceptions.rb new file mode 100644 index 0000000..28d811d --- /dev/null +++ b/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 diff --git a/lib/mongoid_token.rb b/lib/mongoid_token.rb index 819faaf..8e90ad6 100644 --- a/lib/mongoid_token.rb +++ b/lib/mongoid_token.rb @@ -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]) @@ -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)