diff --git a/app/controllers/contestants_controller.rb b/app/controllers/contestants_controller.rb index 42971fa..a8130d6 100644 --- a/app/controllers/contestants_controller.rb +++ b/app/controllers/contestants_controller.rb @@ -111,6 +111,19 @@ class ContestantsController < ApplicationController end end + def export + authorize @contest + + @contestants = @contest.contestants.sort_by { |contestant| [ -contestant.completions.size, contestant.time_seconds ] } + + respond_to do |format| + format.csv do + response.headers["Content-Type"] = "text/csv" + response.headers["Content-Disposition"] = "attachment; filename=export.csv" + end + end + end + private def set_contest diff --git a/app/policies/contest_policy.rb b/app/policies/contest_policy.rb index 3ffa791..0b96f35 100644 --- a/app/policies/contest_policy.rb +++ b/app/policies/contest_policy.rb @@ -43,6 +43,10 @@ class ContestPolicy < ApplicationPolicy record.user.id == user.id || user.admin? end + def export? + record.user.id == user.id || user.admin? + end + def scoreboard? true end diff --git a/app/views/contestants/export.csv.slim b/app/views/contestants/export.csv.slim new file mode 100644 index 0000000..913ee86 --- /dev/null +++ b/app/views/contestants/export.csv.slim @@ -0,0 +1,4 @@ += CSV.generate_line [t("helpers.rank"), t("activerecord.attributes.contestant.name"), t("activerecord.attributes.contestant.display_time"), t("activerecord.attributes.contestant.completions")] + +- @contestants.each_with_index do |contestant, index| + = CSV.generate_line([index + 1, contestant.name, contestant.display_time, contestant.completions.length]) \ No newline at end of file diff --git a/app/views/contests/show.html.slim b/app/views/contests/show.html.slim index a85640d..abe5cf4 100644 --- a/app/views/contests/show.html.slim +++ b/app/views/contests/show.html.slim @@ -40,8 +40,10 @@ javascript: = t("contestants.plural").capitalize a.ms-3.btn.btn-sm.btn-primary href=new_contest_contestant_path(@contest) style="margin-top: -3px" | + #{t("helpers.buttons.add")} - a.ms-2.btn-sm.btn.btn-primary href=contest_import_path(@contest) style="margin-top: -3px" + a.ms-2.btn.btn-sm.btn.btn-primary href=contest_import_path(@contest) style="margin-top: -3px" | #{t("helpers.buttons.import")} + a.ms-2.btn.btn-sm.btn.btn-primary href="/contests/#{@contest.id}/export.csv" style="margin-top: -3px" + | #{t("helpers.buttons.export")} - if @contest.categories.size > 0 .row .col diff --git a/config/locales/en.yml b/config/locales/en.yml index dd1cb8e..31a0554 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -204,6 +204,7 @@ en: create: "Create" delete: "Delete" edit: "Edit" + export: Export import: CSV Import open: Open refresh: Refresh diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 76ad0e7..ae24d03 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -175,6 +175,7 @@ fr: create: "Créer" delete: "Supprimer" edit: "Modifier" + export: Exporter import: Importer un CSV open: Détails refresh: Rafraîchir diff --git a/config/routes.rb b/config/routes.rb index d0e5e17..b5c9edd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,6 +20,7 @@ Rails.application.routes.draw do post "import", to: "contestants#upload_csv" get "import/:id", to: "contestants#convert_csv" post "import/:id", to: "contestants#finalize_import" + get "export", to: "contestants#export" end resources :passwords, param: :token resource :session