Class DynamicReports::Report

  1. lib/dynamic_reports/reports.rb
Parent: Object

Attributes

charts [RW]
class_name [RW]
columns [RW]
name [RW]
records [RW]
styles [RW]
sub_title [RW]
template [RW]
title [RW]

Public class methods

chart (name, *chart_options, &block)

Define a chart for the report

Example:

chart :PV_Visits, {:grxl => 'xxxxx'} do
  title "Pageviews and Visits"
  columns [:pageviews, :visits]
  no_labels false
  label_column "recorded_at"
  width "400"
  height "350"
  type "line"
end
[show source]
# File lib/dynamic_reports/reports.rb, line 169
      def chart(name, *chart_options, &block)
        chart_options = chart_options.shift || {}
        charts(Chart.configure(name, chart_options, &block))
      end
chart_with_name (name)

Return the chart with the specified name, if it exists. nil otherwise.

[show source]
# File lib/dynamic_reports/reports.rb, line 145
      def chart_with_name(name)
        options[:charts].to_a.detect{ |c| c.name == name.to_sym }
      end
charts (object=nil)

Return an array of charts defined for the report.

[show source]
# File lib/dynamic_reports/reports.rb, line 150
      def charts(object=nil)
        options[:charts] ||= [] 
        options[:charts] << object if object
        options[:charts]
      end
class_name (value = nil)
[show source]
# File lib/dynamic_reports/reports.rb, line 88
      def class_name(value = nil)
        if value
          options[:class_name] = value
        elsif options[:class_name].empty?
          options[:class_name] = self.to_s
        else
          options[:class_name]
        end
      end
columns (*array)

Accessor for columns

Pass an array of symbols to define columns on the report

Example:

columns :total, :created_at

Calling the class method with no arguments will return an array with the defined columns.

Example:

OrdersReport.columns

# => [:total, :created_at]
[show source]
# File lib/dynamic_reports/reports.rb, line 132
      def columns(*array)
        unless array.empty?
          if (array.class == Array)
            options[:columns] = array
          else
            raise "Report columns must be specified."
          end
        else
          options[:columns]
        end
      end
name (value = nil)

class level name accessor & setter

Set the name of the report, for example:

name "Orders Report"
[show source]
# File lib/dynamic_reports/reports.rb, line 47
      def name(value = nil)
        if value 
          options[:name] = value
        else 
          # TODO: snake_case_the_name
          options[:name] || self.class.name
        end
      end
new (records, *new_options)

Instantiate the report on a set of records.

Example:

OrdersReport.new(@records)

options is a set of optional overrides for

  • views - Used to override the class defined views.
  • template - Used to override the class defined template.
[show source]
# File lib/dynamic_reports/reports.rb, line 211
    def initialize(records, *new_options)
      new_options  = new_options.shift || {}
      @records = records

      @views = self.class.views
      @views.unshift(new_options.delete(:views)) if new_options[:views]
      @views.flatten!
      @views.uniq!

      @template = self.class.template
      @template = new_options.delete(:template) if new_options[:template]

      @options = self.class.options.merge!(new_options)
      @options.each_pair do |key,value|
        if key == "chart"
          # TODO: erh?
          self.chart(value[:name],{},value)
        else
          instance_variable_set("@#{key}".to_sym, value)
        end
      end
    end
on (records)

Method for instanciating a report instance on a set of given records.

Example:

OrdersReport.on(@records)

Where @records is an array of

  • Objects that respond to methods for all columns defined on the report
  • Hashes that have keys corresponding to all columns defined on the report

This will return an instance of the OrdersReport bound to @records

[show source]
# File lib/dynamic_reports/reports.rb, line 187
      def on(records)
        new(records, @options)
      end
options ()

class level options accessor

[show source]
# File lib/dynamic_reports/reports.rb, line 37
      def options
        @options ||= {}
      end
styles ()
[show source]
# File lib/dynamic_reports/reports.rb, line 84
      def styles
        options[:styles] ||= false
      end
sub_title (value = nil)

Accessor used to set the sub title:

sub_title "All orders for the account"

Or to access the already set sub title:

OrdersReport.sub_title

#=> "All orders for the account"
[show source]
# File lib/dynamic_reports/reports.rb, line 80
      def sub_title(value = nil)
        value ? options[:sub_title] = value : options[:sub_title]
      end
template (value = nil)

Accessor for the template to use for the report.

Example:

template :orders # => renders orders.html.erb (erb is the default engine)

Used without argument returns the template set for the report.

OrdersReport.template # => :orders
[show source]
# File lib/dynamic_reports/reports.rb, line 108
      def template(value = nil)
        if value
          @template = value
          options[:template] = @template
        end
        @template ||= nil
      end
title (value = nil)

Accessor used to set the title:

title "All orders for the account"

Or to access the already set title:

OrdersReport.title

#=> "All orders for the account"
[show source]
# File lib/dynamic_reports/reports.rb, line 66
      def title(value = nil)
        value ? options[:title] = value : options[:title]
      end
views (*array)

Views setter and accessor.

[show source]
# File lib/dynamic_reports/reports.rb, line 25
      def views(*array)
        @views ||= ["#{File::dirname(File::expand_path(__FILE__))}/views/"]
        unless array.empty?
          @views.unshift(array)
          @views.flatten!
          @views.uniq!
        else
          @views
        end
      end

Public instance methods

options ()

options accessor, all report options and configuration is stored in this.

[show source]
# File lib/dynamic_reports/reports.rb, line 19
    def options
      @options
    end
to_csv ()

Not Implemented Yet

[show source]
# File lib/dynamic_reports/reports.rb, line 256
    def to_csv
      # TODO: Write csv hook
    end
to_html (*options)

Convert an instance of a report bound to a records set to an html view

Example

OrdersReport.on(@records).to_html

options
:engine - one of :erb, :haml, :csv, :pdf

Note: CSV & PDF forthcoming.

[show source]
# File lib/dynamic_reports/reports.rb, line 245
    def to_html(*options)
      view    = View.new(self)
      # todo: this is not clean...
      options = (options.shift || {}).merge!(@options || {})
      # todo: if rails is loaded set the default engine: dynamicreports::report.default_engine
      engine  = options.delete(:engine) || @@default_engine
      options[:template] = self.class.template
      view.__send__("#{engine}", options)
    end
to_pdf ()

Not Implemented Yet

[show source]
# File lib/dynamic_reports/reports.rb, line 261
    def to_pdf
      # TODO: Write pdf hook
    end
views ()

views accessor, array of view paths.

[show source]
# File lib/dynamic_reports/reports.rb, line 14
    def views
      @views
    end