Display all ratings for a vote
This commit is contained in:
parent
5096134d2e
commit
46c8745a9e
3 changed files with 40 additions and 19 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_21_235135) do
|
||||
ActiveRecord::Schema[7.2].define(version: 2025_03_22_172051) do
|
||||
create_table "candidates", force: :cascade do |t|
|
||||
t.integer "vote_id"
|
||||
t.string "name"
|
||||
|
|
@ -26,8 +26,10 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_21_235135) do
|
|||
t.integer "value"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "vote_id", null: false
|
||||
t.index ["candidate_id"], name: "index_ratings_on_candidate_id"
|
||||
t.index ["user_id"], name: "index_ratings_on_user_id"
|
||||
t.index ["vote_id"], name: "index_ratings_on_vote_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
|
|
@ -45,4 +47,6 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_21_235135) do
|
|||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
add_foreign_key "ratings", "votes"
|
||||
end
|
||||
|
|
|
|||
|
|
@ -13,13 +13,13 @@
|
|||
<p><%= candidate.name %></p>
|
||||
<p><%= candidate.description %></p>
|
||||
<ol>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-7" value="7" <% if @ratings[candidate] and @ratings[candidate].value == 7 %>checked<% end %>><label for="<%= candidate.id %>-7">Excellent</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-6" value="6" <% if @ratings[candidate] and @ratings[candidate].value == 6 %>checked<% end %>><label for="<%= candidate.id %>-6">Very good</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-5" value="5" <% if @ratings[candidate] and @ratings[candidate].value == 5 %>checked<% end %>><label for="<%= candidate.id %>-5">Good</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-4" value="4" <% if @ratings[candidate] and @ratings[candidate].value == 4 %>checked<% end %>><label for="<%= candidate.id %>-4">Mediocre</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-3" value="3" <% if @ratings[candidate] and @ratings[candidate].value == 3 %>checked<% end %>><label for="<%= candidate.id %>-3">Bad</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-2" value="2" <% if @ratings[candidate] and @ratings[candidate].value == 2 %>checked<% end %>><label for="<%= candidate.id %>-2">Very bad</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-1" value="1" <% if @ratings[candidate] and @ratings[candidate].value == 1 %>checked<% end %>><label for="<%= candidate.id %>-1">Awful</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-7" value="7" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 7 } %>checked<% end %>><label for="<%= candidate.id %>-7">Excellent</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-6" value="6" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 6 } %>checked<% end %>><label for="<%= candidate.id %>-6">Very good</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-5" value="5" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 5 } %>checked<% end %>><label for="<%= candidate.id %>-5">Good</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-4" value="4" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 4 } %>checked<% end %>><label for="<%= candidate.id %>-4">Mediocre</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-3" value="3" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 3 } %>checked<% end %>><label for="<%= candidate.id %>-3">Bad</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-2" value="2" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 2 } %>checked<% end %>><label for="<%= candidate.id %>-2">Very bad</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-1" value="1" <% if @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate and rating.value == 1 } %>checked<% end %>><label for="<%= candidate.id %>-1">Awful</label>
|
||||
</ol>
|
||||
</li>
|
||||
<% end %>
|
||||
|
|
@ -27,6 +27,27 @@
|
|||
<button type="submit">Save ratings</button>
|
||||
</form>
|
||||
|
||||
<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>New candidate</h2>
|
||||
|
||||
<form action="/votes/<%= @vote.secure_id %>/candidates" method="post">
|
||||
|
|
|
|||
18
vote.rb
18
vote.rb
|
|
@ -7,12 +7,13 @@ require 'securerandom'
|
|||
#set :database, 'sqlite3:db/vote.sqlite3'
|
||||
|
||||
class Vote < ActiveRecord::Base
|
||||
self.primary_key = "secure_id"
|
||||
has_many :candidates
|
||||
has_many :ratings
|
||||
end
|
||||
|
||||
class Candidate < ActiveRecord::Base
|
||||
belongs_to :vote
|
||||
has_many :ratings
|
||||
end
|
||||
|
||||
class User < ActiveRecord::Base
|
||||
|
|
@ -20,6 +21,7 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
|
||||
class Rating < ActiveRecord::Base
|
||||
belongs_to :vote
|
||||
belongs_to :user
|
||||
belongs_to :candidate
|
||||
end
|
||||
|
|
@ -77,11 +79,7 @@ end
|
|||
|
||||
get '/votes/:id' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find(params[:id])
|
||||
@ratings = {}
|
||||
@vote.candidates.each do |candidate|
|
||||
@ratings[candidate] = Rating.where("user_id = ? and candidate_id = ?", current_user.id, candidate.id)[0]
|
||||
end
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
erb :votes_show
|
||||
end
|
||||
|
||||
|
|
@ -93,7 +91,7 @@ post '/votes' do
|
|||
end
|
||||
|
||||
post '/votes/:id/candidates' do
|
||||
@vote = Vote.find(params[:id])
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
@candidate = Candidate.new(name: params[:name],
|
||||
description: params[:description])
|
||||
@candidate.vote = @vote
|
||||
|
|
@ -103,11 +101,9 @@ end
|
|||
|
||||
post '/votes/:id/ratings' do
|
||||
redirect '/login' unless current_user
|
||||
vote = Vote.find(params[:id])
|
||||
vote = Vote.find_by(secure_id: params[:id])
|
||||
vote.candidates.each do |candidate|
|
||||
rating = Rating.find_by(user: current_user)
|
||||
rating = Rating.find_by(candidate: candidate)
|
||||
rating = Rating.find_or_initialize_by(user: current_user, candidate: candidate)
|
||||
rating = Rating.find_or_initialize_by(vote: vote, user: current_user, candidate: candidate)
|
||||
rating.value = params[candidate.id.to_s]
|
||||
rating.save
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue