Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #257 from bellroy/master
Browse files Browse the repository at this point in the history
Merge branch master into stable
  • Loading branch information
trike-deploy authored Aug 1, 2023
2 parents 3c2c949 + 117f61b commit 3fa7371
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
16 changes: 3 additions & 13 deletions app/controllers/api/my_predictions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 26 additions & 2 deletions app/controllers/predictions_controller.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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, [])
Expand Down
1 change: 1 addition & 0 deletions app/views/users/settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<p><%= 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.' } %></p>

<h2><%= link_to 'View notifications', [@user,:deadline_notifications] %></h2>
<h2><%= link_to 'Export my predictions as CSV', mine_predictions_path %></h2>
<br>
<% if @user.api_token.present? %>
<h2>API Token: <%= @user.api_token %></h2>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

resources :predictions do
collection do
get :mine, format: :csv
get :recent
get :unjudged
get :judged
Expand Down

0 comments on commit 3fa7371

Please sign in to comment.