Reporting in Rails : Report generation : Downloadable in any Format | PDF | CSV | XLS

Working on report generation is a time consuming task yet very necessary for any software projects. End of the day every business owner needs to see where their business stands or see ongoing activity in the system. Reports need list of records and Graphs/Charts to show the big-picture at a glance.

 With Rails Admin

If you are using RailsAdmin for managing you web-application from backend, you get reports for your data out of the box. In addition you can edit/update in no time with ease. You will not have charts and graph at that instant though.

How can I get Charts?

Yes there are some Rails-Admin plugins to help you out. My favorite is RailsAdmin Charts.

In your Gemfile, add the following dependency:

gem 'rails_admin_charts'

Run:

$ bundle install

In your RailsAdmin initializer (config/initializers/rails_admin.rb), enable the action by adding:

config.actions do
  all # NB: comment out this line for RailsAdmin < 0.6.0
  charts
end

For any model where you wish to display a chart add the following just under the class declaration:

include RailsAdminCharts

This will add a few methods in your model for data gathering and setting chart attributes like delta_records_since, total_records_since, graph_data, xaxis, label_rotation, etc.

The default chart type would be ‘Line chart‘ with multiple threads.

  def self.graph_data(since = 30.days.ago)
    [
      {
          name: 'Admin Users',
          pointInterval: point_interval = 1.day * 1000,
          pointStart: start_point = since.to_i * 1000,
          data: self.where(type: 'Admin').delta_records_since(since)
      },
      {
          name: 'Standard Users',
          pointInterval: point_interval,
          pointStart: start_point,
          data: self.where(type: nil).delta_records_since(since)
      }
    ]
  end

With Query report

Query report is an amazing tool for Rails developers to use for report/chart generation. Its easy to customize and gives you most of the features you require out of the box.

It does the following:

  • Generate paginated HTML view with filters, defined columns with sorting
  • Generate PDF, CSV, JSON
  • Provide feature to define re usable custom filter

Getting started

Query report is tested with Rails 3. You can add it to your Gemfile with:

gem "query_report", "~> 1.0.4"

Run the bundle command to install it.

Here is a sample controller which uses query report. And that is all you need, query report will generate all the view for you.

require 'query_report/helper'  # need to require the helper

class InvoicesController < ApplicationController
  include QueryReport::Helper  # need to include it

  def index
    @invoices = Invoice.scoped

    reporter(@invoices) do
      filter :title, type: :text
      filter :created_at, type: :date, default: [5.months.ago.to_date.to_s(:db),
                                                 1.months.from_now.to_date.to_s(:db)]
      filter :paid, type: :boolean, default: false

      column :title do |invoice|
        link_to invoice.title, invoice
      end

      column :total_paid
      column :total_charged
      column :paid
    end
  end
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s