开发者

Rails controller design pattern for dashboard

开发者 https://www.devze.com 2023-04-06 20:52 出处:网络
I would like to have a开发者_JAVA技巧 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 d

I would like to have a开发者_JAVA技巧 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.


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.


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.


Should I create a dashboard_controller with just index action?

I prefer to put pages that do not perform CRUD upon a model (such as dashboards) in the application_controller. No need to create a dashboard_controller.


Would that be a use case for the Presenter pattern?

I am about to implement a ddashboard and that's the best design I think that will lead to a maintainable code base.


My solution is to run

rails g controller Dashboards

then manually create a app/models/dashboard.rb model which contents can be as little as

class Dashboard end

Of course the model you can put all the logic you need fishing data from other models.

Back to the controller, you create just the index action. Lastly, you manually create the app/views/dashboards/index.html.erb file with whatever content you need. Add the route and you're good to go. If you pay attention to details you can change the url from /dashboards to /dashboard. That's it, i think it's very Rails-y.

0

精彩评论

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

关注公众号