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

leaderboard #86

Merged
merged 2 commits into from
Oct 23, 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
6 changes: 5 additions & 1 deletion app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class NotesController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user!, except: [ :leaderboard ]
before_action :set_note, only: [ :edit, :update, :destroy ]

def index
Expand Down Expand Up @@ -50,6 +50,10 @@ def destroy
redirect_to notes_path, notice: "Note deleted successfully."
end

def leaderboard
@leaderboard = User.leaderboard(10)
end

private

def set_note
Expand Down
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def subscribed?
subscriptions.where(status: "active").any?
end

def self.leaderboard(limit = 10)
self.left_joins(:notes)
.group(:id)
.order("COUNT(notes.id) DESC")
.select("users.id, users.username, COUNT(notes.id) AS score")
.where("notes.archived = true AND notes.completed = true")
.where(public: true)
.limit(limit)
end

private

def send_welcome_email
Expand Down
21 changes: 21 additions & 0 deletions app/views/notes/leaderboard.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="my-container bg-gray-100 p-4 shadow-lg border-4 border-black">
<h2 class="text-3xl font-bold mb-4">Leaderboard</h2>
<table class="w-full table-auto border-collapse border border-black">
<thead class="bg-black text-white">
<tr>
<th class="p-2 border border-black">Rank</th>
<th class="p-2 border border-black">User</th>
<th class="p-2 border border-black">Score</th>
</tr>
</thead>
<tbody>
<% @leaderboard.each_with_index do |user, index| %>
<tr class="<%= cycle('bg-gray-200', 'bg-gray-50') %>">
<td class="p-2 border border-black"><%= index + 1 %></td>
<td class="p-2 border border-black"><%= user.username %></td>
<td class="p-2 border border-black"><%= user.score %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
end
end

get "leaderboard", to: "notes#leaderboard"

# Stripe
scope controller: :static do
get :pricing
Expand Down