Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a simple leaderboard #208

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/leaderboard_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class LeaderboardController < ApplicationController
skip_before_action :authenticate_user!, only: %i[index]

def index
@filter = params[:filter] || "all_time"
@ranked_speakers = Speaker.left_joins(:talks)
.group(:id)
.order("COUNT(talks.id) DESC")
.select("speakers.*, COUNT(talks.id) as talk_count")

if @filter == "last_12_months"
@ranked_speakers = @ranked_speakers.where("talks.date >= ?", 12.months.ago.to_date)
end

@ranked_speakers = @ranked_speakers.limit(100)
end
end
41 changes: 41 additions & 0 deletions app/views/leaderboard/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<div class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold mb-6">Speaker Leaderboard</h1>

<div class="mb-6">
<%= link_to "All Time", leaderboard_path(filter: "all_time"), class: "btn btn-sm #{(@filter == "all_time") ? "btn-primary" : "btn-outline"} mr-2" %>
<%= link_to "Last 12 Months", leaderboard_path(filter: "last_12_months"), class: "btn btn-sm #{(@filter == "last_12_months") ? "btn-primary" : "btn-outline"}" %>
</div>

<div class="overflow-x-auto">
<table class="table w-full table-pin-rows">
<thead>
<tr>
<th class="text-left">Rank</th>
<th class="text-left">Name</th>
<th class="text-left">Talks</th>
</tr>
</thead>
<tbody>
<% @ranked_speakers.each_with_index do |speaker, index| %>
<tr>
<td class="font-bold"><%= index + 1 %></td>
<td>
<%= link_to speaker_path(speaker), class: "link link-hover" do %>
<div class="flex items-center gap-4">
<% if speaker.github.present? %>
<%= image_tag speaker.github_avatar_url(size: 128), class: "rounded-full w-10 h-10", alt: "GitHub picture profile of #{speaker.name}", loading: :lazy %>
<% else %>
<div class="rounded-full w-10 h-10 bg-gray-200"></div>
<% end %>

<%= speaker.name %>
</div>
<% end %>
</td>
<td><%= speaker.talk_count %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<%= render "shared/navbar/link", link_title: "Talks", path: talks_path %>
<%= render "shared/navbar/link", link_title: "Speakers", path: speakers_path %>
<%= render "shared/navbar/link", link_title: "Events", path: events_path %>
<%= render "shared/navbar/link", link_title: "Leaderboard", path: leaderboard_path %>
<% signed_in do %>
<%= render "shared/navbar/link", link_title: "Watch List", path: watch_lists_path %>
<% if Current.user&.admin? %>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
resources :enhance, only: [:update], param: :slug
end

get "leaderboard", to: "leaderboard#index"

# admin
namespace :admin, if: -> { Current.user & admin? } do
resources :suggestions, only: %i[index update destroy]
Expand Down