Uniqueable

Build Status

Uniqueable allows the checking of cross-model and cross-attribute uniqueness check. For example, if you have users and organisations, both of which have a name that is used in the url, then you will want to check uniqueness across both of the models.

Installation

Add this line to your application's Gemfile:

gem 'uniqueable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install uniqueable

Usage

Uniqueable adds a uniqueable method and validator to your models. Uniqueable is most useful when validating uniqueness across multiple Models and fields.

class User < ActiveRecord::Base
  include Uniqueable
  uniquable :username
  validates :username, :uniqueable => true
end

class Organsation < ActiveRecord::Base
  include Uniqueable
  uniquable :alias
  validates :alias, :uniqueable => true
end

user = User.create(:username => "gazler")                 # Works
organisation = Organisation.create(:alias => "gazler")    # Does not work
organisation.errors.messages                              # {:alias => ["has already been taken"]}

Groups

In a large application, you may want to check uniqueness for multiple groups of things. Uniqueable includes groups for this:

class User < ActiveRecord::Base
  include Uniqueable
  uniquable :username, :group => :names_for_stuff
  validates :username, :uniqueable => {:group => :names_for_stuff}
end

class Organsation < ActiveRecord::Base
  include Uniqueable
  uniquable :alias, :group => :names_for_stuff
  validates :alias, :uniqueable => {:group => :names_for_stuff}
end

class AdminUser < ActiveRecord::Base
  include Uniqueable
  uniquable :alias, :group => :names_for_stuff
  uniquable :alias
  validates :uniquable
end


admin_user = AdminUser.create(:alias => "gazler")         # Works
user = User.create(:username => "gazler")                 # Does not work
organisation = Organisation.create(:alias => "gazler")    # Does not work
admin_user = AdminUser.create(:alias => "gazler")         # Does not work

Conditions

Sometimes you will only want a field to be checked if certain conditions are satisfied. Let's say we only want public Organisations to be validated against with a user.

class User < ActiveRecord::Base
  include Uniqueable
  uniquable :username, :group => :names_for_stuff
  validates :username, :uniqueable => {:group => :names_for_stuff}
end

class Organsation < ActiveRecord::Base
  include Uniqueable
  uniquable :alias, :group => :names_for_stuff, :conditions => ["public = ?", true]
  validates :alias, :uniqueable => {:group => :names_for_stuff}
end

Organisation.create(:alias => "gazler", :public => true)         # Works
Organisation.create(:alias => "powershift", :public => false)    # Works
User.create(:username => "powershift")                           # Works
User.create(:username => "gazler")                               # Does Not Work

Other options

The case_sensitive option can be sent through with the validator. It defaults to true.

class User < ActiveRecord::Base
  include Uniqueable
  uniquable :username, :group => :names_for_stuff
  validates :username, :uniqueable => {:group => :names_for_stuff, :case_sensitive => false}
end

Notes

If a group is not set then everything will act in the nil group. That is, all uniqueables without a group will validate against all other uniqueables without a group.

Contributing

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