ruby-on-railsdevise-invitable

Rails 5: set daily invitation limit with devise_invitable


Recently I installed devise_invitable and now I would like to add a daily invitation_limit.

I know that I can make some configurations to devise_invitable in my config/initializers/devise.rb file.

Currently my devise.rb file looks like this:

config.invitation_limit = 5

I thinking of a way to set a daily limit, probably something like this:

  config.invitation_limit = Time.zone.now.beginning_of_day..Time.zone.now.end_of_day.5

Solution

  • Slightly hacky perhaps but you could use the existing mechanisms for limiting invitations and just reset them each day.

    By default invitable adds an invitation_limit column to users which records the number of invitations they have left. If you had an scheduled task to reset that each day to however many invitations you want to allow per day then it would 'just work' I think.

    The less hacky way would be to add your own invitation_daily_limit to users and to check that in an overridden has_invitations_left? method on User and decrement it in decrement_invitation_limit! too.

    You can see those methods in devise_invitable: https://github.com/scambra/devise_invitable/blob/master/lib/devise_invitable/inviter.rb.

    You'd still need to do something to reset the daily limit though and that'd probably be easiest with a scheduled task.