-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlunchvoteomatic.rb
109 lines (89 loc) · 1.83 KB
/
lunchvoteomatic.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'rubygems' if RUBY_VERSION < "1.9"
require 'sinatra'
require 'erb'
require 'active_record'
environment = ENV['RACK_ENV']
dbconfig = YAML.load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection dbconfig[environment]
class Vote < ActiveRecord::Base
has_many :questions
def total_votes
total = 0
self.questions.each do |q|
total += q.count
end
total
end
end
class Question < ActiveRecord::Base
belongs_to :vote
def total_votes
0
end
def vote_address
"/do_vote/#{self.vote.id}/#{self.id}"
end
end
class LunchVoteOMatic < Sinatra::Base
set :public, File.dirname(__FILE__) + '/public'
get '/favicon.ico' do
end
get '/' do
@count = 10
erb :new
end
post '/' do
@vote = Vote.new
@vote.name = params[:name]
if @vote.save
questions = params[:question]
questions.each do |number, text|
new_question = Question.new
new_question.text = text
new_question.count = 0
new_question.vote_id = @vote.id
new_question.save unless text == ""
end
redirect "/#{@vote.id}"
else
redirect '/'
end
end
get '/:id' do
begin
@vote = Vote.find(params[:id])
if @vote.nil?
redirect '/'
else
@vote.questions.sort! {|x,y| x.text <=> y.text }
erb :show
end
rescue
redirect '/'
end
end
get '/:id/result' do
begin
@vote = Vote.find(params[:id])
if @vote.nil?
redirect '/'
else
@vote.questions.sort! {|x,y| x.text <=> y.text }
erb :index
end
rescue
redirect '/'
end
end
get '/do_vote/:vote_id/:question_id' do
question = Question.find(params[:question_id])
if question.nil?
redirect "/#{params[:vote_id]}"
else
question.count = question.count.to_i + 1
question.save
redirect "/#{params[:vote_id]}/result"
#"Votes: #{question.count.to_s}"
end
end
end