Right Traq Blog

Rails Reporting with acts_as_reportable

Posted by acts_as_flinn Thu, 01 Feb 2007 02:25:00 GMT

One of the biggest missing features of Rails has been the lack of a reporting tool. Dynamic reporting is a pain in just about any language, but particularly with Rails because of predefined views and railsish renderings. Views are great when working with predefined sets of data derived from ActiveRecord.

Unfortunately it isn’t easy to dynamically query and render resulting data sets.

Ruport has been around for some time now, but until now hasn’t had a sweet rails plugin to convert ActiveRecord data to ruport data collections.

Along comes acts_as_reportable...

With acts_as_reportable you can do really cool stuff like this:


class Part < ActiveRecord::Base
  acts_as_reportable
  belongs_to :account
  belongs_to :category
end

class Category < ActiveRecord::Base
  acts_as_reportable
  has_many :parts
end

class Account < ActiveRecord::Base
  acts_as_reportable
  has_many :parts
  has_many :categories, :through => :parts
end

class ReportsController < ApplicationController
  def index
    parts = Part.report_table(:all, :include => [:account, :category])

    # Remove non content columns
    parts.column_names.each{ |c| parts.remove_column(:name => c) if c =~ /(.id|_id|_count)$/ }

    # Convert table headings
    parts.column_names = parts.column_names.collect{ |cn| cn.gsub(/\./, ' ').titleize }

    render :text => parts.as(:html), :layout => true
  end
end

See the results here

The possibilities here are pretty awesome. To make this work really well, we could very easily create a form to add conditions to a find. Combine it with something like the ez_where plugin and you’ve got some pretty powerful reporting functionality.

Posted in Rails Development | no comments | no trackbacks

Happy New Year!!!

Posted by acts_as_flinn Mon, 01 Jan 2007 16:28:00 GMT

Welcome to 2007! We’re all excited about big things coming up in the year. This month we are rolling out the TRAQInventory.com beta and we’re working on some other super secret projects to add to our inventory management services.

Posted in Misc. | no comments | no trackbacks