A gem that limits the rate at which ActiveRecord model instances can be created.
The tests for this gem use Rails 5. However, any version of Rails from 3.2 onward should be compatible.
Add the gem to your project's Gemfile:
In the models you want to rate limit simply call the
rate_limit method inside the model.
class ProductReview < ActiveRecord::Base rate_limit end
By default this will rate limit creation of instances using the
ip_address attribute with an interval of one minute. This is a pretty big assumption (that may change in future versions) since there's a good chance you don't have an
ip_address attribute on your model. If that's the case then you can do the following:
class ProductReview < ActiveRecord::Base rate_limit on: :username end
This will instead check for
ProductReviews with a matching
Because you may want to increase or decrease the interval between creating instances of your model you can do this:
class ProductReview < ActiveRecord::Base rate_limit interval: 3.hours end
If you feel like you can add something useful to rate_limiter then don't hesitate to contribute! To make sure your fix/feature has a high chance of being included, please do the following:
Fork the repo.
Run the tests. I will only take pull requests with passing tests, and it's great to know that you have a clean slate:
bundle && rake
Add a test for your change. Only adding tests for existing code, refactoring, and documentation changes require no new tests. If you are adding functionality or fixing a bug, you need a test!
Make the test pass.
Push to your fork and submit a pull request.
I can't guarantee that I will accept the change, but if I don't I will be sure to let you know why!
Some things that will increase the chance that your pull request is accepted, taken straight from the Ruby on Rails guide:
- Use Rails idioms and helpers
- Include tests that fail without your code, and pass with it
- Update the documentation, guides, or whatever is affected by your contribution
Yes, I am well aware of the irony of asking for tests when there are effectively none right now. This gem is a work in progress.