Advice on building a 'reports' feature
My Ruby on Rails enterprise app has a whole range of tables ranging such as customers, products, sales, warehouses, prices etc etc.
I'd like to build a whole range of different reports that users can view that slice and dice the data certain ways. I am imagining there would be a 'search' view where you select your search parameters and then a show page that returns the result.
Is this something you would setup a MVC without a database table for? Also because I could have anywhere up to a hundred of these different reports, would you try to nest the files in your rails app architecture to keep them separated from your core MVC files or would you just not give 2 hoots and let it mix in with the rest and not be OCD about it?
My other alternative is to run a single MVC with conditionals pointing to different views in the controller based on the report selected in the index.
Sometimes I feel deciding these things is the hardest bit...
This is definitely an important thing to decide upon. If you do this poorly, you could end up with a really complex set of reports that are a real pain to do maintenance on.
It sounds like you might be able to structure these reports to hit search and then your reports could simply cache a copy of what the query looks like and maybe you can build configurable views for them that you could simply configure in JSON or YAML somewhere.
Do you have an example of a few reports you might want? I'm trying to get an idea of how they might be alike or different and then extrapolate from there on ideas to whittle it down.
A simple report would be sales where the margin is less than 0. This simply hits one table and I can currently do this using the advanced search feature of Ransack (I have the add filter buttons that Ryan Bates implemented in his Railscast on Ransack).
This is well and good to use the search filters on the existing sales index and show but as soon as it becomes more complex I would like a link that the user goes to and might have different fields etc in the index. I might also like to through a graph on the page.
Another report requires a search form to be filled in to collect certain customer and product groups and then creates a scatterplot of the different customers prices for those products.
In the past I have had this done with a separate controller, view and model for each report but just get concerned as I could have any number of these different reports etc.
Not sure if this helps but this blog post may be a way to go:
I've never implemented something like this myself...but it seems like what you're describing is some kind of dashboard, with data pulled from a lot of different models.
Since you're going to need to do different views (it seems) for the forms on each plot, you may need to create a generic controller and view that can dynamically render the form based upon which report is selected.
If you choose "scatter_plot" in the form, then the next page could render a
scatter_plot.html.erb view. And you could also use this param to call a report class.
After submitting, probably having some plain ruby classes that do the logic of taking the data and producing the graph. Setting up a common API on a set of "Report" ruby classes could help a lot here. They could all have an
initialize method that accepts the data from the form and parses it as necessary. You can add a
render method to each class and then use that for rendering out to the view.
If you have a variable with the name of the graph, you can do
graph_name.constantize to grab the Ruby constant for the class. It would help you take something like
"scatter_plot" and convert it to
ScatterPlot and your code can create a new instance of that class dynamically, pass in the data, and render it out. Obviously, since your reports are all going to be somewhat different, you'll need to create a good amount of these classes, but you can start extracting their shared features to a
Report class they could inherit from.
I'm actually in the same boat right now. I'm working on creating some complex analytics and reports for an internal app. My original version of reporting is a very complex and ugly Class method. But I think using abstraction would really be helpful. I'm also looking into Ransack as well to help leverage results instead of relying on old-school methods of querying data.
I'd love to see what you come up with. Perhaps we can help each other since we're sort of in the same space.
And after reading the OP's post again, I really think you need to abstract as much as possible and separate concerns and responsibilities. Otherwise this could quickly turn into a "God Object" scenario. I'm dealing with Zeus on my side. lol
@chris, please do a tutorial on this topic. Can you reccommend and good materials to review in the meantime?
@chris, Hi!, has anyone got any pointers on this as Im also having trouble finding the best solution for Rails 5.1 app reporting financial data?