Browse files

Merge pull request #3 from reu/master

Change to before_validation callback
  • Loading branch information...
2 parents 0711699 + a56df92 commit bdfb5828cde7e206a50dfc94058a05e71ea0153e @ryanb committed Jun 29, 2011
Showing with 5 additions and 5 deletions.
  1. +5 −5 lib/uniquify.rb
View
10 lib/uniquify.rb
@@ -2,20 +2,20 @@ 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_create do |record|
+ before_validation :on => :create do |record|
if block
record.ensure_unique(name, &block)
else
@@ -26,7 +26,7 @@ def uniquify(*args, &block)
end
end
end
-
+
end
end

0 comments on commit bdfb582

Please sign in to comment.