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
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.
Download vCard





