Project

at_most

0.0
No commit activity in last 3 years
No release in over 3 years
Limit 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

~> 1.3
>= 0
>= 0

Runtime

 Project Readme

at_most

Travis badge

Code Climate

Coverage Status

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!" })
end

There 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.

  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