开发者

Is there a ridiculously easy report plugin for rails?

开发者 https://www.devze.com 2023-01-10 04:43 出处:网络
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 ful

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 wa开发者_开发知识库y, the Ruport gem will likely be incompatible with Rails 3, and to be honest, it's a little unwieldy.


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.


In my projects i use ransack gem, it is amazing, it allow your user make custom queries, and your can customize the attributes available.

Take a look

  • Github
  • Railscast
0

精彩评论

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