NUBIC/surveyor — pull request #370 by yipdw (2012-10-12 01:25:24+00:00)
Title: Eliminate unnecessary (and incorrect) access code uniqueness checks

This set of commits eliminates the find-loop done in ResponseSet's initializer.

IMO, `.new` on ActiveRecord models shouldn't kick off database queries. However, probably more important than that is that _the entire check is ill-conceived_ and is just slowing down ResponseSet initialization; the access code uniqueness check is subject to the same fundamental problem affecting `validates_uniqueness_of`.

This pull request replaces the access code generator with one using `SecureRandom` and provides an analysis of its collision probability. I find it to be sufficiently low (given 10,000 response sets, there's a 1:1.5 billion chance of a collision); however, I would appreciate a double-check on the math and the arithmetic.

Note, too, that even with its current code, Surveyor is still prone to access code collisions. To truly handle collisions, Surveyor needs to code to catch a uniqueness violation as signaled by the database, generate a new access code, and restart the transaction.