0

I have a rails application with 2 roles, say admin and user. But the thing is, The admin doesn't use a backend like ActiveAdmin for example. I want both Admin and User to see the same views, but depending on the role, I restrict what they can see. I'm using Cancan, but since for example both admin and user can see the product page, I end up with many conditions inside the view and controller actions stating for example if this is an admin show that, if not then show that instead.

So I don't really think that this is the "Rails way". I end up with many repeated code, and code inside the views which doesn't really support the idea of keeping the logic away from the views.

So my question is, What's the best way to implement such a scenario with many roles but the same views.

Thank you.


I'm thinking of two options currently, but I don't like either. One is to redirect the admin to another view, but this way most of the view is the same hence it's not DRY at all. Option 2 is to use the exact same view, but add many conditions in the view, so I end up with a huge complex view with code. I'm trying to find a way that keeps things DRY yet simple, and keeps the views code free.

user1069624
  • 561
  • 2
  • 9
  • 24

2 Answers2

0

You can have the admin module under seperate namespace and users as the default namespace. You can extract the common code under partials and use the same in both admin and user module.

This way you can separate the code for user and admin, and if sometime in future if you decide to go for a different views for admin and user. It won;t be much of a task.

  1. Have controllers as

    app/controllers/admin/articles ------ for admin users

    app/controllers/articles ---- for normal users

and

  1. views

    app/views/admin/articles

    app/views/articles

    app/views/shared

Ross
  • 1,562
  • 1
  • 15
  • 26
  • Ok, that seems a lot neater, but now do I have to pass by all the files and append an admin_ at the beginning of all paths? Is there an easier way to do that? – user1069624 Oct 23 '12 at 11:44
  • Check out this links for better idea http://stackoverflow.com/questions/119197/the-rails-way-namespaces http://www.purpleworkshops.com/articles/grouped-controllers .In short you are just seperating them actions done by normal user and admin by namespace – Ross Oct 23 '12 at 11:53
0

There are different possible approaches. A variation of the 'decorator' pattern would come to mind as described here

It's some time I last read it, but I think the basic idea is to put the logic in the model or helpers. As soon as a user logs in and you can see if he is an admin or normal user you have methods that return the necessary html or text.

A more simple approach would be to just have two partials for everything. Either as Ross says in a separate admin namespace or even simpler (but more work if you later need to change that) just like _user_view_something.html.erb and _admin_view_something.html.erb.

Somewhat similar thoughts go into the DCI pattern. This Blog gives some nice overview how this could play into Rails. The article is more about permissions, but again the basic idea is to supplement the user object with the necessary functions.

So you have the basic "do_something" method which renders something or places some information in the view and replace it with the one that fits to the role the actual user has.

thorsten müller
  • 5,621
  • 1
  • 22
  • 30