Skip to content
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
2 changes: 1 addition & 1 deletion app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class AdminController < ApplicationController
before_action :require_admin

def index
@submissions = Submission.includes(:user).order(created_at: :desc)
@submissions = Submission.includes(:user, :votes).order(created_at: :desc)
end

def toggle_discard
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
class PagesController < ApplicationController
def home
@submissions = Submission.kept.order("RANDOM()")
@submissions = Submission.kept.includes(:votes).sort_by { |s| -s.votes.size }
if Current.user
@voted_submission_ids = Current.user.votes.where(submission_id: @submissions.map(&:id)).pluck(:submission_id).to_set
else
@voted_submission_ids = Set.new
end
end
end
26 changes: 26 additions & 0 deletions app/controllers/votes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class VotesController < ApplicationController
before_action :require_authentication
before_action :set_submission

def create
vote = @submission.votes.new(user: Current.user)

if vote.save
redirect_back fallback_location: root_path
else
redirect_back fallback_location: root_path, alert: vote.errors.full_messages.first
end
end

def destroy
vote = @submission.votes.find_by(user: Current.user)
vote&.destroy
redirect_back fallback_location: root_path
end

private

def set_submission
@submission = Submission.find(params[:submission_id])
end
end
1 change: 1 addition & 0 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Submission < ApplicationRecord
belongs_to :user
has_many :votes

validates :line1, :line2, :line3, presence: true

Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class User < ApplicationRecord
has_one :submission
has_many :votes

def self.find_or_create_from_omniauth(auth)
find_or_create_by(github_uid: auth.uid) do |user|
Expand Down
15 changes: 15 additions & 0 deletions app/models/vote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Vote < ApplicationRecord
belongs_to :user
belongs_to :submission

validates :user_id, uniqueness: { scope: :submission_id }
validate :not_own_submission

private

def not_own_submission
if submission && user_id == submission.user_id
errors.add(:base, "You cannot vote on your own submission")
end
end
end
1 change: 1 addition & 0 deletions app/views/admin/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<strong><%= submission.user.github_username %></strong>
(<%= submission.user.github_email %>)
&middot; <%= time_ago_in_words(submission.created_at) %> ago
&middot; <strong><%= submission.votes.size %></strong> vote<%= submission.votes.size == 1 ? "" : "s" %>
<% if submission.discarded? %>
<span class="text-red-400 font-medium">— discarded</span>
<% end %>
Expand Down
4 changes: 3 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
<%= yield :head %>

<link rel="stylesheet" href="https://use.typekit.net/bzt5uqd.css">
<link rel="icon" href="/favicon.ico" sizes="any">
<link rel="icon" href="/icon.png" type="image/png">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/icon.png">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/manifest.webmanifest">

<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %>
Expand Down
21 changes: 21 additions & 0 deletions app/views/pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@
<%= submission.line2 %><br>
<%= submission.line3 %>
</p>

<div class="mt-4 flex items-center justify-between gap-2">
<div class="text-gray-500">
<%= submission.votes.size %> vote<%= submission.votes.size == 1 ? "" : "s" %>
</div>

<div>
<% if Current.user && submission.user_id != Current.user.id %>
<% if @voted_submission_ids.include?(submission.id) %>
<%= button_to submission_vote_path(submission), method: :delete, class: "inline-flex items-center gap-1 text-sm px-3 py-1 rounded-full cursor-pointer bg-gray-400 bg-linear-to-t from-gray-500 to-gray-400 text-white" do %>
Voted
<% end %>
<% else %>
<%= button_to submission_vote_path(submission), method: :post, class: "inline-flex items-center gap-1 text-sm px-3 py-1 rounded-full cursor-pointer bg-[#C41C1C] bg-linear-to-t from-[#C41C1C] to-[#DD423E] text-white" do %>
⬆ Vote
<% end %>
<% end %>
<% end %>
</div>

</div>
</div>
<% end %>
</div>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
get "auth/failure", to: "sessions#failure"
delete "sign_out", to: "sessions#destroy"

resources :submissions, only: [ :new, :create, :update ]
resources :submissions, only: [ :new, :create, :update ] do
resource :vote, only: [ :create, :destroy ]
end

get "admin", to: "admin#index"
post "admin/:id/toggle_discard", to: "admin#toggle_discard", as: :admin_toggle_discard
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20260320191401_create_votes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateVotes < ActiveRecord::Migration[8.1]
def change
create_table :votes do |t|
t.references :user, null: false, foreign_key: true
t.references :submission, null: false, foreign_key: true

t.timestamps
end

add_index :votes, [ :user_id, :submission_id ], unique: true
end
end
14 changes: 13 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added public/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/favicon.ico
Binary file not shown.
Binary file added public/icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions public/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions public/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"icons": [
{ "src": "/icon-192.png", "type": "image/png", "sizes": "192x192" },
{ "src": "/icon-512.png", "type": "image/png", "sizes": "512x512" }
]
}
Loading