No commit activity in last 3 years
No release in over 3 years
before_create helper to create unique idenfication fields
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0
>= 0
>= 0
 Project Readme

Unique Identifier

make unique identifier fields quick and simple

Installation

Add this line to your application's Gemfile:

gem 'unique_identifier'

And then execute:

$ bundle

Or install it yourself as:

$ gem install unique_identifier

Usage

  class SomeModel < ActiveRecord::Base

    # name of column to store unique identifier
    #            |
    #         -------
    unique_id :number, -> { Array.new(9) { rand(9) }.join }
    #                  ------------------------------------
    #                                    |
    # proc to be run `before_validation, on: :create` to generate unique identifier

  end

  instance = SomeModel.create
  instance.number # => "324516542"

How it works

The above example is equivalent to this:

  class SomeModel < ActiveRecord::Base

    before_validation :generate_unique_id, on: :create

    def generate_unique_id
      self.number = loop do
        random = Array.new(9) { rand(9) }.join
        break random unless self.class.exists?(number: random)
      end
    end

  end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request