Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: bdfb5828cd
Fetching contributors…

Cannot retrieve contributors at this time

36 lines (30 sloc) 0.835 kb
module Uniquify
def self.included(base)
base.extend ClassMethods
end
def ensure_unique(name)
begin
self[name] = yield
end while self.class.exists?(name => self[name])
end
module ClassMethods
def uniquify(*args, &block)
options = { :length => 8, :chars => ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a }
options.merge!(args.pop) if args.last.kind_of? Hash
args.each do |name|
before_validation :on => :create do |record|
if block
record.ensure_unique(name, &block)
else
record.ensure_unique(name) do
Array.new(options[:length]) { options[:chars].to_a[rand(options[:chars].to_a.size)] }.join
end
end
end
end
end
end
end
class ActiveRecord::Base
include Uniquify
end
Jump to Line
Something went wrong with that request. Please try again.