reportable.rb - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (HTM) git clone git://jay.scot/reportable
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       reportable.rb (3617B)
       ---
            1 
            2 require 'saulabs/reportable/report'
            3 require 'saulabs/reportable/cumulated_report'
            4 require 'saulabs/reportable/railtie'
            5 
            6 module Saulabs
            7 
            8   module Reportable
            9 
           10     # The adapter connects Reportable and Rails.
           11     #
           12     module RailsAdapter
           13 
           14       # Extends the {Saulabs::Reportable::ClassMethods#reportable} method into +base+.
           15       #
           16       def self.included(base)
           17         base.extend ClassMethods
           18       end
           19 
           20       module ClassMethods
           21 
           22         # Generates a report on a model. That report can then be executed via the new method +<name>_report+ (see documentation of {Saulabs::Reportable::Report#run}).
           23         # 
           24         # @param [String] name
           25         #   the name of the report, also defines the name of the generated report method (+<name>_report+)
           26         # @param [Hash] options
           27         #   the options to generate the reports with
           28         #
           29         # @option options [Symbol] :date_column (created_at)
           30         #   the name of the date column over that the records are aggregated
           31         # @option options [String, Symbol] :value_column (:id)
           32         #   the name of the column that holds the values to aggregate when using a calculation aggregation like +:sum+
           33         # @option options [Symbol] :aggregation (:count)
           34         #   the aggregation to use (one of +:count+, +:sum+, +:minimum+, +:maximum+ or +:average+); when using anything other than +:count+, +:value_column+ must also be specified
           35         # @option options [Symbol] :grouping (:day)
           36         #   the period records are grouped in (+:hour+, +:day+, +:week+, +:month+); <b>Beware that <tt>reportable</tt> treats weeks as starting on monday!</b>
           37         # @option options [Fixnum] :limit (100)
           38         #   the number of reporting periods to get (see +:grouping+)
           39         # @option options [Hash] :conditions ({})
           40         #   conditions like in +ActiveRecord::Base#find+; only records that match these conditions are reported;
           41         # @option options [Hash] :include ({})
           42         #   include like in +ActiveRecord::Base#find+; names associations that should be loaded alongside; the symbols named refer to already defined associations
           43         # @option options [Boolean] :live_data (false)
           44         #   specifies whether data for the current reporting period is to be read; <b>if +:live_data+ is +true+, you will experience a performance hit since the request cannot be satisfied from the cache alone</b>
           45         # @option options [DateTime, Boolean] :end_date (false)
           46         #   when specified, the report will only include data for the +:limit+ reporting periods until this date.
           47         #
           48         # @example Declaring reports on a model
           49         #
           50         #  class User < ActiveRecord::Base
           51         #    reportable :registrations, :aggregation => :count
           52         #    reportable :activations,   :aggregation => :count, :date_column => :activated_at
           53         #    reportable :total_users,   :cumulate => true
           54         #    reportable :rake,          :aggregation => :sum,   :value_column => :profile_visits
           55         #  end
           56         def reportable(name, options = {})
           57           (class << self; self; end).instance_eval do
           58             report_klass = if options.delete(:cumulate)
           59               Saulabs::Reportable::CumulatedReport
           60             else
           61               Saulabs::Reportable::Report
           62             end
           63             define_method("#{name.to_s}_report".to_sym) do |*args|
           64               report = report_klass.new(self, name, options)
           65               raise ArgumentError.new unless args.empty? || (args.length == 1 && args.first.is_a?(Hash))
           66               report.run(args.first || {})
           67             end
           68           end
           69         end
           70 
           71       end
           72 
           73     end
           74 
           75   end
           76 
           77 end