Class: DynamicReports::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_reports/reports.rb

Overview

DynamicReports::Report

Constant Summary collapse

@@default_engine =
"erb"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, *new_options) ⇒ Report

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.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/dynamic_reports/reports.rb', line 247

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

Instance Attribute Details

#chartsObject

Returns the value of attribute charts.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def charts
  @charts
end

#class_nameObject

Returns the value of attribute class_name.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def class_name
  @class_name
end

#columnsObject

Returns the value of attribute columns.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def columns
  @columns
end

Returns the value of attribute links.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def links
  @links
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def name
  @name
end

#recordsObject

Returns the value of attribute records.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def records
  @records
end

#stylesObject

Returns the value of attribute styles.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def styles
  @styles
end

#sub_titleObject

Returns the value of attribute sub_title.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def sub_title
  @sub_title
end

#templateObject

Returns the value of attribute template.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def template
  @template
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/dynamic_reports/reports.rb', line 11

def title
  @title
end

Class Method Details

.chart(name, *chart_options, &block) ⇒ Object

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


169
170
171
172
# 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) ⇒ Object

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



145
146
147
# 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) ⇒ Object

Return an array of charts defined for the report.



150
151
152
153
154
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
# 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) ⇒ Object

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]


132
133
134
135
136
137
138
139
140
141
142
# 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

Define a link for the report

Pass parameters within {}. Parameters are replaced with the row values from passed records. You do NOT need to include a parameter value as a report column for it to be used in a link. For example, you might want to generate a link with an ID field in it but not display that id in the actual report. Just include id to do this.

Example:

link :visits, ‘/reports/visit/details?date=recorded_at’



193
194
195
# File 'lib/dynamic_reports/reports.rb', line 193

def link(column, url, link_options=nil)
  links({:column => column, :url => url, :link_options => link_options})
end

Return an array of links defined for the report.



175
176
177
178
179
# File 'lib/dynamic_reports/reports.rb', line 175

def links(object=nil)
  options[:links] ||= []
  options[:links] << object if object
  options[:links]
end

.name(value = nil) ⇒ Object

class level name accessor & setter

Set the name of the report, for example:

name "Orders Report"


47
48
49
50
51
52
53
54
# 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

.on(records) ⇒ Object

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



231
232
233
# File 'lib/dynamic_reports/reports.rb', line 231

def on(records)
  new(records, @options)
end

.optionsObject

class level options accessor



37
38
39
# File 'lib/dynamic_reports/reports.rb', line 37

def options
  @options ||= {}
end

.stylesObject



84
85
86
# File 'lib/dynamic_reports/reports.rb', line 84

def styles
  options[:styles] ||= false
end

.sub_title(value = nil) ⇒ Object

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"


80
81
82
# File 'lib/dynamic_reports/reports.rb', line 80

def sub_title(value = nil)
  value ? options[:sub_title] = value : options[:sub_title]
end

.subreport(column, url, link_options = nil) ⇒ Object

Define an inline subreport for the report

Pass parameters within {}. Parameters are replaced with the row values from passed records. You do NOT need to include a parameter value as a report column for it to be used in a link. For example, you might want to generate a subreport with an ID field in it but not display that id in the actual report. Just include id to do this.

Example:

subreport :visits, ‘/reports/visit/details?date=recorded_at’

The subreport should be created using the same report definition style that you use for any other report.



212
213
214
215
216
# File 'lib/dynamic_reports/reports.rb', line 212

def subreport(column, url, link_options=nil)
  link_options ||= {}
  link_options.merge!({:class => 'sub_report_link'}) 
  links({:column => column, :url => url, :link_options => link_options})
end

.template(value = nil) ⇒ Object

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


108
109
110
111
112
113
114
# 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) ⇒ Object

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"


66
67
68
# File 'lib/dynamic_reports/reports.rb', line 66

def title(value = nil)
  value ? options[:title] = value : options[:title]
end

.views(*array) ⇒ Object

Views setter and accessor.



25
26
27
28
29
30
31
32
33
34
# 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

Instance Method Details

#optionsObject

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



19
20
21
# File 'lib/dynamic_reports/reports.rb', line 19

def options
  @options
end

#to_csvObject

Not Implemented Yet



292
293
294
# File 'lib/dynamic_reports/reports.rb', line 292

def to_csv
  # TODO: Write csv hook
end

#to_html(*options) ⇒ Object

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.



281
282
283
284
285
286
287
288
289
# File 'lib/dynamic_reports/reports.rb', line 281

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_pdfObject

Not Implemented Yet



297
298
299
# File 'lib/dynamic_reports/reports.rb', line 297

def to_pdf
  # TODO: Write pdf hook
end

#viewsObject

views accessor, array of view paths.



14
15
16
# File 'lib/dynamic_reports/reports.rb', line 14

def views
  @views
end