Skip to content

Commit

Permalink
adds download csv button for non-js admin reports
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDancingClown committed Jul 31, 2023
1 parent dbc7cb9 commit 968a17a
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 39 deletions.
54 changes: 33 additions & 21 deletions app/controllers/admin/dashboard_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,56 @@ class Admin::DashboardReportsController < Admin::BaseController
# that represents the type of the application

def all_applications
authorize :dashboard, :reports?

@report = Reports::Dashboard::ApplicationsReport.new(kind: params[:kind])
render partial: "admin/dashboard/totals_#{params[:kind]}/table_body"
render_or_download_report nil, params[:kind], params[:format]
end

def international_trade
authorize :dashboard, :reports?

@report = Reports::Dashboard::ApplicationsReport.new(kind: params[:kind], award_type: :trade)
render partial: "admin/dashboard/totals_#{params[:kind]}/table_body"
render_or_download_report :trade, params[:kind], params[:format]
end

def innovation
authorize :dashboard, :reports?

@report = Reports::Dashboard::ApplicationsReport.new(kind: params[:kind], award_type: :innovation)
render partial: "admin/dashboard/totals_#{params[:kind]}/table_body"
render_or_download_report :innovation, params[:kind], params[:format]
end

def social_mobility
authorize :dashboard, :reports?

@report = Reports::Dashboard::ApplicationsReport.new(kind: params[:kind], award_type: :mobility)
render partial: "admin/dashboard/totals_#{params[:kind]}/table_body"
render_or_download_report :mobility, params[:kind], params[:format]
end

def sustainable_development
authorize :dashboard, :reports?

@report = Reports::Dashboard::ApplicationsReport.new(kind: params[:kind], award_type: :development)
render partial: "admin/dashboard/totals_#{params[:kind]}/table_body"
render_or_download_report :development, params[:kind], params[:format]
end

def account_registrations
authorize :dashboard, :reports?

@report = Reports::Dashboard::UsersReport.new(kind: params[:kind])
render partial: "admin/dashboard/totals_#{params[:kind]}/users_table_body"

respond_to do |format|
format.csv do
send_data @report.as_csv, filename: @report.csv_filename
end

format.html do
render partial: "admin/dashboard/totals_#{params[:kind]}/users_table_body"
end
end
end

private

def render_or_download_report award_type, kind, format
authorize :dashboard, :reports?

@report = Reports::Dashboard::ApplicationsReport.new(kind: kind, award_type: award_type)

respond_to do |format|
format.csv do
send_data @report.as_csv, filename: @report.csv_filename
end

format.html do
render partial: "admin/dashboard/totals_#{kind}/table_body"
end
end
end
end
29 changes: 29 additions & 0 deletions app/models/reports/dashboard/applications_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,33 @@ def generate_content(form_answers, date)
["&nbsp;".html_safe, "&nbsp;".html_safe]
end
end

def as_csv
CSV.generate do |csv|
headers = ["Year"]
case kind
when "by_month"
columns = ["By end of April", "By end of May", "By end of June", "By end of July", "By end of August", "Totals on deadline"]
when "by_week"
columns = ["6 weeks before deadline", "5 weeks before deadline", "4 weeks before deadline", "3 weeks before deadline", "2 weeks before deadline", "1 week before deadline", "Totals on deadline"]
when "by_day"
columns = ["6 days before deadline", "5 days before deadline", "4 days before deadline", "3 days before deadline", "2 days before deadline", "1 day before deadline", "Totals on deadline"]
end
csv << (headers + columns.map{|c| [c] << nil}).flatten
subheaders =[""]
columns.each{subheaders << ["In progress", "Submitted"]}
csv << subheaders.flatten

stats.each do |row|
content = []
content << row.label
row.content.each{|c| content << (c == "&nbsp;" ? nil : c)}
csv << content
end
end
end

def csv_filename
"#{award_type || "all"}_applications_report_#{kind}.csv"
end
end
26 changes: 26 additions & 0 deletions app/models/reports/dashboard/users_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,30 @@ def generate_content(users, date)
"&nbsp;".html_safe
end
end

def as_csv
CSV.generate do |csv|
headers = ["Year"]
case kind
when "by_month"
columns = ["By end of April", "By end of May", "By end of June", "By end of July", "By end of August", "Totals on deadline"]
when "by_week"
columns = ["6 weeks before deadline", "5 weeks before deadline", "4 weeks before deadline", "3 weeks before deadline", "2 weeks before deadline", "1 week before deadline", "Totals on deadline"]
when "by_day"
columns = ["6 days before deadline", "5 days before deadline", "4 days before deadline", "3 days before deadline", "2 days before deadline", "1 day before deadline", "Totals on deadline"]
end
csv << (headers + columns).flatten

stats.each do |row|
content = []
content << row.label
row.content.each{|c| content << (c == "&nbsp;" ? nil : c)}
csv << content
end
end
end

def csv_filename
"account_registrations_#{kind}.csv"
end
end
3 changes: 2 additions & 1 deletion app/views/admin/dashboard/_applications_report.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.dashboard-table__wrapper.dashboard-report
button.btn.btn-primary.btn--load.update-report href=url aria-label="Load #{title} data"
button.btn.btn-primary.btn--load.update-report.if-no-js-hide href=url aria-label="Load #{title} data"
| Load data
= link_to "Download data", url, class: "btn btn-primary btn--load update-report if-js-hide", aria: { label: "Download #{title} data" }
button.btn.btn-primary.btn--loading.updating-data.hidden aria-label="Updating #{title} data"
| Updating data

Expand Down
20 changes: 15 additions & 5 deletions app/views/admin/dashboard/totals_by_day.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@
p.dashboard__subheading
| Running totals compared to previous years

.dashboard__section
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: all_applications_admin_dashboard_reports_path(kind: "by_day"), title: "All applications", kind: "by_day"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: all_applications_admin_dashboard_reports_path(kind: "by_day", format: :csv), title: "All applications", kind: "by_day"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: international_trade_admin_dashboard_reports_path(kind: "by_day"), title: "International trade", kind: "by_day"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: international_trade_admin_dashboard_reports_path(kind: "by_day", format: :csv), title: "International trade", kind: "by_day"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: innovation_admin_dashboard_reports_path(kind: "by_day"), title: "Innovation", kind: "by_day"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: innovation_admin_dashboard_reports_path(kind: "by_day", format: :csv), title: "Innovation", kind: "by_day"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: social_mobility_admin_dashboard_reports_path(kind: "by_day"), title: "Social mobility", kind: "by_day"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: social_mobility_admin_dashboard_reports_path(kind: "by_day", format: :csv), title: "Social mobility", kind: "by_day"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: sustainable_development_admin_dashboard_reports_path(kind: "by_day"), title: "Sustainable development", kind: "by_day"
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: sustainable_development_admin_dashboard_reports_path(kind: "by_day", format: :csv), title: "Sustainable development", kind: "by_day"
.dashboard__section
= render "admin/dashboard/totals_by_day/registrations"
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.dashboard-table__wrapper.dashboard-report
button.btn.btn-primary.btn--load.update-report href=account_registrations_admin_dashboard_reports_path(kind: "by_day") aria-label="Load data for new account registrations table"
button.btn.btn-primary.btn--load.update-report.if-no-js-hide href=account_registrations_admin_dashboard_reports_path(kind: "by_day") aria-label="Load data for new account registrations table"
| Load data
= link_to "Download data", account_registrations_admin_dashboard_reports_path(kind: "by_day", format: :csv),
class: "btn btn-primary btn--load update-report if-js-hide", aria: { label: "Download registration totals by day" }
button.btn.btn-primary.btn--loading.updating-data.hidden
| Updating data

Expand Down
20 changes: 15 additions & 5 deletions app/views/admin/dashboard/totals_by_month.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@
p.dashboard__subheading
| Running totals compared to previous years

.dashboard__section
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: all_applications_admin_dashboard_reports_path(kind: "by_month"), title: "All applications", kind: "by_month"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: all_applications_admin_dashboard_reports_path(kind: "by_month", format: :csv), title: "All applications", kind: "by_month"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: international_trade_admin_dashboard_reports_path(kind: "by_month"), title: "International trade", kind: "by_month"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: international_trade_admin_dashboard_reports_path(kind: "by_month", format: :csv), title: "International trade", kind: "by_month"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: innovation_admin_dashboard_reports_path(kind: "by_month"), title: "Innovation", kind: "by_month"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: innovation_admin_dashboard_reports_path(kind: "by_month", format: :csv), title: "Innovation", kind: "by_month"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: social_mobility_admin_dashboard_reports_path(kind: "by_month"), title: "Social mobility", kind: "by_month"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: social_mobility_admin_dashboard_reports_path(kind: "by_month", format: :csv), title: "Social mobility", kind: "by_month"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: sustainable_development_admin_dashboard_reports_path(kind: "by_month"), title: "Sustainable development", kind: "by_month"
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: sustainable_development_admin_dashboard_reports_path(kind: "by_month", format: :csv), title: "Sustainable development", kind: "by_month"
.dashboard__section
= render "admin/dashboard/totals_by_month/registrations"
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.dashboard-table__wrapper.dashboard-report
button.btn.btn-primary.btn--load.update-report href=account_registrations_admin_dashboard_reports_path(kind: "by_month") aria-label="Load data for new account registrations table"
| Load data
= link_to "Download data", account_registrations_admin_dashboard_reports_path(kind: "by_month", format: :csv),
class: "btn btn-primary btn--load update-report if-js-hide", aria: { label: "Download registration totals by month" }
button.btn.btn-primary.btn--loading.updating-data.hidden
| Updating data

Expand Down
20 changes: 15 additions & 5 deletions app/views/admin/dashboard/totals_by_week.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@
p.dashboard__subheading
| Running totals compared to previous years

.dashboard__section
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: all_applications_admin_dashboard_reports_path(kind: "by_week"), title: "All applications", kind: "by_week"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: all_applications_admin_dashboard_reports_path(kind: "by_week", format: :csv), title: "All applications", kind: "by_week"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: international_trade_admin_dashboard_reports_path(kind: "by_week"), title: "International trade", kind: "by_week"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: international_trade_admin_dashboard_reports_path(kind: "by_week", format: :csv), title: "International trade", kind: "by_week"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: innovation_admin_dashboard_reports_path(kind: "by_week"), title: "Innovation", kind: "by_week"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: innovation_admin_dashboard_reports_path(kind: "by_week", format: :csv), title: "Innovation", kind: "by_week"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: social_mobility_admin_dashboard_reports_path(kind: "by_week"), title: "Social mobility", kind: "by_week"
.dashboard__section
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: social_mobility_admin_dashboard_reports_path(kind: "by_week", format: :csv), title: "Social mobility", kind: "by_week"
.dashboard__section.if-no-js-hide
= render "admin/dashboard/applications_report", url: sustainable_development_admin_dashboard_reports_path(kind: "by_week"), title: "Sustainable development", kind: "by_week"
.dashboard__section.if-js-hide
= render "admin/dashboard/applications_report", url: sustainable_development_admin_dashboard_reports_path(kind: "by_week", format: :csv), title: "Sustainable development", kind: "by_week"
.dashboard__section
= render "admin/dashboard/totals_by_week/registrations"
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.dashboard-table__wrapper.dashboard-report
button.btn.btn-primary.btn--load.update-report href=account_registrations_admin_dashboard_reports_path(kind: "by_week") aria-label="Load data for new account registrations table"
button.btn.btn-primary.btn--load.update-report.if-no-js-hide href=account_registrations_admin_dashboard_reports_path(kind: "by_week") aria-label="Load data for new account registrations table"
| Load data
= link_to "Download data", account_registrations_admin_dashboard_reports_path(kind: "by_week", format: :csv),
class: "btn btn-primary btn--load update-report if-js-hide", aria: { label: "Download registration totals by week" }
button.btn.btn-primary.btn--loading.updating-data.hidden
| Updating data

Expand Down

0 comments on commit 968a17a

Please sign in to comment.