Module: Randomid::ModelAdditions

Defined in:
lib/randomid/model_additions.rb

Instance Method Summary collapse

Instance Method Details

#unique_identifier(attribute, length = 16) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/randomid/model_additions.rb', line 5

def unique_identifier(attribute, length=16)
  before_create do 
    loop do
      token = Randomid.generate_uid(length)
      break send("#{attribute}=", token) unless self.class.exists?("#{attribute}" => token)
    end
  end
end