lib/vanity/metric/active_record.rb
def model(class_or_scope, options = nil)
      ActiveSupport.on_load(:active_record, :yield=>true) do
        class_or_scope = class_or_scope.constantize if class_or_scope.is_a?(String)
        options = options || {}
        conditions = options.delete(:conditions)

        @ar_scoped = conditions ? class_or_scope.where(conditions) : class_or_scope
        @ar_aggregate = AGGREGATES.find { |key| options.has_key?(key) }
        @ar_column = options.delete(@ar_aggregate)
        fail "Cannot use multiple aggregates in a single metric" if AGGREGATES.find { |key| options.has_key?(key) }

        @ar_timestamp = options.delete(:timestamp) || :created_at
        @ar_timestamp, @ar_timestamp_table = @ar_timestamp.to_s.split('.').reverse
        @ar_timestamp_table ||= @ar_scoped.table_name

        @ar_identity_block = options.delete(:identity)

        fail "Unrecognized options: #{options.keys * ", "}" unless options.empty?

        @ar_scoped.after_create(self)
        extend ActiveRecord
      end
    end
def model(class_or_scope, options = nil)
      ActiveSupport.on_load(:active_record, :yield=>true) do
        class_or_scope = class_or_scope.constantize if class_or_scope.is_a?(String)
        options = options || {}
        conditions = options.delete(:conditions)

        @ar_scoped = conditions ? class_or_scope.where(conditions) : class_or_scope
        @ar_aggregate = AGGREGATES.find { |key| options.has_key?(key) }
        @ar_column = options.delete(@ar_aggregate)
        fail "Cannot use multiple aggregates in a single metric" if AGGREGATES.find { |key| options.has_key?(key) }

        @ar_timestamp = options.delete(:timestamp) || :created_at
        @ar_timestamp, @ar_timestamp_table = @ar_timestamp.to_s.split('.').reverse
        @ar_timestamp_table ||= @ar_scoped.table_name

        @ar_identity_block = options.delete(:identity)

        fail "Unrecognized options: #{options.keys * ", "}" unless options.empty?

        @ar_scoped.after_create(self)
        extend ActiveRecord
      end
    end
def model ( class_or_scope , options = nil ) ActiveSupport . on_load ( :active_record , :yield => true ) do class_or_scope = class_or_scope . constantize if class_or_scope . is_a? ( String ) options = options || { } conditions = options . delete ( :conditions ) @ar_scoped = conditions ? class_or_scope . where ( conditions ) : class_or_scope @ar_aggregate = AGGREGATES . find { | key | options . has_key? ( key ) } @ar_column = options . delete ( @ar_aggregate ) fail "Cannot use multiple aggregates in a single metric" if AGGREGATES . find { | key | options . has_key? ( key ) } @ar_timestamp = options . delete ( :timestamp ) || :created_at @ar_timestamp , @ar_timestamp_table = @ar_timestamp . to_s . split ( '.' ) . reverse @ar_timestamp_table ||= @ar_scoped . table_name @ar_identity_block = options . delete ( :identity ) fail "Unrecognized options: #{options.keys * ", "}" unless options . empty? @ar_scoped . after_create ( self ) extend ActiveRecord end end
Use an ActiveRecord model to get metric data from database table. Also
 forwards +after_create+ callbacks to hooks (updating experiments).

 Supported options:
 :conditions -- Only select records that match this condition
 :average -- Metric value is average of this column
 :minimum -- Metric value is minimum of this column
 :maximum -- Metric value is maximum of this column
 :sum -- Metric value is sum of this column
 :timestamp -- Use this column to filter/group records (defaults to
 +created_at+)

 @example Track sign ups using User model
   metric "Signups" do
     model Account
   end
 @example Track satisfaction using Survey model
   metric "Satisfaction" do
     model Survey, :average=>:rating
   end
 @example Track only high ratings
   metric "High ratings" do
     model Rating, :conditions=>["stars >= 4"]
   end
 @example Track only high ratings (using scope)
   metric "High ratings" do
     model Rating.high
   end

 @since 1.2.0
 @see Vanity::Metric::ActiveRecord
Use an ActiveRecord model to get metric data from database table . Also forwards + after_create + callbacks to hooks ( updating experiments ) .