Created
January 11, 2011 20:13
-
-
Save hopsoft/775034 to your computer and use it in GitHub Desktop.
Example reports controller for Ryan
This file contains 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 MagicShow | |
def show | |
method_name = params[:id] | |
if respond_to? method_name | |
send(method_name) | |
else | |
render :text => "That operation is not supported!", :layout => false | |
end | |
end | |
end | |
# sample urls for reports as a resource | |
# /reports/daily_user | |
# /reports/monthly_user | |
class ReportsController < ApplicationController | |
include MagicShow | |
private | |
# example of resulting data structure | |
# data = { | |
# "nate" => { | |
# 3 => { | |
# :updates => [<update>, <update>, <update>], | |
# :total => 28 | |
# } | |
# } | |
# } | |
def render_user_report(name, start_date, end_date) | |
name = name.to_s | |
@report_name = name.capitalize | |
@data = {} | |
updates = Update.where(:created_at => (start_date)..end_date.midnight) | |
types = {} | |
UpdateType.all.each {|ut| types[ut.id] = ut.name if ut} | |
updates.each do |update| | |
@data[update.user] ||= {} # data["nate"] = {} # first time "nate" is encountered in updates | |
@data[update.user][update.update_type_id] ||= {} # data["nate"][3-bug] = {} # first time 3-bug is encountered under "nate" | |
@data[update.user][update.update_type_id][:updates] ||= [] # data["nate"][3-bug][:updates] = [] # first time 3-bug is encountered under "nate" | |
@data[update.user][update.update_type_id][:total] ||= 0 # data["nate"][3-bug][:total] = 0 # first time 3-bug is encountered under "nate" | |
@data[update.user][update.update_type_id][:updates] << update # data["nate"][3-bug][:updates] << (fixed 5 template bugs) # every time | |
@data[update.user][update.update_type_id][:total] += update.amount || 0 # data["nate"][3-bug][:total] += 5 # every time | |
end | |
render :action => "user" | |
end | |
def daily_user | |
render_user_report(:daily, Time.now, Time.now.advance(:days => 1)) | |
end | |
def monthly_user | |
render_user_report(:daily, Time.now, Time.now.advance(:days => 1)) | |
end | |
def render_summary_report(start_date, end_date) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment