Module: Ruport::Reportable::InstanceMethods

Defined in:
lib/ruport/acts_as_reportable.rb

Overview

Overview

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

Instance Method Summary collapse

Instance Method Details

#reportable_data(options = {}) ⇒ Object

Grabs all of the object's attributes and the attributes of the associated objects and returns them as an array of record hashes.

Associated object attributes are stored in the record with “association.attribute” keys.

Passing :only as an option will only get those attributes. Passing :except as an option will exclude those attributes. Must pass :include as an option to access associations. Options may be passed to the included associations by providing the :include option as a hash. Passing :methods as an option will include any methods on the object.

Example:

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

abook.reportable_data(:only => ['title'], :include => [:author])

Returns:

[{'title' => 'book title',
  'author.id' => 'author id',
  'author.name' => 'author name' }]

NOTE: title will only be returned if the value exists in the table. If the books table does not have a title column, it will not be returned.

Example:

abook.reportable_data(:only => ['title'],
  :include => { :author => { :only => ['name'] } })

Returns:

[{'title' => 'book title',
  'author.name' => 'author name' }]

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/ruport/acts_as_reportable.rb', line 290

def reportable_data(options = {})
  options = options.merge(self.class.aar_options) unless
    has_report_options?(options)
  
  data_records = [get_attributes_with_options(options)]
  Array(options[:methods]).each do |method|
    if options[:qualify_attribute_names]
      m = "#{options[:qualify_attribute_names]}.#{method}"
    else
      m = "#{method}"
    end
    data_records.first[m] = send(method)
  end
  
  # Reorder columns to match options[:only]
  if Array === options[:only]
    cols = options[:only].map {|c| c.to_s }
    self.class.aar_columns = cols
  end
                      
  self.class.aar_columns |= data_records.first.keys  
  
  data_records =
    add_includes(data_records, options[:include]) if options[:include] 
    
  data_records
end