144 lines
3 KiB
Ruby
144 lines
3 KiB
Ruby
require 'bundler/setup'
|
|
require 'sinatra'
|
|
require 'sinatra/activerecord'
|
|
require 'bcrypt'
|
|
require 'securerandom'
|
|
require_relative 'mj'
|
|
|
|
class Vote < ActiveRecord::Base
|
|
has_many :candidates
|
|
has_many :ratings
|
|
has_many :organizers
|
|
has_many :users, through: :organizers
|
|
end
|
|
|
|
class Candidate < ActiveRecord::Base
|
|
belongs_to :vote
|
|
has_many :ratings
|
|
|
|
def mj
|
|
return MajorityJudgment.new(self.ratings.collect {|r| r.value })
|
|
end
|
|
|
|
end
|
|
|
|
class User < ActiveRecord::Base
|
|
has_many :ratings
|
|
has_many :organizers
|
|
has_many :votes, through: :organizers
|
|
end
|
|
|
|
class Organizer < ActiveRecord::Base
|
|
belongs_to :vote
|
|
belongs_to :user
|
|
validates :vote_id, uniqueness: { scope: :user_id }
|
|
validates :user_id, uniqueness: { scope: :vote_id }
|
|
end
|
|
|
|
class Rating < ActiveRecord::Base
|
|
belongs_to :vote
|
|
belongs_to :user
|
|
belongs_to :candidate
|
|
end
|
|
|
|
def hash_password(password)
|
|
BCrypt::Password.create(password).to_s
|
|
end
|
|
|
|
def verify_password(password, hash)
|
|
BCrypt::Password.new(hash) == password
|
|
end
|
|
|
|
enable :sessions
|
|
|
|
get '/' do
|
|
redirect '/login' unless current_user
|
|
@votes = Vote.all
|
|
erb :home
|
|
end
|
|
|
|
get '/signup' do
|
|
erb :signup
|
|
end
|
|
|
|
post '/signup' do
|
|
@user = User.create(email: params[:email],
|
|
password: hash_password(params[:password]))
|
|
redirect '/'
|
|
end
|
|
|
|
get '/login' do
|
|
erb :login
|
|
end
|
|
|
|
post '/login' do
|
|
user = User.find_by(email: params[:email])
|
|
if user && verify_password(params[:password], user.password)
|
|
session.clear
|
|
session[:user_id] = user.id
|
|
redirect '/'
|
|
else
|
|
@error = 'Username or password was incorrect'
|
|
erb :login
|
|
end
|
|
end
|
|
|
|
post '/logout' do
|
|
session.clear
|
|
redirect '/login'
|
|
end
|
|
|
|
get '/votes/new' do
|
|
erb :votes_new
|
|
end
|
|
|
|
get '/votes/:id' do
|
|
redirect '/login' unless current_user
|
|
@vote = Vote.find_by(secure_id: params[:id])
|
|
erb :votes_show
|
|
end
|
|
|
|
post '/votes' do
|
|
@vote = Vote.create(secure_id: SecureRandom.hex(8),
|
|
title: params[:title],
|
|
description: params[:description])
|
|
redirect '/votes/' + @vote.secure_id
|
|
end
|
|
|
|
post '/votes/:id/candidates' do
|
|
@vote = Vote.find_by(secure_id: params[:id])
|
|
@candidate = Candidate.new(name: params[:name],
|
|
description: params[:description])
|
|
@candidate.vote = @vote
|
|
@candidate.save
|
|
redirect '/votes/' + @vote.secure_id
|
|
end
|
|
|
|
post '/votes/:id/ratings' do
|
|
redirect '/login' unless current_user
|
|
vote = Vote.find_by(secure_id: params[:id])
|
|
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
|
|
|
|
post '/votes/:id/organizers' do
|
|
redirect '/login' unless current_user
|
|
vote = Vote.find_by(secure_id: params[:id])
|
|
user = User.find_by(email: params[:email])
|
|
vote.users << user
|
|
redirect '/votes/' + vote.secure_id
|
|
end
|
|
|
|
helpers do
|
|
def current_user
|
|
if session[:user_id]
|
|
User.find(session[:user_id])
|
|
else
|
|
nil
|
|
end
|
|
end
|
|
end
|