Module: Ruport::Reportable::SingletonMethods

Defined in:
lib/ruport/acts_as_reportable.rb

Overview

Overview

This module contains methods that will be made available as singleton class methods to any ActiveRecord model that calls acts_as_reportable.

Instance Method Summary collapse

Instance Method Details

#report_table(number = :all, options = {}) ⇒ Object

Creates a Ruport::Data::Table from an ActiveRecord find. Takes parameters just like a regular find.

Additional options include:

:only

An attribute name or array of attribute names to include in the results, other attributes will be excuded.

:except

An attribute name or array of attribute names to exclude from the results.

:methods

A method name or array of method names whose result(s) will be included in the table.

:include

An associated model or array of associated models to include in the results.

:filters

A proc or array of procs that set up conditions to filter the data being added to the table.

:transforms

A proc or array of procs that perform transformations on the data being added to the table.

:record_class

Specify the class of the table's records.

:eager_loading

Set to false if you don't want to eager load included associations.

The :only, :except, :methods, and :include options may also be passed to the :include option in order to specify the output for any associated models. In this case, the :include option must be a hash, where the keys are the names of the associations and the values are hashes of options.

Any options passed to report_table will disable the options set by the acts_as_reportable class method.

Example:

class Book < ActiveRecord::Base
  belongs_to :author
  acts_as_reportable
end

Book.report_table(:all, :only => ['title'],
  :include => { :author => { :only => 'name' } }).as(:html)

Returns:

an html version of the table with two columns, title from the book, and name from the associated author.

Example:

Book.report_table(:all, :include => :author).as(:html)

Returns:

an html version of the table with all columns from books and authors.

Note: column names for attributes of included models will be qualified with the name of the association.


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ruport/acts_as_reportable.rb', line 152

def report_table(number = :all, options = {})
  only = options.delete(:only)
  except = options.delete(:except)
  methods = options.delete(:methods)
  includes = options.delete(:include)
  filters = options.delete(:filters) 
  transforms = options.delete(:transforms)
  record_class = options.delete(:record_class) || Ruport::Data::Record
  self.aar_columns = []

  unless options.delete(:eager_loading) == false
    options[:include] = get_include_for_find(includes)
  end
  
  data = [find(number, options)].flatten
  data = data.map {|r| r.reportable_data(:include => includes,
                         :only => only,
                         :except => except,
                         :methods => methods) }.flatten   

  table = Ruport::Data::Table.new(:data => data,
                                  :column_names => aar_columns,
                                  :record_class => record_class,
                                  :filters => filters,
                                  :transforms => transforms )
end

#report_table_by_sql(sql, options = {}) ⇒ Object

Creates a Ruport::Data::Table from an ActiveRecord find_by_sql.

Additional options include:

:filters

A proc or array of procs that set up conditions to filter the data being added to the table.

:transforms

A proc or array of procs that perform transformations on the data being added to the table.

:record_class

Specify the class of the table's records.

Example:

class Book < ActiveRecord::Base
  belongs_to :author
  acts_as_reportable
end

Book.report_table_by_sql("SELECT * FROM books")

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/ruport/acts_as_reportable.rb', line 201

def report_table_by_sql(sql, options = {})
  record_class = options.delete(:record_class) || Ruport::Data::Record
  filters = options.delete(:filters) 
  transforms = options.delete(:transforms)
  self.aar_columns = []

  data = find_by_sql(sql)
  data = data.map {|r| r.reportable_data }.flatten

  table = Ruport::Data::Table.new(:data => data,
                                  :column_names => aar_columns,
                                  :record_class => record_class,
                                  :filters => filters,
                                  :transforms => transforms)
end