Force ratings on all candidates before saving

This commit is contained in:
ricola 2025-10-13 20:07:34 +02:00
parent fa57d465be
commit b07b0da672
2 changed files with 35 additions and 5 deletions

View file

@ -11,6 +11,7 @@ require 'gettext'
require 'securerandom' require 'securerandom'
require 'chartkick' require 'chartkick'
require 'mail' require 'mail'
require 'ostruct'
require 'tzinfo' require 'tzinfo'
require 'redcarpet' require 'redcarpet'
@ -386,16 +387,31 @@ post '/votes/:id/reopen' do
redirect '/votes/' + @vote.secure_id redirect '/votes/' + @vote.secure_id
end end
get '/votes/:id/ratings' do
redirect '/votes/' + params[:id]
end
post '/votes/:id/ratings' do post '/votes/:id/ratings' do
require_login require_login
find_vote find_vote
require_open_vote require_open_vote
@errors = []
@vote.candidates.each do |candidate| @vote.candidates.each do |candidate|
rating = Rating.find_or_initialize_by(vote: @vote, user: current_user, candidate: candidate) if not params[candidate.id.to_s]
rating.value = params[candidate.id.to_s] @errors << OpenStruct.new(:attribute => :rating, :type => :blank, :candidate => candidate)
rating.save end
end
if not @errors.empty?
@params = params
erb :votes_show_open
else
@vote.candidates.each do |candidate|
rating = Rating.find_or_initialize_by(vote: @vote, user: current_user, candidate: candidate)
rating.value = params[candidate.id.to_s]
rating.save
end
redirect '/votes/' + @vote.secure_id
end end
redirect '/votes/' + @vote.secure_id
end end
post '/votes/:id/organizers' do post '/votes/:id/organizers' do

View file

@ -1,5 +1,13 @@
<h1><%= @vote.title %></h1> <h1><%= @vote.title %></h1>
<% if @errors %>
<% @errors.each do |error| %>
<% if error.attribute == :rating and error.type == :blank %>
<p class="error"><%= _("Missing rating for candidate <i>%{name}</i>." % { name: error.candidate.name }) %></p>
<% end %>
<% end %>
<% end %>
<% if @vote.expire_on %> <% if @vote.expire_on %>
<p><%= _("Closing date: %{date}" % { date: format_date_and_time(@vote.expire_on) }) %></p> <p><%= _("Closing date: %{date}" % { date: format_date_and_time(@vote.expire_on) }) %></p>
<% end %> <% end %>
@ -12,7 +20,13 @@
<ul> <ul>
<% @vote.candidates.each do |candidate| %> <% @vote.candidates.each do |candidate| %>
<% rating = @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate } %> <% rating = @vote.ratings.find { |rating| rating.user == current_user and rating.candidate == candidate } %>
<% value = rating ? rating.value : 0 %> <% if rating
value = rating.value
elsif @params[candidate.id.to_s]
value = params[candidate.id.to_s].to_i
else
value = 0
end %>
<li> <li>
<p><b><%= candidate.name %></b></p> <p><b><%= candidate.name %></b></p>
<%= markdown(candidate.description) %> <%= markdown(candidate.description) %>