Sha256: 0178ba35904e0eb1e69d5831f104800745d3ec6fbc7350373754706d36a087a8

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

module Mongoid
  module Token
    module Collisions
      def resolve_token_collisions(resolver)
        retries = resolver.retry_count
        begin
          yield
        rescue Moped::Errors::OperationFailure => e
          if is_duplicate_token_error?(e, self, resolver.field_name)
            if (retries -= 1) >= 0
              resolver.create_new_token_for(self)
              retry
            end
            raise_collision_retries_exceeded_error resolver.field_name, resolver.retry_count
          else
            raise e
          end
        end
      end

      def raise_collision_retries_exceeded_error(field_name, retry_count)
        Rails.logger.warn "[Mongoid::Token] Warning: Maximum token generation retries (#{retry_count}) exceeded on `#{field_name}'." if defined?(Rails)
        raise Mongoid::Token::CollisionRetriesExceeded.new(self, 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)}"/
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mongoid_token-3.0.0 lib/mongoid/token/collisions.rb
mongoid_token-2.2.0 lib/mongoid/token/collisions.rb
mongoid_token-2.1.2 lib/mongoid/token/collisions.rb
mongoid_token-2.1.1 lib/mongoid/token/collisions.rb
mongoid_token-2.1.0 lib/mongoid/token/collisions.rb
mongoid_token-2.0.2 lib/mongoid/token/collisions.rb