Search code examples
ruby-on-railsrubyuser-interfacedesign-patternsprivileges

Creating multiple dashboards for a half dozen user types in Rails


I'm working on a project that involves seven different kinds of users. These users each have a very similar-looking dashboard.

They're almost exactly the same for each user, but do differ slightly:

  • Higher-tier users have certain buttons visible
  • The table layout includes extra columns
  • Some actions related to table entries are restricted to normal users

I'd love to make the code more DRY but how do I approach this problem if partials will complicate the layout rather than simplify it, in this case?

My initial idea was to display ALL the elements, but hide/restrict them with if statements, making for one, but very logic-heavy view.

More broadly speaking, if the UI changes significantly based on the privilege level of the user viewing it, what's the accepted Rails pattern for keeping things organized?


Solution

  • I think Pundit will be the perfect fit for you if DRY and OO are the concerns :). It is really super object-oriented and clean approach.

    Essentially, you'll move all the (often complicated) authorization logic to policies, and you're views will look like this (from docs):

    <% if policy(@post).update? %>
      <%= link_to "Edit post", edit_post_path(@post) %>
    <% end %>