Using ActsAsReportable in Standalone Projects

ActsAsReportable integrates Ruport with ActiveRecord, so that you can use it to get the data for your report. It is of course useful within Rails, but what if you want to use it in a standalone project? This recipe will show you how.

We'll do this in the context of a rope generated project, but the same approach can be taken whether or not you've used rope.

If you haven't tried rope before, you might want to look at the RuportUtil page for further instructions.

Create Your Project

First, you need to create your project's structure. Using rope, do:

rope tattle_aar

Define Your Models

Next, define your model classes and make sure they call the acts_as_reportable method. To do this manually, create a data/models directory to hold the model definitions. The database table for the project is called "reports", so you'll need to define a Report model. File report.rb contains the following model definition.

class Report < ActiveRecord::Base
  acts_as_reportable
end

Also added a file called models.rb to the data directory. This file simply does a require for each of the model files. We'll use it to load all of our models. In this case, it contains:

require "data/models/report"

If you're using Rope, there's a script in ./util/build that will do all of that for you:

./util/build model Report

Edit environment.rb File

Rope creates a file config/environment.rb where we'll load ActsAsReportable and set up our connection with ActiveRecord. Add the following to the bottom of the file (or for rope projects, just uncomment it), and update the connection parameters:

require "active_record"
require "ruport/acts_as_reportable"

ActiveRecord::Base.establish_connection (
    :adapter  => 'mysql',
    :host     => 'localhost',
    :username => 'name',
    :password => 'password',
    :database => 'tattle')

Use it in Your Reports

Now you can use the report_table method to get the data for your reports. See the ActsAsReportable tutorial for more information, but here is an example of collecting data for the report in our project.

table = Report.report_table(:all,
  :only       => %w[host_os rubygems_version ruby_version user_key],
  :conditions => "user_key is not null and user_key <> ''",
  :group      => "host_os, rubygems_version, ruby_version, user_key")