at_most
A simple extension to ActiveRecord to allow limiting of model creation via validation. This gem was extracted out of a Rails app function that limited the number of users that could be created with CRUD access to a different AR model.
Installation
Add this line to your application's Gemfile:
gem 'at_most'
And then execute:
$ bundle
Or install it yourself as:
$ gem install at_most
Usage
# app/models/user.rb
class User < ActiveRecord::Base
  at_most(5)
end# after fifth User instance, in IRB
> User.create!
  ActiveRecord::RecordInvalid: Validation failed: The maximum number of users has
been reached.It's really that simple.
Errors
You can specify custom errors in the model, or through i18n:
# app/models/user.rb
class User < ActiveRecord::Base
  at_most(5, { message: "Sorry, all our spots are full!" })
endThere are two i18n options: a per model setting, and a global setting.
en:
  activerecord:
    errors:
      models:
        users:
          at_most: "All of our spots are full :(:("
        books:
          at_most: "I can't read all of these!"
  at_most: "Something went terribly wrong."Contributing
Running tests -- the default rake task runs the Rspec suite.
- Fork it
 - Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request