Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

MongoMapper + ActiveRecord Unique Value Generator

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 unique_generator.gemspec
README.md

Unique Generator Build Status Dependency Status

Introduction

Unique Generator is a plugin for ActiveRecord and MongoMapper which makes it simple to generate unique fields and unique tokens e.g. for invites, api keys and the like.

Unique Generator is only available for Rails 3.0+.

Installation

Installing Unique Generator is simple, just add the following to your Gemfile:

gem 'unique_generator', '~> 1.0'

We'll automatically hook into ActiveRecord and MongoMapper via a railtie, adding three methods.

Usage

Unique Generator defines a very simple method, adding only four methods:

  • YourModelClass.generate_random(length = 32) - Generates a case sensitive field with size characters.
  • YourModelClass.generate_unique(length = 32, &block) - Taking a block which defines the predicate of a given value, it will generate a random toke of the specified length until the block returns true.
  • YourModelClass#unique_field?(name) - Called on an instance, tells you whether that instances value for a given field is unique in the database.

And finally, for most people, the only method that matters:

  • YourModelClass#generate_unique_field!(name, length = 32) - Generates a random token of the specified length, combining the above methods to ensure it has a unique value.

Note: Please keep in mind, in your migration your field accepting a random token should ideally be case sensitive.

Example

As a simple example, say we want to generate a unique invite token on a model, to do this, we'd do something like:

class Invite < ActiveRecord::Base

  before_save :generate_invite_token

  def generate_invite_token
    generate_unique_field! :token, 32 if token.blank?
  end

end

Contributors

Contributing

We encourage all community contributions. Keeping this in mind, please follow these general guidelines when contributing:

  • Fork the project
  • Create a topic branch for what you’re working on (git checkout -b awesome_feature)
  • Commit away, push that up (git push your_remote awesome_feature)
  • Create a new GitHub Issue with the commit, asking for review. Alternatively, send a pull request with details of what you added.
  • Once it’s accepted, if you want access to the core repository feel free to ask! Otherwise, you can continue to hack away in your own fork.

Other than that, our guidelines very closely match the GemCutter guidelines here.

(Thanks to GemCutter for the contribution guide)

License

RocketPants is released under the MIT License (see the license file) and is copyright Filter Squad, 2012.

Something went wrong with that request. Please try again.