Project

pretty_id

0.0
No commit activity in last 3 years
No release in over 3 years
Add random, unique "pretty" ids to your ActiveRecord models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 0
 Project Readme

pretty_id

Travis Code Climate Coveralls RubyGem

Add random, unique "pretty" ids to your ActiveRecord models.

Usage

1. Install the gem

gem 'pretty_id'

2. Add a pretty_id column (and index!) to your model

add_column :books, :pretty_id, :string
add_index :books, :pretty_id

3. Add has_pretty_id to your model

class Book < ActiveRecord::Base
  has_pretty_id
end

Generation methods

:pretty (default)

chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
Array.new(options[:length]) { chars[rand(chars.length)] }.join

:urlsafe_base64

SecureRandom.urlsafe_base64(options[:length] / 1.333)

Options

class Book < ActiveRecord::Base
  has_pretty_id method: :urlsafe_base64,          # default: :pretty
                column: :another_string_column,   # default: :pretty_id
                length: 12,                       # default: 8
                uniq: false                       # default: true
end

Instance methods

#regenerate_#{column_name}

user = User.create
user.pretty_id # => 'a0923sdf'
user.regenerate_pretty_id
user.pretty_id # => 'lf91fs9s'

#regenerate_#{column_name}!

Same as above, but also calls save on the record