Module | Ruport::Reportable::SingletonMethods |
In: |
lib/ruport/acts_as_reportable.rb
|
This module contains methods that will be made available as singleton class methods to any ActiveRecord model that calls acts_as_reportable.
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. |
:record_class: | specify the class of the table‘s records. |
The same set of options may 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 model‘s underscored class name, e.g. ‘author.name’ By default, this will not preserve the entire namespace, but you can get the fully qualified namespace by using the :preserve_namespace => true option to report_table. So if the Author model was enclosed in a module called MyModule, you‘d get ‘my_module/author.name’ as the column name.
Creates a Ruport::Data::Table from an ActiveRecord find_by_sql.
Additional options include:
: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")