Up until now the MVC separation hasn’t given me any big problems, but I have to admit the “right” ruby/rails/dry/mvc way to tackle color-coded display of tabular data has eluded me.
My app has a page that shows 7 columns (days of week) with 20 rows of data (20 different products) that ranges from 0-100 in each cell. Each value is an average of between 50-500 rows in the database.
So the table is displaying 7×20=140 separate ‘average’ calculations representing up to 500 rows of data each.
Because of the amount of data being shown to the user, we need to color code the cells based upon the value: <25 = red, 25..75 = yellow, >75 = green
our method that does the calculation is product.get_daily_average(date)
We’re using HAML, btw.
The problem I have is figuring out where to put the code that specifies the color to apply to each cell…
…should there be if/then code to set the cell style (color) in the View? Is this a logical place for a Helper? Or should the model method that fetches the data “return” the html snippet with the color-code + data (instead of returning the value and letting the view handle the color)?
This is normally the type of thing that you would put in a helper method.
In your view you’d do something like this (sorry, don’t know haml, this example is with erb)
And then in a helper