Create model for organizers

This commit is contained in:
ricola 2025-04-06 17:04:31 -06:00
parent c8bfea4898
commit abc26f733f
3 changed files with 44 additions and 2 deletions

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2025_03_22_172051) do
ActiveRecord::Schema[7.2].define(version: 2025_03_28_010547) do
create_table "candidates", force: :cascade do |t|
t.integer "vote_id"
t.string "name"
@ -20,6 +20,13 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_22_172051) do
t.index ["vote_id"], name: "index_candidates_on_vote_id"
end
create_table "organizers", force: :cascade do |t|
t.integer "vote_id"
t.integer "user_id"
t.index ["user_id"], name: "index_organizers_on_user_id"
t.index ["vote_id"], name: "index_organizers_on_vote_id"
end
create_table "ratings", force: :cascade do |t|
t.integer "user_id"
t.integer "candidate_id"

View file

@ -2,10 +2,26 @@
<p><%= @vote.description %></p>
<p>Hello, <%= current_user.email %>.</p>
<h2>Organizers</h2>
<ul>
<% @vote.organizers.each do |organizer| %>
<li><%= organizer.user.email %></li>
<% end %>
</ul>
<form action="/votes/<%= @vote.secure_id %>/organizers" method="post">
<p>
<label for="email">Email</label>
<input type="text" name="email">
</p>
<button type="submit">Add new organizer</button>
</form>
<h2>Your ratings</h2>
<p>Hello, <%= current_user.email %>.</p>
<form action="/votes/<%= @vote.secure_id %>/ratings" method="post">
<ul>
<% @vote.candidates.each do |candidate| %>

19
vote.rb
View file

@ -8,6 +8,8 @@ require_relative 'mj'
class Vote < ActiveRecord::Base
has_many :candidates
has_many :ratings
has_many :organizers
has_many :users, through: :organizers
end
class Candidate < ActiveRecord::Base
@ -22,6 +24,15 @@ end
class User < ActiveRecord::Base
has_many :ratings
has_many :organizers
has_many :votes, through: :organizers
end
class Organizer < ActiveRecord::Base
belongs_to :vote
belongs_to :user
validates :vote_id, uniqueness: { scope: :user_id }
validates :user_id, uniqueness: { scope: :vote_id }
end
class Rating < ActiveRecord::Base
@ -114,6 +125,14 @@ post '/votes/:id/ratings' do
redirect '/votes/' + vote.secure_id
end
post '/votes/:id/organizers' do
redirect '/login' unless current_user
vote = Vote.find_by(secure_id: params[:id])
user = User.find_by(email: params[:email])
vote.users << user
redirect '/votes/' + vote.secure_id
end
helpers do
def current_user
if session[:user_id]