diff --git a/vedia.rb b/vedia.rb index ee3ec7b..54c3d70 100644 --- a/vedia.rb +++ b/vedia.rb @@ -11,6 +11,7 @@ require 'gettext' require 'securerandom' require 'chartkick' require 'mail' +require 'ostruct' require 'tzinfo' require 'redcarpet' @@ -386,16 +387,31 @@ post '/votes/:id/reopen' do redirect '/votes/' + @vote.secure_id end +get '/votes/:id/ratings' do + redirect '/votes/' + params[:id] +end + post '/votes/:id/ratings' do require_login find_vote require_open_vote + @errors = [] @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 + if not params[candidate.id.to_s] + @errors << OpenStruct.new(:attribute => :rating, :type => :blank, :candidate => candidate) + 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 - redirect '/votes/' + @vote.secure_id end post '/votes/:id/organizers' do diff --git a/views/votes_show_open.erb b/views/votes_show_open.erb index d5f5a8f..757afb5 100644 --- a/views/votes_show_open.erb +++ b/views/votes_show_open.erb @@ -1,5 +1,13 @@

<%= @vote.title %>

+<% if @errors %> +<% @errors.each do |error| %> + <% if error.attribute == :rating and error.type == :blank %> +

<%= _("Missing rating for candidate %{name}." % { name: error.candidate.name }) %>

+ <% end %> +<% end %> +<% end %> + <% if @vote.expire_on %>

<%= _("Closing date: %{date}" % { date: format_date_and_time(@vote.expire_on) }) %>

<% end %> @@ -12,7 +20,13 @@