Quick Start Tutorial

Oleksandr Avoiants edited this page Jul 13, 2016 · 5 revisions
Clone this wiki locally

A quick start tutorial on Ruport

In this example I’ll be building a simple time reporting system

1. Add Gems

Add the following to the Gemfile

# Reports (git version for ruby 1.9.x compatibility)
gem 'ruport', :git => 'https://github.com/ruport/ruport.git', :branch => 'ruby19-compat'
gem 'acts_as_reportable'

Run ‘bundle’

2. Setup environment

Create a directory app/reports to hold your report controllers

Add this to your path in the `config/application.rb` file

config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/reports)

Add to your `config/environments/development.rb` under the configure block

# requires must go inside the Initializer block
require 'ruport' # Ruby Reporting Tool
require 'ruport/acts_as_reportable' # ActiveRecord data collection for Ruport.

3. Create a Ruport controller

Under app/report create a file for your report

`time_report.rb`

Boiler plate.

class TimeReport < Ruport::Controller
  stage :header

  def setup
  end

  class Html < Ruport::Formatter
    renders :html, :for => TimeReport

    def build_header
      output << "<h1>Time Report</h1>"      
    end

  end
end

Test your controller

$ rails c
> TimeReport.render_html
"<h1>Time Report</h1>"      

4. Testing in a Rails

Create a rails controller called ‘reports’

$ rails generate controller reports

Add the following to `config/routes.rb`

# Reports
match '/reports/time' => "reports#time"

Add the following to `reports_controller.rb`

class ReportsController < ApplicationController
  def time
    @report = TimeReport.render_html
  end
end

Now in the `app/views/reports/time.html.haml`

= @report.html_safe

Restart your server Point your browser to `http://localhost:3000/reports/time`

You should get the heading ‘Time Report’ as a heading.

Now we can start to add data.

5. Passing a variable from controller to report controller

Ruport puts variables into a options variable

EG: If you want to get all hours for one user then you could do this

From Rails controller

@report = TimeReport.render_html(:user => current_user)

Within the Ruport::Controller

class Html < Ruport::Formatter
  renders :html, :for => TimeReport

  def build_header
    user = options.user
    output << "<h1>Time Report for #{user.name}</h1>"      
  end
end