I have those models
User, Developer, App, Permission
- A User can have Default Permision
- A User can have Permission for Different Application
- A user can install Multiple Applications
- A User can also be a Developer of an Application
- A Developer is still a User and can have all user’s privillage (Install applcation, default permission, permissions for each application)
Until now I have:
user.rb
class User < ActiveRecord::Base
has_many :apps
end
app.rb
class App < ActiveRecord::Base
has_many :permissions, :through => :app_permissions
end
permission.rb
class Permission < ActiveRecord::Base
belongs_to :app
end
app_permission.rb
class AppPermission < ActiveRecord::Base
end
Questions
- How to distinguish users? (Regular, Developer) Is it better to use CanCan or Rails STI or Simple Roles Class? Please justify why is better to use any of those three solutions or something else.
- Is it better to create a Default_Permission model to separate application permissions from default permission?
EDIT:
If I miss any information please ask. I would like to see some different solutions and how each solution works. Thanks
I would recommend the following:
Developer is a User object. Distinguish developers from users with a is_developer boolean in your schema. This will make it easier going forward to keep Users / Developers integrated (without switch statements). You can add a named scope to find developers specfically:
Alternatively, you could have User / Developer work as polymorphic associations. E.g.
The downside to this approach is it will make your code more complicated for little or zero semantic gain.
I don’t truly understand what you mean by default permission, but it seems to be a logic issue as opposed to a database. Does everyone have the default permission? Then you can add it on *after_create*, or when writing your logic, assume it’s true (or controlled by a boolean flag). The following code will create a permission for each user that is default true after they are created (for existing users, you can add the permissions by hand / rake task).
As for default_permissions, I would suggest having an *is_default* boolean on the permissions table. This way, you can have multiple default permissions going forward (or remove default permissions later). As a default permission is a permissions, there’s no need to differentiate the object models. I.e.
Finally, make sure to fully spell out all of your ActiveRecord associations, i.e.
When a user installs an app: EDITED BELOW FOR POLYMORPHISM
Hope this helps you work through your problem and let me know if you need any additional information.