forked from engineyard/onefilerailsapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ru
76 lines (65 loc) · 2.38 KB
/
config.ru
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 'action_controller/railtie'
SURVEY_CHOICES = ["Netflix", "Gaming", "Dining", "NightClub"]
class SurveyApp < Rails::Application
config.eager_load = true # necessary to silence warning
config.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
config.secret_key_base = SecureRandom.uuid # Rails won't start without this
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' }
routes.append { root :to => "survey#index" }
routes.append { post "/survey" => "survey#index" }
end
class SurveyController < ActionController::Base
@@responses = {}
def index
choice = params["activity_choice"]
if choice
count = @@responses[choice]
count = 0 unless not count.nil?
@@responses[choice] = count + 1
render html: results_html(choice).html_safe
else
render html: form_html.html_safe
end
end
def form_html
form_start = <<-FORM_START_HTML
<h3>What is your favorite weekend activity?</h3>
<form method="post" action="survey">
FORM_START_HTML
form_end = <<-FORM_END_HTML
<br/><br/><div class="12u$">
<input type="submit" value="Submit" style="background-color: #5AA6ED; color: #ffffff !important; font-family: Taviraj, serif; font-size: 18; height: 26; padding: 0; border-radius: 5px; width: 100; vertical-align: middle;" />
</div>
</form>
FORM_END_HTML
buffer = form_start
SURVEY_CHOICES.each do |choice|
buffer = "#{buffer}#{button_html(choice)}"
end
"#{buffer}#{form_end}"
end
def button_html(choice)
<<-CHOICE_HTML
<div class="4u 12u$(small)">
<input type="radio" id="#{choice}" name="activity_choice" value="#{choice}" style="font-family: Taviraj, serif;">
<label for="netflix">#{choice}</label>
</div>
CHOICE_HTML
end
def results_html(answer)
header = <<-RESULTS_HTML
<h3>What is your favorite weekend activity?</h3><div style="color: #A9A9A9">
Thanks for participating? You chose <span style="color: #8FC0F1">#{answer}</span>. Overall results:<br/></div><hr/>
<div class="container horizontal rounded">
RESULTS_HTML
buffer = ""
SURVEY_CHOICES.each do |choice|
count = @@responses[choice]
count = 0 unless not count.nil?
buffer = "#{buffer}<pre>#{choice.rjust(10, ' ')}: #{count}</pre>"
end
"#{header}#{buffer}</div>"
end
end
SurveyApp.initialize!
run SurveyApp