Created
February 23, 2013 17:26
-
-
Save mohitjain1/5020578 to your computer and use it in GitHub Desktop.
act as countable module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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