diff --git a/db/schema.rb b/db/schema.rb
index c5b9cd1..f42b321 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -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"
diff --git a/views/votes_edit.erb b/views/votes_edit.erb
new file mode 100644
index 0000000..4004bbc
--- /dev/null
+++ b/views/votes_edit.erb
@@ -0,0 +1,56 @@
+
Edit draft vote
+
+
+
+Candidates
+
+<% @vote.candidates.each do |candidate| %>
+<%= candidate.name %>
+<%= candidate.description %>
+<% end %>
+
+Add candidate
+
+
+
+Organizers
+
+
+ <% @vote.organizers.each do |organizer| %>
+ - <%= organizer.user.email %>
+ <% end %>
+
+
+
+
+Start voting period
+
+
diff --git a/views/votes_results.erb b/views/votes_results.erb
new file mode 100644
index 0000000..01b14f9
--- /dev/null
+++ b/views/votes_results.erb
@@ -0,0 +1,44 @@
+<%= @vote.title %>
+
+<%= @vote.description %>
+
+
+
+Organizers
+
+
+ <% @vote.organizers.each do |organizer| %>
+ - <%= organizer.user.email %>
+ <% end %>
+
+
+All ratings
+
+
+
+ | Participant |
+ <% @vote.candidates.each do |candidate| %>
+ <%= candidate.name %> |
+ <% end %>
+
+ <% @vote.ratings.collect { |rating| rating.user }.uniq.each do |user| %>
+
+ | <%= user.email %> |
+ <% @vote.candidates.each do |candidate| %>
+ <% if rating = @vote.ratings.find { |rating| rating.user == user and rating.candidate == candidate } %>
+ <%= rating.value %> |
+ <% end %>
+ <% end %>
+
+ <% end %>
+
+
+Results
+
+
+ <% @vote.candidates.sort { |a, b| a.mj <=> b.mj }.reverse.each do |candidate| %>
+ - <%= candidate.name %>: <%= candidate.mj.mj %>
+ <% end %>
+
diff --git a/views/votes_show.erb b/views/votes_show.erb
index 2c9f763..d80ad31 100644
--- a/views/votes_show.erb
+++ b/views/votes_show.erb
@@ -2,6 +2,10 @@
<%= @vote.description %>
+
+
Organizers
@@ -10,13 +14,13 @@
<% end %>
-
+Participants
+
+
+<% @vote.ratings.collect { |rating| rating.user }.uniq.each do |user| %>
+ - <%= user.email %>
+<% end %>
+
Your ratings
@@ -43,45 +47,8 @@
-All ratings
+Close voting period
-
-
- | Participant |
- <% @vote.candidates.each do |candidate| %>
- <%= candidate.name %> |
- <% end %>
-
- <% @vote.ratings.collect { |rating| rating.user }.uniq.each do |user| %>
-
- | <%= user.email %> |
- <% @vote.candidates.each do |candidate| %>
- <% if rating = @vote.ratings.find { |rating| rating.user == user and rating.candidate == candidate } %>
- <%= rating.value %> |
- <% end %>
- <% end %>
-
- <% end %>
-
-
-Results
-
-
- <% @vote.candidates.sort { |a, b| a.mj <=> b.mj }.reverse.each do |candidate| %>
- - <%= candidate.name %>: <%= candidate.mj.mj %>
- <% end %>
-
-
-New candidate
-
-
diff --git a/vote.rb b/vote.rb
index 539078b..ac64e4a 100644
--- a/vote.rb
+++ b/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])