Factorize find_vote
This commit is contained in:
parent
c042f0c3a5
commit
dc702d0723
1 changed files with 15 additions and 11 deletions
26
vote.rb
26
vote.rb
|
|
@ -124,7 +124,7 @@ end
|
|||
|
||||
get '/votes/:id' do
|
||||
require_login
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
find_vote(params)
|
||||
case @vote.state
|
||||
when 'draft'
|
||||
if @vote.users.exists?(current_user.id)
|
||||
|
|
@ -145,7 +145,7 @@ end
|
|||
|
||||
post '/votes/:id/edit' do
|
||||
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)
|
||||
@vote.title = params[:title]
|
||||
@vote.description = params[:description]
|
||||
|
|
@ -155,7 +155,7 @@ end
|
|||
|
||||
post '/votes/:id/candidates' do
|
||||
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)
|
||||
@candidate = Candidate.new(name: params[:name],
|
||||
description: params[:description])
|
||||
|
|
@ -166,7 +166,7 @@ end
|
|||
|
||||
post '/votes/:id/candidates/:cid/delete' do
|
||||
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)
|
||||
@candidate = Candidate.find(params[:cid])
|
||||
@candidate.destroy
|
||||
|
|
@ -175,7 +175,7 @@ end
|
|||
|
||||
post '/votes/:id/open' do
|
||||
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)
|
||||
@vote.state = 'open'
|
||||
@vote.save
|
||||
|
|
@ -184,7 +184,7 @@ end
|
|||
|
||||
post '/votes/:id/draft' do
|
||||
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)
|
||||
@vote.ratings.each {|r| r.destroy}
|
||||
@vote.state = 'draft'
|
||||
|
|
@ -194,7 +194,7 @@ end
|
|||
|
||||
post '/votes/:id/close' do
|
||||
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)
|
||||
@vote.state = 'closed'
|
||||
@vote.save
|
||||
|
|
@ -203,7 +203,7 @@ end
|
|||
|
||||
post '/votes/:id/reopen' do
|
||||
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)
|
||||
@vote.state = 'open'
|
||||
@vote.save
|
||||
|
|
@ -212,7 +212,7 @@ end
|
|||
|
||||
post '/votes/:id/ratings' do
|
||||
require_login
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
find_vote
|
||||
@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]
|
||||
|
|
@ -223,7 +223,7 @@ end
|
|||
|
||||
post '/votes/:id/organizers' do
|
||||
require_login
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
find_vote
|
||||
redirect '/votes/' + @vote.secure_id unless @vote.users.exists?(current_user.id)
|
||||
user = User.find_by(email: params[:email])
|
||||
@vote.users << user
|
||||
|
|
@ -232,7 +232,7 @@ end
|
|||
|
||||
post '/votes/:id/delete' do
|
||||
require_login
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
find_vote
|
||||
redirect '/votes/' + @vote.secure_id unless @vote.users.exists?(current_user.id)
|
||||
@vote.destroy
|
||||
redirect '/'
|
||||
|
|
@ -250,4 +250,8 @@ helpers do
|
|||
def require_login
|
||||
redirect '/login' unless current_user
|
||||
end
|
||||
|
||||
def find_vote(params)
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue