Create model for organizers
This commit is contained in:
parent
c8bfea4898
commit
abc26f733f
3 changed files with 44 additions and 2 deletions
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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|
|
create_table "candidates", force: :cascade do |t|
|
||||||
t.integer "vote_id"
|
t.integer "vote_id"
|
||||||
t.string "name"
|
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"
|
t.index ["vote_id"], name: "index_candidates_on_vote_id"
|
||||||
end
|
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|
|
create_table "ratings", force: :cascade do |t|
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
t.integer "candidate_id"
|
t.integer "candidate_id"
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,26 @@
|
||||||
|
|
||||||
<p><%= @vote.description %></p>
|
<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>
|
<h2>Your ratings</h2>
|
||||||
|
|
||||||
|
<p>Hello, <%= current_user.email %>.</p>
|
||||||
|
|
||||||
<form action="/votes/<%= @vote.secure_id %>/ratings" method="post">
|
<form action="/votes/<%= @vote.secure_id %>/ratings" method="post">
|
||||||
<ul>
|
<ul>
|
||||||
<% @vote.candidates.each do |candidate| %>
|
<% @vote.candidates.each do |candidate| %>
|
||||||
|
|
|
||||||
19
vote.rb
19
vote.rb
|
|
@ -8,6 +8,8 @@ require_relative 'mj'
|
||||||
class Vote < ActiveRecord::Base
|
class Vote < ActiveRecord::Base
|
||||||
has_many :candidates
|
has_many :candidates
|
||||||
has_many :ratings
|
has_many :ratings
|
||||||
|
has_many :organizers
|
||||||
|
has_many :users, through: :organizers
|
||||||
end
|
end
|
||||||
|
|
||||||
class Candidate < ActiveRecord::Base
|
class Candidate < ActiveRecord::Base
|
||||||
|
|
@ -22,6 +24,15 @@ end
|
||||||
|
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
has_many :ratings
|
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
|
end
|
||||||
|
|
||||||
class Rating < ActiveRecord::Base
|
class Rating < ActiveRecord::Base
|
||||||
|
|
@ -114,6 +125,14 @@ post '/votes/:id/ratings' do
|
||||||
redirect '/votes/' + vote.secure_id
|
redirect '/votes/' + vote.secure_id
|
||||||
end
|
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
|
helpers do
|
||||||
def current_user
|
def current_user
|
||||||
if session[:user_id]
|
if session[:user_id]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue