Project

allowed

0.02
No commit activity in last 3 years
No release in over 3 years
Throttling of ActiveRecord model creations.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 1.0.0
= 1.5.0
= 10.3.2
= 3.0.0
= 1.3.9

Runtime

 Project Readme

allowed Build Status

Throttle record creation in ActiveRecord.

Example

class Comment < ActiveRecord::Base
  belongs_to :screenshot
  belongs_to :user

  # Custom scopes beyond default created_at attribute.
  allow 10, per: 1.day, scope: :user_id
  allow 5,  per: 1.day, scope: [:screenshot_id, :user_id]

  # Custom error message.
  allow 100, per: 7.days, message: "Too many comments this week."

  # Custom conditions.
  allow 100, per: 7.days, unless: :whitelisted_user?
  allow 100, per: 7.days, unless: -> (comment) { comment.user.admin? }

  # Dynamic limit.
  allow :user_daily_limit, per: 1.day

  # Callbacks when limit is reached.
  allow 10, per: 2.minutes, callback: -> (comment) { comment.user.suspend! }
  allow 25, per: 5.minutes do |comment|
    comment.user.suspend!
  end

  def whitelisted_user?
    user.whitelisted? || screenshot.user == user
  end

  def user_daily_limit
    user.daily_limit
  end
end

Credit

Based on code originally written by Bruce Spang.

License

allowed uses the MIT license. See LICENSE for more details.