• Stars
    star
    222
  • Rank 179,123 (Top 4 %)
  • Language
    Ruby
  • Created over 15 years ago
  • Updated over 15 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Dynamic Reporting Engine for Ruby && Rails

Dynamic Reports¶ ↑

A dynamic reporting engine for Ruby / Rails

Reports¶ ↑

The dynamic reports gem was created to fill a HUGE hole that we felt existed in the 
Ruby community - the ability to QUICKLY create stylized admin reports and charts for 
people to use to view key metrics and data. 

Sample uses include the ability to quickly display sales data if your an eShop, our 
site metrics if you are recording your own site visits, or user feedback if you are storing 
feedback in a model somewhere.

Basically, with DR you can create a stylized table of ANY information found in a model 
(kind of like looking at the grid output from a GUI query analyzer) as well as add Google 
Charts API powered line, pie, bar or column charts of any numeric data.  All this can 
be done by simply creating a report definition and feeding it your data.

While this library is usable in any Ruby application it was made mainly with Rails in mind.
Suppose we have an online store and we wish to add reporting to the admin area quickly and easily.
First we define a report in app/reports/orders_report.rb, something like:

  class OrdersReport < DynamicReports::Report
    title "Orders Report"
    subtitle "All orders recorded in database"
    columns :total, :created_at
  end

Then in our admin/reports controller (this can be any controller) we define an action to deliver the report:

  def orders
    @orders = Order.find(:all, :limit => 25)
    render :text => OrdersReport.on(@orders).to_html, :layout => "application"
  end

This will render an html table containing some basic styling and containing the columns 'total' and 'created_at' from the order objects.
Note that the report Title will be "Orders Report" and it's name will be :orders_report
Report#on expects that it receives an object that responds to #each and
That each object that it iterates over is either a
  * An object
  * A Hash
that responds to a method / has keys for each column defined within the report.

Templating engines may also be specified, currently :erb and :haml are supported (we will soon be adding :csv and :pdf) like so:

    render :text => OrdersReport.on(@orders).to_html(:engine => :haml), :layout => "application"

Note that erb is the default templating engine since it is available by default in Ruby.

Now let us extend our report definition to specify a template to use!

  class OrdersReport < DynamicReports::Report
    title "Orders Report"
    subtitle "All orders recorded in database"
    columns :total, :created_at

    template :my_custom_template
  end

This will look in app/views/reports/ for a template named "my_custom_template.html.erb" by default.
If you specify :engine => :haml then it will look for "my_custom_template.html.haml"

If you happen to have your report templates in a different location you can specify this as follows:

  class OrdersReport < DynamicReports::Report
     title "Orders Report"
    subtitle "All orders recorded in database"
    columns :total, :created_at

    template :my_custom_template
    views "app/views/admin/reports/"
  end

And DynamicReports will look for the specified template in app/views/reports as well as app/views/admin/reports.

It is also worth pointing out that you can have as many dynamic reports in a view as you wish, simply include
each report render where desired within the view.

Charts¶ ↑

Charts can be defined on a report easily. Let's say we wish to chart the total versus the item quantity sold for our Orders Report exmaple:

  class OrdersReport < DynamicReports::Report
    title "Orders Report"
    subtitle "All orders recorded in database"
    columns :total, :created_at

    chart :total_vs_quantity do
      columns :total, :quantity
      label_column "created_at"
    end
  end

This will render a *line* chart by default displaying the columns total and quantity.
Chart types may be specified easily:

      type :bar

Available chart types are:

  * :line (default)
  * :bar
  * :pie

Since DynamicReport's charts utilize the Google Chart API, you can easily extend each chart by passing a hash of chart options as part
of the block.  The options are appended onto the request to the API so they should follow the Google's API commands (http://code.google.com/apis/chart/)

For example, to add min, max and average labels to the example chart, you would do something like this:

  chart :total_vs_quantity, {:chxt => "r", :chxl => "0:|min|average|max"} do
      columns :total, :quantity
      label_column "created_at"
  end
Dynamic Reports supports linking from any column within your table.  To add a link to a column, add the following 
to a report definition:

link :column, url

For example:

class OrdersReport < DynamicReports::Report
  title "Orders Report"
  subtitle "All orders recorded in database"
  columns :total, :created_at

  link :total, '/report/daily_sales'
end

You can also pass parameters to the URL based on values from the underlying model.  To pass a parameter, surround the 
field name with {}.  The parameter does NOT need to be a displayed, column.  For example, you might want to pass 
an external link an ID column but not display this column on the table.  

For example:

class OrdersReport < DynamicReports::Report
  title "Orders Report"
  subtitle "All orders recorded in database"
  columns :total, :created_at

  link :total, '/report/item_sales?id={id}'   # =>  Will substitute ID for the value of ID associated with that record
end

Subreports¶ ↑

Dynamic Reports supports the display of a sub-report within any report.  This is accomplished using the jQuery library 
available at http://www.jquery.com.  

Sub-reports are created using the same definition format that you would use to create a standard report.  The only 
difference is that it is displayed INLINE when an associated link is clicked.  

A sub-report is defined using the same format as a LINK above, but is labeled as:

subreport :column, url

For example, if you wanted to show all sales and allow a user to click on a specific item to see all historic sales 
inline for just that item, you would do the following:

IN CONTROLLER:

def orders
  @orders = Order.find(:all, :limit => 25)
  render :text => OrdersReport.on(@orders).to_html, :layout => "application"
end

def item_sales
  @item_orders = Order.find_by_id(params[:id])
  render :text => ItemSales.on(@orders).to_html, :layout => "application"
end

REPORT DEFINITIONS

class OrdersReport < DynamicReports::Report
  title "Orders Report"
  subtitle "All orders recorded in database"
  columns :total, :created_at

  subreport :total, '/report/item_sales?id={id}'   # =>  Will substitute ID for the value of ID associated with that record
end

class ItemSales < DynamicReports::Report
  columns :item, :price, :created_at
end

Subreports can also be nested.

Rails Usage¶ ↑

The gem includes a stylesheet and javascript based on jQuery.  To add both to your Rails project, 
simply type "drsetup" from the project root.  This will add:

public/stylesheets/dynamic_reports.css  (controls style of dynamic reports)
public/javascripts/dynamic_reports.js   (controls display of subreports)

You can then modify these files as you see fit.

Inside the initializer block in config/environment.rb 

  config.gem "dynamic_reports"

Then define your reports (as exampled above) in app/reports/*_report.rb
If you would like to customize the default report simply create your report templates 
within app/views/reports/*_report.<content-type>.<engine>.

Two Rails features that we are currently working on are:

  * generator
  * render extensions

Optional Dependencies¶ ↑

We are currently examining solutions for csv, pdf and charting.

  * Fastercsv     # csv
  * Prawn         # pdf
  * flying saucer # html => PDF - if jRuby available
  * amcharts      # Charting, note that default is built in google charts.

These will be defined/implemented using DynamicReports plugin API (not implemented yet)
Which allows for user defined plugins of arbitrary types beyond html,csv,pdf,xml

Contact / Feedback¶ ↑

If you have any suggestions on improvement please send us an email.

Authors (alphabetically)¶ ↑

Joshua Lippiner (jlippiner@gmail.com)

Wayne E. Seguin (wayneeseguin@gmail.com, irc: wayneeseguin)

Thanks To¶ ↑

* Daniel Neighman
* Kenneth Kalmer & Nic Young
* Yehuda Katz

For their encouragement, feedback and advise.

Source¶ ↑

http://github.com/wayneeseguin/dynamic_reports

More Repositories

1

redis-installer

Redis Installer
Shell
47
star
2

jquery_templates

jQuery Templating Engine to enable .jqt script file processing.
JavaScript
23
star
3

merblogger

A Merb Blogging &amp; Publishing Platform using Merb, DataMapper, haml and jQuery.
JavaScript
20
star
4

alogr

AlogR is a threadsafe non-blocking asynchronous configurable logger for Ruby.
Ruby
17
star
5

sequel

Sequel ORM
Ruby
14
star
6

sequel-model

Sequel::Model (No longer working on this project)
Ruby
10
star
7

autozest

AutoZest is an autotest addon that: * automated growl installation * generation of .autotest with growl & autozest config * generation of .autozest.yml config file * autozest.sqlite3 database file for pulling random messages based on severity
Ruby
10
star
8

ar_migration_branches

Active Record Migration Branches
Ruby
8
star
9

psql-cm

PostgreSQL Change Management Tool
Ruby
8
star
10

merbtastic

Merb + Webgen CMS system that has dynamic routing, Nginx config and static site generation with haml/sass/erb/... support.
8
star
11

hash_extension

Provides ActiveRecord finder methods that return hashes and makes hashes more object-like.
Ruby
8
star
12

rnginx

Command line utility and library for working with Nginx configuration scripts.
Ruby
8
star
13

ruby-dcap

Ruby DCamp "D-Cap" Website
Ruby
5
star
14

miniruby

Stripped down 1.9.1 to miniruby
C
5
star
15

exdata-project2

Exploratory Data Analysis Course Project 2
R
4
star
16

hello_world

Hello World Rails Application for testing deployments.
4
star
17

db

dbm
3
star
18

dbm

3
star
19

appfirst-boshrelease

AppFirst BOSH Release (ABR)
Shell
3
star
20

bosh-create

Exploration of ways to do bosh-release logic and structure.
Shell
2
star
21

cf-appfirst-buildpack

Cloud Foundry AppFirst Collector Buildpack
Shell
2
star
22

cf-app-logstail

Cloud Foundry Logs Tailer
Go
2
star
23

onramp

OnRamp
Shell
2
star
24

datasciencecoursera

Data Science @ Coursera
2
star
25

sm_mux

SM Tmux Project manager
Shell
1
star
26

pgconf2016-steel-elephant-notes

Notes
Shell
1
star
27

ridemo

RailsInstaller demo
Ruby
1
star
28

turtle

Turtle Rails 3 application, simulating slow startup times. Example: $ SLEEP=15 rails server # 15 second minimum bootup time
Ruby
1
star
29

haproxy-cli

HAProxy Stats/Info CLI & Library
Go
1
star