Skip to content

Instantly share code, notes, and snippets.

@coreymartella
Created December 17, 2010 19:30
Show Gist options
  • Save coreymartella/745559 to your computer and use it in GitHub Desktop.
Save coreymartella/745559 to your computer and use it in GitHub Desktop.
Builds totals and counts for a scope or conditions
scope :summarized_by_app_and_date, select("app_id, occurred_on, count(*) as count,sum(total) as total").group("app_id, occurred_on").order("app_id, occurred_on")
def self.chart_data(conditions_or_scope)
if conditions_or_scope.is_a?(ActiveRecord::Relation)
totals = conditions_or_scope.summarized_by_app_and_date.all
else
totals = where(conditions_or_scope).summarized_by_app_and_date.all
end
totals.inject({:count => {}, :total => {}}) do |h,t|
h[:count][t.app_id] ||= []
h[:total][t.app_id] ||= []
h[:count][t.app_id] << [t.occurred_on.to_time.to_i*1000,t.count]
h[:total][t.app_id] << [t.occurred_on.to_time.to_i*1000,t.total]
h
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment