Class: ActiveReport::Base

Inherits:
Object
  • Object
show all
Includes:
Validations, ActiveSupport::Callbacks, Enumerable
Defined in:
lib/active_report/base.rb

Overview

Active Report objects define how to build a particular report and all required information. Unlike Active Record objects, Active Reports are not saved to nor retrieved from a database, but rather aggregate data from existing Active Record objects (or any other means required).

Creation

Active Reports offer many helper methods to assist in creation. For instance, you can specify any number of attributes with the define_attributes method. These attributes will be available to the rest of the class and will be populated with any values that were passed to the new method.

class JobReport < ActiveReport::Base
  define_attribute :jobNumber

  validates_presence_of :jobNumber

  def build_report
    entries = Job.find_all_by_jobNumber(self.jobNumber)
  end
end

Alternatively, you could use an ActiveReport::HashEntry or simple Hash.

class TestReport < ActiveReport::Base
  def build_report
    (1..5).each { |i| entries.add(ActiveReport::HashEntry.new( :i => i, :name => 'Topher' )) }
  end
end

class TestReport < ActiveReport::Base
  def build_report
    (1..5).each { |i| entries.add({ :i => i, :name => 'Topher' }) }
  end
end

Reporting

Generating reports is incredibly easy. The following example shows basic usage assuming that Job is a class extending ActiveRecord::Base.

Job.create( :id => 1, :jobNumber => 123456, :company => "Apple Inc." )
Job.create( :id => 2, :jobNumber => 123456, :company => "37signals" )

params = {}
params[:jobNumber] = 123456

report = JobReport.new(params)

if report.generate
  report.entries.each { |e| puts "#{e.id}: Job ##{e.jobNumber} for #{e.company}" }
end

# => 1: Job #123456 for Apple Inc.
     2: Job #123456 for 37signals

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

included

Constructor Details

#initialize(params = {}) ⇒ Base

Sets up all parameters including mulit-parameters-attributes. Sets the id of the report to be the current Time as an integer of the strftime format “%Y%m%d%H%M%S”.



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/active_report/base.rb', line 152

def initialize(params = {})
  @params = setup_parameters(params)
  
  run_callbacks(:before_initialize)
  
  @id = Time.now.strftime("%Y%m%d%H%M%S").to_i
  
  @errors = ActiveReport::Errors.new
  @entries = []
  
  run_callbacks(:after_initialize)
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



147
148
149
# File 'lib/active_report/base.rb', line 147

def entries
  @entries
end

#errorsObject

Returns the value of attribute errors.



147
148
149
# File 'lib/active_report/base.rb', line 147

def errors
  @errors
end

#idObject

Returns the value of attribute id.



147
148
149
# File 'lib/active_report/base.rb', line 147

def id
  @id
end

#paramsObject

Returns the value of attribute params.



147
148
149
# File 'lib/active_report/base.rb', line 147

def params
  @params
end

Instance Method Details

#generate(perform_validation = true) ⇒ Object

Generates the report by calling build_report and runs validation if requested. Calls before_build_report at the beginning and after_build_report at the end and returns the report against which this method was run.



242
243
244
245
246
247
248
249
250
# File 'lib/active_report/base.rb', line 242

def generate(perform_validation = true)
  return false if perform_validation and not self.valid?
  
  run_callbacks(:before_build_report)
  send(:build_report) if self.respond_to? :build_report
  run_callbacks(:after_build_report)
  
  self
end

#new_record?Boolean

Defined to always return true since no data actually gets saved to the database. This is necessary for the form_for methods in ActionView::Helpers::FormHelper to work properly.

Returns:

  • (Boolean)


268
269
270
# File 'lib/active_report/base.rb', line 268

def new_record?
  true
end

#to_csvObject

Override this method to allow for exporting to a csv file.

Example:

def to_csv
  FasterCSV.generate do |csv|
    csv << [ 'Name', 'Date' ]

    @entries.each do |e|
      csv << [ e.name, e.created_at ]
    end
  end
end


285
286
# File 'lib/active_report/base.rb', line 285

def to_csv
end

#valid?Boolean

Runs before_validate, validate and after_validate and returns true if there were no errors, false otherwise.

Returns:

  • (Boolean)


254
255
256
257
258
259
260
261
262
263
# File 'lib/active_report/base.rb', line 254

def valid?
  run_callbacks(:before_validate)
  
  @errors = ActiveReport::Errors.new
  
  run_callbacks(:validate)
  run_callbacks(:after_validate)
  
  @errors.empty?
end