Module: Vanity::Metric::ActiveRecord

Defined in:
lib/vanity/metric/active_record.rb

Overview

Calling model method on a metric extends it with these modules, redefining the values and track! methods.

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) after_create(record)

AR model after_create callback notifies all the hooks.

Since:

  • 1.3.0



78
79
80
81
82
# File 'lib/vanity/metric/active_record.rb', line 78

def after_create(record)
  return unless @playground.collecting?
  count = @ar_column ? (record.send(@ar_column) || 0) : 1
  call_hooks record.send(@ar_timestamp), nil, [count] if count > 0 && @ar_scoped.exists?(record)
end

- (Object) last_update_at

Since:

  • 1.3.0



72
73
74
75
# File 'lib/vanity/metric/active_record.rb', line 72

def last_update_at
  record = @ar_scoped.find(:first, :order=>"#@ar_timestamp DESC", :limit=>1, :select=>@ar_timestamp)
  record && record.send(@ar_timestamp)
end

- (Object) track!(args = nil)

This track! method stores nothing, but calls the hooks.

Since:

  • 1.3.0



67
68
69
70
# File 'lib/vanity/metric/active_record.rb', line 67

def track!(args = nil)
  return unless @playground.collecting?
  call_hooks *track_args(args)
end

- (Object) values(sdate, edate)

This values method queries the database.

Since:

  • 1.3.0



59
60
61
62
63
64
# File 'lib/vanity/metric/active_record.rb', line 59

def values(sdate, edate)
  query = { :conditions=> { @ar_timestamp_table => { @ar_timestamp => (sdate.to_time...(edate + 1).to_time) } },
            :group=>"date(#{@ar_scoped.quoted_table_name}.#{@ar_scoped.connection.quote_column_name @ar_timestamp})" }
  grouped = @ar_column ? @ar_scoped.send(@ar_aggregate, @ar_column, query) : @ar_scoped.count(query)
  (sdate..edate).inject([]) { |ordered, date| ordered << (grouped[date.to_s] || 0) }
end