Add state for votes
This commit is contained in:
parent
abc26f733f
commit
aefc722c9d
5 changed files with 177 additions and 49 deletions
|
|
@ -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_28_010547) do
|
||||
ActiveRecord::Schema[7.2].define(version: 2025_03_28_014902) do
|
||||
create_table "candidates", force: :cascade do |t|
|
||||
t.integer "vote_id"
|
||||
t.string "name"
|
||||
|
|
@ -53,6 +53,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_28_010547) do
|
|||
t.datetime "expire_on"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "state"
|
||||
end
|
||||
|
||||
add_foreign_key "ratings", "votes"
|
||||
|
|
|
|||
56
views/votes_edit.erb
Normal file
56
views/votes_edit.erb
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<h1>Edit draft vote</h1>
|
||||
|
||||
<form action="/votes/<%= @vote.secure_id %>/edit" method="post">
|
||||
<p>
|
||||
<label for="title">Title</label>
|
||||
<input type="text" name="title" value="<%= @vote.title %>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="description">Description</label>
|
||||
<input type="text" name="description" value="<%= @vote.description %>">
|
||||
</p>
|
||||
<button type="submit">Update vote description</button>
|
||||
</form>
|
||||
|
||||
<h2>Candidates</h2>
|
||||
|
||||
<% @vote.candidates.each do |candidate| %>
|
||||
<h3><%= candidate.name %></h3>
|
||||
<p><%= candidate.description %></p>
|
||||
<% end %>
|
||||
|
||||
<h3>Add candidate</h3>
|
||||
|
||||
<form action="/votes/<%= @vote.secure_id %>/candidates" method="post">
|
||||
<p>
|
||||
<label for="name">Name</label>
|
||||
<input type="text" name="name">
|
||||
</p>
|
||||
<p>
|
||||
<label for="description">Description</label>
|
||||
<input type="text" name="description">
|
||||
</p>
|
||||
<button type="submit">Add new candidate</button>
|
||||
</form>
|
||||
|
||||
<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>Start voting period</h2>
|
||||
|
||||
<form action="/votes/<%= @vote.secure_id %>/open" method="post">
|
||||
<button type="submit">Open vote to participants</button>
|
||||
</form>
|
||||
44
views/votes_results.erb
Normal file
44
views/votes_results.erb
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<h1><%= @vote.title %></h1>
|
||||
|
||||
<p><%= @vote.description %></p>
|
||||
|
||||
<form action="/votes/<%= @vote.secure_id %>/reopen" method="post">
|
||||
<button type="submit">Reopen voting period</button>
|
||||
</form>
|
||||
|
||||
<h2>Organizers</h2>
|
||||
|
||||
<ul>
|
||||
<% @vote.organizers.each do |organizer| %>
|
||||
<li><%= organizer.user.email %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
<h2>All ratings</h2>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Participant</th>
|
||||
<% @vote.candidates.each do |candidate| %>
|
||||
<th><%= candidate.name %></th>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% @vote.ratings.collect { |rating| rating.user }.uniq.each do |user| %>
|
||||
<tr>
|
||||
<td><%= user.email %></td>
|
||||
<% @vote.candidates.each do |candidate| %>
|
||||
<% if rating = @vote.ratings.find { |rating| rating.user == user and rating.candidate == candidate } %>
|
||||
<td><%= rating.value %></td>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<h2>Results</h2>
|
||||
|
||||
<ol>
|
||||
<% @vote.candidates.sort { |a, b| a.mj <=> b.mj }.reverse.each do |candidate| %>
|
||||
<li><%= candidate.name %>: <%= candidate.mj.mj %></li>
|
||||
<% end %>
|
||||
</ol>
|
||||
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
<p><%= @vote.description %></p>
|
||||
|
||||
<form action="/votes/<%= @vote.secure_id %>/draft" method="post">
|
||||
<button type="submit">Change back to draft vote</button>
|
||||
</form>
|
||||
|
||||
<h2>Organizers</h2>
|
||||
|
||||
<ul>
|
||||
|
|
@ -10,13 +14,13 @@
|
|||
<% 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>Participants</h2>
|
||||
|
||||
<ul>
|
||||
<% @vote.ratings.collect { |rating| rating.user }.uniq.each do |user| %>
|
||||
<li><%= user.email %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
<h2>Your ratings</h2>
|
||||
|
||||
|
|
@ -43,45 +47,8 @@
|
|||
<button type="submit">Save ratings</button>
|
||||
</form>
|
||||
|
||||
<h2>All ratings</h2>
|
||||
<h2>Close voting period</h2>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Participant</th>
|
||||
<% @vote.candidates.each do |candidate| %>
|
||||
<th><%= candidate.name %></th>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% @vote.ratings.collect { |rating| rating.user }.uniq.each do |user| %>
|
||||
<tr>
|
||||
<td><%= user.email %></td>
|
||||
<% @vote.candidates.each do |candidate| %>
|
||||
<% if rating = @vote.ratings.find { |rating| rating.user == user and rating.candidate == candidate } %>
|
||||
<td><%= rating.value %></td>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<h2>Results</h2>
|
||||
|
||||
<ol>
|
||||
<% @vote.candidates.sort { |a, b| a.mj <=> b.mj }.reverse.each do |candidate| %>
|
||||
<li><%= candidate.name %>: <%= candidate.mj.mj %></li>
|
||||
<% end %>
|
||||
</ol>
|
||||
|
||||
<h2>New candidate</h2>
|
||||
|
||||
<form action="/votes/<%= @vote.secure_id %>/candidates" method="post">
|
||||
<p>
|
||||
<label for="name">Name</label>
|
||||
<input type="text" name="name">
|
||||
</p>
|
||||
<p>
|
||||
<label for="description">Description</label>
|
||||
<input type="text" name="description">
|
||||
</p>
|
||||
<button type="submit">Add new candidate</button>
|
||||
<form action="/votes/<%= @vote.secure_id %>/close" method="post">
|
||||
<button type="submit">Show results</button>
|
||||
</form>
|
||||
|
|
|
|||
62
vote.rb
62
vote.rb
|
|
@ -10,6 +10,7 @@ class Vote < ActiveRecord::Base
|
|||
has_many :ratings
|
||||
has_many :organizers
|
||||
has_many :users, through: :organizers
|
||||
validates :state, inclusion: { in: ["draft", "open", "closed"] }
|
||||
end
|
||||
|
||||
class Candidate < ActiveRecord::Base
|
||||
|
|
@ -95,7 +96,28 @@ end
|
|||
get '/votes/:id' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
erb :votes_show
|
||||
case @vote.state
|
||||
when "open"
|
||||
erb :votes_show
|
||||
when "draft"
|
||||
erb :votes_edit
|
||||
when "closed"
|
||||
erb :votes_results
|
||||
else
|
||||
@vote.state = "draft"
|
||||
@vote.save
|
||||
erb :votes_edit
|
||||
end
|
||||
end
|
||||
|
||||
post '/votes/:id/edit' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
redirect '/votes/' + vote.secure_id if @vote.state != "draft"
|
||||
@vote.title = params[:title]
|
||||
@vote.description = params[:description]
|
||||
@vote.save
|
||||
erb :votes_edit
|
||||
end
|
||||
|
||||
post '/votes' do
|
||||
|
|
@ -106,7 +128,9 @@ post '/votes' do
|
|||
end
|
||||
|
||||
post '/votes/:id/candidates' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
redirect '/votes/' + vote.secure_id if @vote.state != "draft"
|
||||
@candidate = Candidate.new(name: params[:name],
|
||||
description: params[:description])
|
||||
@candidate.vote = @vote
|
||||
|
|
@ -114,6 +138,42 @@ post '/votes/:id/candidates' do
|
|||
redirect '/votes/' + @vote.secure_id
|
||||
end
|
||||
|
||||
post '/votes/:id/open' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
redirect '/votes/' + vote.secure_id if @vote.state != "draft"
|
||||
@vote.state = "open"
|
||||
@vote.save
|
||||
redirect '/votes/' + @vote.secure_id
|
||||
end
|
||||
|
||||
post '/votes/:id/draft' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
redirect '/votes/' + vote.secure_id if @vote.state != "open"
|
||||
@vote.state = "draft"
|
||||
@vote.save
|
||||
redirect '/votes/' + @vote.secure_id
|
||||
end
|
||||
|
||||
post '/votes/:id/close' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
redirect '/votes/' + vote.secure_id if @vote.state != "open"
|
||||
@vote.state = "closed"
|
||||
@vote.save
|
||||
redirect '/votes/' + @vote.secure_id
|
||||
end
|
||||
|
||||
post '/votes/:id/reopen' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
redirect '/votes/' + vote.secure_id if @vote.state != "closed"
|
||||
@vote.state = "open"
|
||||
@vote.save
|
||||
redirect '/votes/' + @vote.secure_id
|
||||
end
|
||||
|
||||
post '/votes/:id/ratings' do
|
||||
redirect '/login' unless current_user
|
||||
vote = Vote.find_by(secure_id: params[:id])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue