forked from chastell/suffragist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuffragist.rb
76 lines (67 loc) · 1.65 KB
/
suffragist.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
require 'sinatra'
require 'yaml/store'
Choices = {
'krk' => 'Cracow',
'rad' => 'Radom',
'waw' => 'Warsaw',
'wro' => 'Wrocław',
}
get '/' do
@title = 'Welcome to the Suffragist!'
erb :index
end
post '/cast' do
@title = 'Thanks for casting your vote!'
@vote = params['vote']
@store = YAML::Store.new 'votes.yml'
@store.transaction do
@store['votes'] ||= {}
@store['votes'][@vote] ||= 0
@store['votes'][@vote] += 1
end
erb :cast
end
get '/results' do
@title = 'Results so far:'
@store = YAML::Store.new 'votes.yml'
@votes = @store.transaction { @store['votes'] }
erb :results
end
__END__
@@ layout
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8' />
<title>Suffragist</title>
<link href='//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css' rel='stylesheet' />
</head>
<body class='container'>
<h1><%= @title %></h1>
<%= yield %>
</body>
</html>
@@ index
<p>Cast your vote:</p>
<form action='cast' method='post'>
<ul class='unstyled'>
<% Choices.each do |id, text| %>
<li><label class='radio'><input type='radio' name='vote' value='<%= id %>' id='vote_<%= id %>' /> <%= text %></label></li>
<% end %>
</ul>
<button type='submit' class='btn btn-primary'>Cast this vote!</button>
</form>
@@ cast
<p>You cast: <%= Choices[@vote] %></p>
<p><a href='results'>See the results!</a></p>
@@ results
<table class='table table-hover table-striped'>
<% Choices.each do |id, text| %>
<tr>
<th><%= text %></th>
<td><%= @votes[id] || 0 %>
<td><%= '#' * (@votes[id] || 0) %></td>
</tr>
<% end %>
</table>
<p><a href='/'>Cast more votes!</a></p>