Module: Uniqueness::Model::ClassMethods

Defined in:
lib/uniqueness/model.rb

Instance Method Summary collapse

Instance Method Details

#has_unique_field(name, options = {}) ⇒ Object

Adds random field support to Rails models

Examples:

To auto-generate a new random string for field +foo+
has_unique_field :foo

You can customize the generated string by
passing an options hash. The following keys are supported:

+:length+ number of characters, defaults to <tt>32</tt>

+:case_sensitive+ defaults to <tt>true</tt>

+:type+ type of string, defaults to <tt>:auto</tt>
        can be one of: <tt>:human</tt>, <tt>:auto</tt>

+:blacklist+ characters to exclude when generating the random
             string, defaults to <tt>[]</tt>

+:scope+ defines the `ActiveRecord` `scope` applied before
         calculating the `position` field value.
         defaults to <tt>[]</tt>


32
33
34
35
36
37
# File 'lib/uniqueness/model.rb', line 32

def has_unique_field(name, options = {})
  self.uniqueness_options ||= {}
  self.uniqueness_options[name] = Uniqueness.uniqueness_default_options.merge(options)
  before_validation :uniqueness_generate
  validate :uniqueness_validation
end