views:

78

answers:

1

It's getting old rewriting report code for Rails applications. Ya have to create the query, the routes, the action, and the view...(yes, I'm lazy) Is there anything out there that would create a full report in even fewer steps?

Here's what I imagine would be ideal:

You have a Report record that has a name, query code (in Ruby or SQL), and perhaps some report options like so:

Report.create(:name  => "With last name 'smith'",
              :query => "Person.where( :last_name => 'smith' )")

This would store a record, and you'd dynamically get a route:

method  :  report_with_last_name_smith_path
http    :  GET  
url     :  /report_with_last_name_smith
options :   {
              :controller => 'reports', 
              :action => 'with_last_name_smith'
            }

And the report record would retrieve all columns from the query (which happens to be all columns in the people table in this case), and generate a view with the data like so (pretend this is html):

 | First Name | Last Name | Date of Birth | Sex    | 
 | Bob        | Smith     | 03-13-2000    | Male   | 
 | Lisa       | Smith     | 03-23-1980    | Female | 
 | Jack       | Smith     | 03-13-1975    | Male   | 

Anyone know of a plugin that helps achieve at least part of this?

By the way, the Ruport gem will likely be incompatible with Rails 3, and to be honest, it's a little unwieldy.

A: 

Here's something that gets us almost there:

http://github.com/wayneeseguin/dynamic_reports

In Dynamic Reports you create a report class that specifies a few parameters, and add a controller action to specify the query to use for the results.

Here's the example on the site:

# In some class that you create
class OrdersReport < DynamicReports::Report
  title "Orders Report"
  subtitle "All orders recorded in database"
  columns :total, :created_at

  link :total, '/report/item_sales?id={id}'   # =>  Will substitute ID for the value of ID associated with that record
end

# In any controller of your choosing:
def orders
  @orders = Order.find(:all, :limit => 25)
  render :text => OrdersReport.on(@orders).to_html, :layout => "application"
end

The docs don't say anything about routes, so I assume we have to create those ourselves.

It also allows you to use any layout or custom templates you want, and it generates charts using the Google charts API.

btelles
Doh! I guess this is as close as I'm gonna get.
btelles