Skip to content

Commit

Permalink
added most_backed report
Browse files Browse the repository at this point in the history
  • Loading branch information
devton committed Feb 15, 2012
1 parent 117f67b commit 01043c8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app/admin/dashboards.rb
@@ -1,9 +1,16 @@
#encoding: utf-8
ActiveAdmin::Dashboards.build do

section "Relatorios" do
ul do
li link_to 'Usuários que mais apoiaram', most_backed_report_path
end
end

# Define your dashboard sections here. Each block will be
# rendered on the dashboard in the context of the view. So just
# return the content which you would like to display.

# == Simple Dashboard Section
# Here is an example of a simple dashboard section
#
Expand All @@ -14,7 +21,7 @@
# end
# end
# end

# == Render Partial Section
# The block is rendered within the context of the view, so you can
# easily render a partial rather than build content in ruby.
Expand All @@ -24,7 +31,7 @@
# render 'recent_posts' # => this will render /app/views/admin/dashboard/_recent_posts.html.erb
# end
# end

# == Section Ordering
# The dashboard sections are ordered by a given priority from top left to
# bottom right. The default priority is 10. By giving a section numerically lower
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/reports_controller.rb
Expand Up @@ -15,4 +15,12 @@ def location_by_project
:type => 'text/csv; charset=utf-8; header=present',
:disposition => "attachment; filename=location_report_of_project_#{params[:project_id]}.csv"
end

def users_most_backed
return unless require_admin
@csv = Reports::Users::Backers.most_backed
send_data @csv,
:type => 'text/csv; charset=utf-8; header=present',
:disposition => "attachment; filename=user_most_backed_#{params[:project_id]}.csv"
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -8,6 +8,7 @@
root :to => "projects#index"
match "/reports/financial/:project_id/backers" => "reports#financial_by_project", :as => :backers_financial_report
match "/reports/location/:project_id/backers" => "reports#location_by_project", :as => :backers_location_report
match "/reports/users_most_backed" => "reports#users_most_backed", :as => :most_backed_report

match '/sitemap' => "static#sitemap", :as => :sitemap
# Static Pages
Expand Down
5 changes: 5 additions & 0 deletions lib/reports/users.rb
@@ -0,0 +1,5 @@
module Reports
module Users
autoload :Backers, 'reports/users/backers'
end
end
37 changes: 37 additions & 0 deletions lib/reports/users/backers.rb
@@ -0,0 +1,37 @@
#encoding:utf-8
module Reports
module Users
class Backers
class << self
def most_backed(limit=20)
@users = User.joins(:backs).select(
<<-SQL
users.id,
users.name,
count(backers.id) as count_backs
SQL
).order("count_backs desc").group("users.name, users.id").limit(limit)

@csv = CSV.generate(:col_sep => ',') do |csv_string|

# TODO: Change this later *order and names to use i18n*
# for moment header only in portuguese.
csv_string << [
'ID',
'Nome do apoiador',
'Total de apoios'
]

@users.each do |user|
csv_string << [
user.id,
user.display_name,
user.count_backs
]
end
end
end
end
end
end
end

0 comments on commit 01043c8

Please sign in to comment.