Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails controller design pattern for dashboard

I would like to have a Dashboard page that gather information from multiple models to a summary view (without its own model). How should I go about it the Rails way? Should I create a dashboard_controller with just index action?

Thank you.

like image 630
AdamNYC Avatar asked Sep 23 '11 18:09

AdamNYC


2 Answers

just think a little less Rails. i'm assuming your dashboard contains mainly widgets? i.e. some self contained elements, whatever? why not make those widgets your model (not AR-based). encapsulate all your logic and data retrieval in your widget model(s). so now if you want to go RESTful, you could for example make a WidgetsController with an index action as dashboard. in a second step you could use your show action to provide JSON data for each widget for AJAX parts of your dashboard. etc.

just remember REST is not necessarily == CRUD, and a Model doesn't need to be a ActiveRecord model. Rails forces you into the MVC pattern, which is a great thing, and makes even poorly written Rails apps better than a lot of PHP mayhem out there, but sometimes we tend to forget that there are a lot of possibilities outside of ActionController + ActiveRecord etc.

like image 57
Marian Theisen Avatar answered Oct 18 '22 13:10

Marian Theisen


What you would want is to take a look at the administration frameworks and see if that solves your needs, I like Typus and RailsAdmin Otherwise you would want to take a look at the presenter pattern and how it applies to Rails and your application. A dashboard basically would only interface with your existing models and controller logic since you dont want to end up with a situation where you have two sets of logic for each model. While the blog I linked to is from 2007 you should be able to pull some useful information off of it and get the idea of what you need to do.

If you have any questions feel free to ask.

like image 1
Devin M Avatar answered Oct 18 '22 13:10

Devin M