acts_as_reportable in 30 seconds or your money back
acts_as_reportable is an ActiveRecord extension that allows you to convert a query on an ActiveRecord model object into a Ruport::Data::Table for easy integration with Ruport's formatting capabilities.
To use AAR, install Acts as Reportable
In your model class, you can call the acts_as_reportable class method to bless your model class with the report_table method. This is where the magic happens. report_table creates a Ruport::Data::Table from an ActiveRecord find.
Here's a little example borrowed from the acts_as_reportable source.
Given these model classes:
class Book < ActiveRecord::Base
acts_as_reportable
belongs_to :author
def author_name
author.name
end
end
class Author < ActiveRecord::Base
acts_as_reportable
has_many :books
end
You can call:
>> puts Book.report_table(:all).as(:csv) id,title,author_id 1,Whys (Poignant) Guide to Ruby,1 2,"Flow My Tears, The Policeman Said",2 3,Farenheit 451,3
As you can see, this gets you a quick csv dump of the books table.
You can tailor the resulting columns of the table with the :include, :only, :except, and :methods options. The same options can be passed to associations by providing a hash for the :include option.
>> puts Book.report_table(:all, :include => [:author]).as(:text) +---------------------------------------------------------------------------------+ | id | title | author_id | author.id | author.name | +---------------------------------------------------------------------------------+ | 1 | Why's (Poignant) Guide to Ruby | 1 | 1 | _why | | 2 | Flow My Tears, The Policeman Said | 2 | 2 | Philip K. Dick | | 3 | Farenheit 451 | 3 | 3 | Ray Bradbury | +---------------------------------------------------------------------------------+
>> puts Book.report_table(:all, :only => ['title'],
:include => {:author => {:only => 'name'}}).as(:text)
+----------------------------------------------------+
| title | author.name |
+----------------------------------------------------+
| Why's (Poignant) Guide to Ruby | _why |
| Flow My Tears, The Policeman Said | Philip K. Dick |
| Farenheit 451 | Ray Bradbury |
+----------------------------------------------------+
>> puts Book.report_table(:all, :except => ['author_id']).as(:text) +----------------------------------------+ | id | title | +----------------------------------------+ | 1 | Why's (Poignant) Guide to Ruby | | 2 | Flow My Tears, The Policeman Said | | 3 | Farenheit 451 | +----------------------------------------+
>> puts Book.report_table(:all, :only => ['title'], :methods => [:author_name]).as(:text) +----------------------------------------------------+ | title | author_name | +----------------------------------------------------+ | Why's (Poignant) Guide to Ruby | _why | | Flow My Tears, The Policeman Said | Philip K. Dick | | Farenheit 451 | Ray Bradbury | +----------------------------------------------------+
The acts_as_reportable class method also takes all of the same options as the report_table method, allowing you to set default options for your reports. Any options passed to the report_table method will disable these default options.
class Book < ActiveRecord::Base acts_as_reportable :except => ['id', 'author_id'] belongs_to :author end
You can also pass any of the ordinary ActiveRecord find options to report_table, so you could add conditions if you like:
>> puts Book.report_table(:all,
:only => ['title','author.name'],
:conditions => "title like '%Poignant%'").as(:text)
report_table returns a Ruport::Data::Table object, so you might want to check out the api docs for Ruport to see what you can do with it.
