diff --git a/app/controllers/api/my_predictions_controller.rb b/app/controllers/api/my_predictions_controller.rb index ad1ec371..431152af 100644 --- a/app/controllers/api/my_predictions_controller.rb +++ b/app/controllers/api/my_predictions_controller.rb @@ -3,25 +3,15 @@ module Api class MyPredictionsController < AuthorisedController def index - render json: { user: @user.to_h, predictions: prediction_hash_array } - end - - private - - def prediction_hash_array - predictions.map do |prediction| - PredictionSerializer.new(prediction).serializable_hash - end - end - - def predictions - PredictionsQuery.new( + ps = PredictionsQuery.new( page: params[:page].to_i, page_size: params[:page_size].to_i, predictions: @user.predictions.not_withdrawn, status: 'recent', tag_names: params.fetch(:tag_names, []) ).call + serialized_predictions = ps.map { |p| PredictionSerializer.new(p).serializable_hash } + render json: { user: @user.to_h, predictions: serialized_predictions } end end end diff --git a/app/controllers/predictions_controller.rb b/app/controllers/predictions_controller.rb index 5610f4cc..9215a0b3 100644 --- a/app/controllers/predictions_controller.rb +++ b/app/controllers/predictions_controller.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true +require 'csv' + class PredictionsController < ApplicationController - before_action :authenticate_user!, only: %i[new create judge withdraw edit update] + before_action :authenticate_user!, only: %i[new create judge withdraw edit update mine] before_action :find_prediction, only: %i[judge show withdraw edit update] before_action :must_be_authorized_for_prediction, only: %i[withdraw edit update show] before_action :ensure_statistics, only: [:index] @@ -52,6 +54,28 @@ def home @show_statistics = false end + def mine + ps = PredictionsQuery.new( + page: params[:page].to_i, + page_size: params[:page_size].to_i, + predictions: current_user.predictions.not_withdrawn, + status: 'recent', + tag_names: params.fetch(:tag_names, []) + ).call + serialized_predictions = ps.map { |p| PredictionSerializer.new(p).serializable_hash.except(:responses) } + generated_csv = CSV.generate do |csv| + written_column_headers = false + serialized_predictions.each do |h| + unless written_column_headers + csv << h.keys + written_column_headers = true + end + csv << h.values + end + end + send_data(generated_csv, filename: 'my_predictions.csv') + end + def recent # TODO: remove this in a month or so redirect_to predictions_path, status: :moved_permanently @@ -70,7 +94,7 @@ def index @title = 'Recent Predictions' @filter = 'recent' @predictions = PredictionsQuery.new( - page: params[:page].to_i, + page: params[:page].to_i, predictions: Prediction.visible_to_everyone, status: @filter, tag_names: params.fetch(:tag_names, []) diff --git a/app/views/users/settings.html.erb b/app/views/users/settings.html.erb index 763c634d..487044b6 100644 --- a/app/views/users/settings.html.erb +++ b/app/views/users/settings.html.erb @@ -12,6 +12,7 @@

<%= link_to 'Delete my account!', user_path(@user), method: :delete, data: { confirm: 'Are you sure you want to delete your account? This action is irreversible.' } %>

<%= link_to 'View notifications', [@user,:deadline_notifications] %>

+

<%= link_to 'Export my predictions as CSV', mine_predictions_path %>


<% if @user.api_token.present? %>

API Token: <%= @user.api_token %>

diff --git a/config/routes.rb b/config/routes.rb index 90324f69..a8f1d85d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,7 @@ resources :predictions do collection do + get :mine, format: :csv get :recent get :unjudged get :judged