How to implement plans in your app

I’m trying to implement plans into an SaaS application. Each plan would determine the price of a package and limitations such as number of users allowed, number of emails one is allowed to send, access to different kind of reports etc. I currently have Plan model with couple of subclasses (free, basic & premium) which handle price and description.

I’m guessing I have to build a policy object of some kind (maybe policy isn’t even a proper naming) and do all the checking through that model. So in UsersController I’d have filters like before_action :ensure_creations_allowed to check if Survey.plan.users_limit >= users.count. In ReportsController I’d have checks for each type of the report. Sounds a lot like I’d need something similar to CanCan’s can? checks which I could use in controller and views.

Am I heading into the right direction? I feel like all my controllers would have a lot of before filters each checking a particular limitation? What are your recommendations for solving this kind of problem? I would appreciate any suggestion or tip for tackling this.

Hi @lenartr!

I would start out by building this without using the filter. I can definitely see that in the future you would probably need to use filters but to start building out the logic, I would work directly inline.

Once you’ve built the first flow or two you can probably extract a common ‘policy object’ to handle those types of questions. The Policy would be a nice place to wrap up an action to determine if it is allowed for a plan. Depending on the number of actions that are dependent on the plan you may end up with a few different objects to handle this behavior.

Once you have the object ready, using a before filter may make sense or you may see that it is cleaner and easier to use inline.