Factorize find_vote

This commit is contained in:
ricola 2025-04-06 17:04:31 -06:00
parent c042f0c3a5
commit dc702d0723

26
vote.rb
View file

@ -124,7 +124,7 @@ end
get '/votes/:id' do get '/votes/:id' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote(params)
case @vote.state case @vote.state
when 'draft' when 'draft'
if @vote.users.exists?(current_user.id) if @vote.users.exists?(current_user.id)
@ -145,7 +145,7 @@ end
post '/votes/:id/edit' do post '/votes/:id/edit' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.state == 'draft' and @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.state == 'draft' and @vote.users.exists?(current_user.id)
@vote.title = params[:title] @vote.title = params[:title]
@vote.description = params[:description] @vote.description = params[:description]
@ -155,7 +155,7 @@ end
post '/votes/:id/candidates' do post '/votes/:id/candidates' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.state == 'draft' and @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.state == 'draft' and @vote.users.exists?(current_user.id)
@candidate = Candidate.new(name: params[:name], @candidate = Candidate.new(name: params[:name],
description: params[:description]) description: params[:description])
@ -166,7 +166,7 @@ end
post '/votes/:id/candidates/:cid/delete' do post '/votes/:id/candidates/:cid/delete' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.state == 'draft' and @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.state == 'draft' and @vote.users.exists?(current_user.id)
@candidate = Candidate.find(params[:cid]) @candidate = Candidate.find(params[:cid])
@candidate.destroy @candidate.destroy
@ -175,7 +175,7 @@ end
post '/votes/:id/open' do post '/votes/:id/open' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.state == 'draft' and @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.state == 'draft' and @vote.users.exists?(current_user.id)
@vote.state = 'open' @vote.state = 'open'
@vote.save @vote.save
@ -184,7 +184,7 @@ end
post '/votes/:id/draft' do post '/votes/:id/draft' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.state == 'open' and @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.state == 'open' and @vote.users.exists?(current_user.id)
@vote.ratings.each {|r| r.destroy} @vote.ratings.each {|r| r.destroy}
@vote.state = 'draft' @vote.state = 'draft'
@ -194,7 +194,7 @@ end
post '/votes/:id/close' do post '/votes/:id/close' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.state == 'open' and @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.state == 'open' and @vote.users.exists?(current_user.id)
@vote.state = 'closed' @vote.state = 'closed'
@vote.save @vote.save
@ -203,7 +203,7 @@ end
post '/votes/:id/reopen' do post '/votes/:id/reopen' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.state == 'closed' and @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.state == 'closed' and @vote.users.exists?(current_user.id)
@vote.state = 'open' @vote.state = 'open'
@vote.save @vote.save
@ -212,7 +212,7 @@ end
post '/votes/:id/ratings' do post '/votes/:id/ratings' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
@vote.candidates.each do |candidate| @vote.candidates.each do |candidate|
rating = Rating.find_or_initialize_by(vote: @vote, 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.value = params[candidate.id.to_s]
@ -223,7 +223,7 @@ end
post '/votes/:id/organizers' do post '/votes/:id/organizers' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.users.exists?(current_user.id)
user = User.find_by(email: params[:email]) user = User.find_by(email: params[:email])
@vote.users << user @vote.users << user
@ -232,7 +232,7 @@ end
post '/votes/:id/delete' do post '/votes/:id/delete' do
require_login require_login
@vote = Vote.find_by(secure_id: params[:id]) find_vote
redirect '/votes/' + @vote.secure_id unless @vote.users.exists?(current_user.id) redirect '/votes/' + @vote.secure_id unless @vote.users.exists?(current_user.id)
@vote.destroy @vote.destroy
redirect '/' redirect '/'
@ -250,4 +250,8 @@ helpers do
def require_login def require_login
redirect '/login' unless current_user redirect '/login' unless current_user
end end
def find_vote(params)
@vote = Vote.find_by(secure_id: params[:id])
end
end end