Search code examples
ruby-on-railsrubygemsuser-accounts

Is there a Rails gem for management of a "multi-visit pass" scenario?


I'm a Rails noob. I'm looking to implement an application where users can purchase a multi-visit pass, then spend the credits week-by-week.

For example, register and login, then purchase 10 visits at a gym - the system should list 10 remaining visits. Sign up to a class and 9 remaining visits are listed. When the credits are low, remind the user to top them up with another 10-visit pass, etc.

I know I can use Devise and CanCan to manage the authentication and authorisation aspects.

My question is whether there's already a gem to handle the management of the user's credits, or whether I'd need to write this from scratch.

I've searched https://rubygems.org/gems/rails with no luck, but it's entirely possible I'm missing something obvious.


Solution

  • I don't think there is a gem to do that, but it should be pretty simple to code:

    • Add remaining_visits to your User model and table.
    • Do current_user.update(remaining_visits: current_user.remaining_visits+10) when a ticket is purchased.
    • Copy Devise sessions controller into app/controllers/devise/sessions_controller.rb.
    • Inside this controller, add this kind of code to create (where the user logs in): current_user.update(remaining_visits: current_user.remaining_visits-1).

    Note: Instead of copying Devise sessions controller you can just overwrite the create action.