diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb
index 70c0386..64627e7 100644
--- a/app/controllers/admin_controller.rb
+++ b/app/controllers/admin_controller.rb
@@ -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
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
index 51c1789..2d8011b 100644
--- a/app/controllers/pages_controller.rb
+++ b/app/controllers/pages_controller.rb
@@ -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
diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb
new file mode 100644
index 0000000..b713001
--- /dev/null
+++ b/app/controllers/votes_controller.rb
@@ -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
diff --git a/app/models/submission.rb b/app/models/submission.rb
index 149beab..d507e7a 100644
--- a/app/models/submission.rb
+++ b/app/models/submission.rb
@@ -1,5 +1,6 @@
class Submission < ApplicationRecord
belongs_to :user
+ has_many :votes
validates :line1, :line2, :line3, presence: true
diff --git a/app/models/user.rb b/app/models/user.rb
index 8fb17a2..032a7dc 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -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|
diff --git a/app/models/vote.rb b/app/models/vote.rb
new file mode 100644
index 0000000..d31cb3b
--- /dev/null
+++ b/app/models/vote.rb
@@ -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
diff --git a/app/views/admin/index.html.erb b/app/views/admin/index.html.erb
index 45c793d..438e03c 100644
--- a/app/views/admin/index.html.erb
+++ b/app/views/admin/index.html.erb
@@ -11,6 +11,7 @@
<%= submission.user.github_username %>
(<%= submission.user.github_email %>)
· <%= time_ago_in_words(submission.created_at) %> ago
+ · <%= submission.votes.size %> vote<%= submission.votes.size == 1 ? "" : "s" %>
<% if submission.discarded? %>
— discarded
<% end %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 44402aa..1f9de27 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -13,9 +13,11 @@
<%= yield :head %>
+
-
+
+
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %>
diff --git a/app/views/pages/home.html.erb b/app/views/pages/home.html.erb
index f5dd016..c231be9 100644
--- a/app/views/pages/home.html.erb
+++ b/app/views/pages/home.html.erb
@@ -56,6 +56,27 @@
<%= submission.line2 %>
<%= submission.line3 %>