Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Creating token before the record validation
Browse files Browse the repository at this point in the history
  • Loading branch information
reu committed Dec 22, 2010
1 parent 0711699 commit a56df92
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/uniquify.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ module Uniquify
def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
end end

def ensure_unique(name) def ensure_unique(name)
begin begin
self[name] = yield self[name] = yield
end while self.class.exists?(name => self[name]) end while self.class.exists?(name => self[name])
end end

module ClassMethods module ClassMethods

def uniquify(*args, &block) def uniquify(*args, &block)
options = { :length => 8, :chars => ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a } 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 options.merge!(args.pop) if args.last.kind_of? Hash
args.each do |name| args.each do |name|
before_create do |record| before_validation :on => :create do |record|
if block if block
record.ensure_unique(name, &block) record.ensure_unique(name, &block)
else else
Expand All @@ -26,7 +26,7 @@ def uniquify(*args, &block)
end end
end end
end end

end end
end end


Expand Down

0 comments on commit a56df92

Please sign in to comment.