Recently, I needed to create some static reporting pages for Skribit. From a quick search, I got a lot of results that talk about Rails and static pages, but none did exactly what I needed:
- To be able to generate pages with different paths from one URL
- Pages to persist across Rails deployments
Not seeing any solutions that fit my needs, I set out to come up with my own. Here is what I ended up with.
First, I needed to add a route for generating/displaying the reports:
From here, I could just use the standard caches_page :show declaration, but that would only generate the page I wanted if I used /report/2009/05/25 as the URL. What if I wanted /report to generate the report for the current week? Well, you can do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
The magic is in the after_filter method cache_weekly_report. We basically use the same mechanism Rails page caching uses to save our new report page. Now, calling /report will generate a static report at /report/2009/05/25, or whatever the current day is.
The last thing to do is to make sure that the reports persist through new server deployments. That can easily be done with a symlink in your capistrano script:
1 2 3 4
And that’s it! What do you think? I’d love to know if there are any simpler solutions to this.