Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Creating token before the record validation

  • Loading branch information...
commit a56df922cd348b2a95abe75069349447196a5fe5 1 parent 0711699
@reu reu authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.