Skip to content

Instantly share code, notes, and snippets.

@svs
Created September 18, 2012 10:32
Show Gist options
  • Save svs/3742501 to your computer and use it in GitHub Desktop.
Save svs/3742501 to your computer and use it in GitHub Desktop.
class UserStatistics
# This class is responsible for aggregating User data into meaningful reports
attr_accessor :user
#
#
# ... snip ...
#
#
# Public: Returns an aggregation of monthly spends by category
# options is a Hash with the following keys
# from -> the start date of the aggregation
# to -> the end date of the aggregation
# categories -> a list of categories to filter by. if ommitted, gets all categories
# return a Hash like {2012 => {1 => {:amount => 200, :count => 2}....}....}
def monthly_spending_by_category(options)
by_year_and_month(
@user.receipts.all(:date => from..to).aggregate(:date, :amount.sum, :category)
)
end
private
def by_year_and_month(array)
array.group_by{|x| [x[0].year, x[0].month]}.map{|y| some.complicated{|l|ist comprehension }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment