Skip to content
Permalink
Browse files

Port the rankings page to rails!

Fixes #301.
  • Loading branch information...
AlbertoPdRF committed Aug 5, 2019
1 parent a605db1 commit 2869ae45671841a08c1174c5717f6ff06f3c5fc8
@@ -0,0 +1,20 @@
onPage('results#index', function() {
// Ajax searching
var $form = $('#results-query-form');
function submitForm() {
$form.trigger('submit.rails');
}

$form.on('change', '#region', submitForm);

// Necessary hack because Safari fires a popstate event on document load
$(window).load(function() {
setTimeout(function() {
// When back/forward is clicked the url changes since we use pushState,
// but the content is not reloaded so we have to do this manually.
$(window).on('popstate', function() {
location.reload();
});
}, 0);
});
});
@@ -38,6 +38,7 @@
@import "regional_organizations";
@import "users";
@import "competitions";
@import "results";
@import "registrations";
@import "wca-autocomplete";
@import "collapsible-panels";
@@ -0,0 +1,39 @@
#results-query-form {
width: 100%;
margin: 20px 0;

#event,
#region,
#type,
#years,
#show {
font-size: 12px;
font-weight: normal;
display: block;
min-width: 165px;
@media (max-width: $screen-sm) {
display: inline-block;
width: 100%;
}
}
}

#search-results {
position: relative;

&.results-list {
display: none;
}

#results-list {

li:nth-child(odd) {
background-color: $table-bg-accent;
}

li {
padding: 5px 10px;
font-size: 13px;
}
}
}
@@ -155,6 +155,11 @@ table.wca-results {
&.single {
text-align: right;
}
&.result {
text-align: right;
}
&.region {
}
}
tbody tr td {
padding: 3px 5px;
@@ -190,6 +195,15 @@ table.wca-results {
&.single {
text-align: right;
}
&.result {
text-align: right;
font-weight: bold;
}
&.region {
&.tied-previous {
@extend .text-muted;
}
}
}

tbody tr.sort-by-average td {
@@ -350,6 +364,10 @@ textarea {
cursor: pointer;
}
}

.active {
color: $blue;
}
}

// _associated_events_picker.html.erb specific styles
@@ -0,0 +1,279 @@
# frozen_string_literal: true

class ResultsController < ApplicationController
def index
support_old_links!

# Default params
params[:region] ||= "world"
params[:years] ||= "all years"
params[:show] ||= "100 persons"

@event_param = params[:event_id]
@region_param = params[:region]
@type_param = params[:type]
years_param = params[:years]
show_param = params[:show]

@years = Competition.non_future_years

@quantities = ["100", "1000"]

@types = ["single", "average"]
@is_average = @type_param == @types[1]
value = @is_average ? "average" : "best"
capitalized_type_param = @type_param.capitalize

event_condition = "AND eventId = '#{@event_param}'"
event_condition_without_and = "eventId = '#{@event_param}'"

@is_by_region = show_param == "by region"
splitted_show_param = show_param.split
@show = splitted_show_param[0]
@is_persons = splitted_show_param[1] == "persons"
@is_results = splitted_show_param[1] == "results"
limit_condition = "LIMIT #{@show}"

id_string = @is_results ? ".id" : "Id"
@continent = Continent.c_find(@region_param)
@country = Country.c_find(@region_param)
if @continent.present?
region_condition = "AND continentId = '#{@continent.id}'"
elsif @country.present?
region_condition = "AND country#{id_string} = '#{@country.id}'"
else
region_condition = ""
end

@is_all_years = years_param == "all years"
splitted_years_param = years_param.split
@is_only = splitted_years_param[0] == "only"
@is_until = splitted_years_param[0] == "until"
@year = splitted_years_param[1]
if @is_only
years_condition = "AND year = #{@year}"
elsif @is_until
years_condition = "AND year <= #{@year}"
else
years_condition = ""
end

if @is_persons
query = <<-SQL
SELECT result.*,
result.#{value} value,
competition.cellName competitionName,
country.name countryName
FROM
(SELECT MIN(valueAndId) valueAndId
FROM Concise#{capitalized_type_param}Results result
WHERE #{value} > 0
#{event_condition}
#{years_condition}
#{region_condition}
GROUP BY personId
ORDER BY valueAndId
#{limit_condition}) top,
Results result,
Competitions competition,
Countries country
WHERE
result.id = valueAndId % 1000000000
AND competition.id = competitionId
AND country.id = result.countryId
ORDER BY
value, personName
SQL
elsif @is_results
if @is_average
query = <<-SQL
SELECT result.*,
result.#{value} value,
competition.cellName competitionName,
country.name countryName
FROM
Results result,
Competitions competition,
Countries country
WHERE #{value} > 0
#{event_condition}
#{years_condition}
#{region_condition}
AND competition.id = competitionId
AND country.id = result.countryId
ORDER BY
value, best, personName, competitionId, roundTypeId
#{limit_condition}
SQL
else
subqueries = []
(1..5).each do |i|
subqueries[i-1] = <<-SQL
SELECT
value#{i} value,
personId, personName,
country.id countryId,
competitionId,
competition.cellName competitionName,
roundTypeId
FROM
Results result,
Competitions competition,
Countries country
WHERE value#{i} > 0
#{event_condition}
#{years_condition}
#{region_condition}
AND competition.id = competitionId
AND country.id = result.countryId
ORDER BY
value, personName
#{limit_condition}
SQL
end
subquery = ("(" + subqueries.join(") UNION ALL (") + ")")

query = <<-SQL
SELECT *
FROM
(#{subquery}) result
ORDER BY
value, personName, competitionId, roundTypeId
#{limit_condition}
SQL
end
elsif @is_by_region
query = <<-SQL
SELECT
result.personId personId,
result.personName personName,
result.formatId formatId,
result.roundTypeId roundTypeId,
country.id countryId,
country.name countryName,
continent.id continentId,
continent.name continentName,
competition.id competitionId,
competition.cellName competitionName,
#{value} value,
event.format valueFormat,
value1, value2, value3, value4, value5
FROM
(SELECT countryId recordCountryId, MIN(#{value}) recordValue
FROM Concise#{capitalized_type_param}Results result
WHERE 1
#{event_condition}
#{years_condition}
GROUP BY countryId) record,
Results result,
Countries country,
Continents continent,
Competitions competition,
Events event
WHERE
#{event_condition_without_and}
#{years_condition}
AND result.#{value} = recordValue
AND result.countryId = recordCountryId
AND country.id = result.countryId
AND continent.id = continentId
AND competition.id = competitionId
AND event.id = eventId
ORDER BY
value, countryId, year, month, day, personName
SQL
else
flash[:danger] = t(".unknown_show")
return redirect_to results_rankings_path
end

@rows = ActiveRecord::Base.connection.exec_query(query)
compute_by_region if @is_by_region

respond_to do |format|
format.html {}
format.js do
# We change the browser's history when replacing url after an Ajax request.
# So we must prevent a browser from caching the JavaScript response.
# It's necessary because if the browser caches the response, the user will see a JavaScript response
# when he clicks browser back/forward buttons.
response.headers["Cache-Control"] = "no-cache, no-store"
render 'index', locals: { current_path: request.original_fullpath }
end
end
end

def compute_by_region
best_value_of_world = @rows.first["value"]
best_values_of_continents = {}
best_values_of_countries = {}
world_rows = []
continents_rows = []
countries_rows = []
@rows.each do |row|
result = LightResult.new(row, Country.c_find(row["countryId"]), Format.c_find(row["formatId"]), RoundType.c_find(row["roundTypeId"]), Event.c_find(@event_param))
value = row["value"]

world_rows << row if value == best_value_of_world

if best_values_of_continents[result.country.continent.id].nil? || value == best_values_of_continents[result.country.continent.id]
best_values_of_continents[result.country.continent.id] = value

if (@country.present? && @country.continent.id == result.country.continent.id) || (@continent.present? && @continent.id == result.country.continent.id) || @region_param == "world"
continents_rows << row
end
end

if best_values_of_countries[result.country.id].nil? || value == best_values_of_countries[result.country.id]
best_values_of_countries[result.country.id] = value

if (@country.present? && @country.id == result.country.id) || @region_param == "world"
countries_rows << row
end
end
end

@change_to_continents = world_rows.length
@change_to_countries = @change_to_continents + continents_rows.length
@rows_to_display = world_rows + continents_rows + countries_rows
end

# Normalizes the params so that old links to index still work.
private def support_old_links!
if params[:eventId].present?
params[:event_id] = params[:eventId]
params[:eventId] = nil
end

if params[:single].present?
params[:type] = params[:single].downcase!
params[:single] = nil
end
if params[:average].present?
params[:type] = params[:average].downcase!
params[:average] = nil
end

if params[:regionId].present?
params[:region_id] = params[:regionId]
params[:regionId] = nil
end

if params[:years].present?
if params[:years] == "all"
params[:years] = "all years"
elsif params[:years].include?("+")
params[:years].sub("+", " ")
end
end

if params[:show].present?
if params[:show].include?("All")
# We are not supporting the all option anymore!
params[:show] = "100 persons"
elsif params[:show].include?("+")
params[:show].sub("+", " ").downcase!
end
end
end
end

0 comments on commit 2869ae4

Please sign in to comment.
You can’t perform that action at this time.