Skip to content

Instantly share code, notes, and snippets.

@mohitjain1
Created February 23, 2013 17:26
Show Gist options
  • Save mohitjain1/5020578 to your computer and use it in GitHub Desktop.
Save mohitjain1/5020578 to your computer and use it in GitHub Desktop.
act as countable module
module ActAsCountable
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def total_records_counts_array_in_past_n_days(range = 30.days)
total_records = []
counts = self.group('Date(created_at)').count
(30.days.ago.to_date..Date.today).each do |day|
total_records.push((total_records.last || 0) + (counts[day] || 0))
end
total_records
end
def daily_records_counts_array_in_past_n_days(range = 30.days)
counts = self.group('Date(created_at)').count
(range.ago.to_date..Date.today).map {|date| counts[date] || 0}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment