开发者

Setting 'limits' in rails views

An application has users which can have one of three diffe开发者_高级运维rent account types. Let's call them Small, Medium, Large

I'm trying to figure out the best way to set limits depending on the account type. I looked into cancan but it seems to just have basic authentication options like read, manage, etc. whereas I am mostly going to be doing things like:

if user_has_hit_upload_limit?
  # display a message
else
  # display the upload form

What's the best way to do this? Am I correct in thinking cancan isn't ideal for this?

I started making a model class that sets all the limits and does all the checks, then added methods to ApplicationHelper to call that class, which is in turn called by the views.

Does this seem reasonable or not good? Is there a better way?


CanCan is for authorization based on abilities defined for users. It's probably not what you want for a quota type system like you described. Assuming you kept track of current usage and the user's quota both in the user model you could write a helper like this:

def user_has_hit_upload_limit?
  current_user && current_user.upload_count < current_user.upload_limit
end

The hard part is actually tracking the usage and that will depend on what you are trying to do within your application.

You could still use CanCan define an ability that had a block condition but IMHO that's more complexity then it really needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜