-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.rb
136 lines (115 loc) · 3.27 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'rubygems'
require 'bundler/setup'
require 'json'
Bundler.require :default
require 'sinatra/asset_pipeline'
require_relative 'config/base'
$stdout.sync = true
require 'active_record'
require_relative 'lib/flowdock/create_poll'
require_relative 'lib/flowdock/close_poll'
require_relative 'lib/flowdock/comment_poll'
require_relative 'lib/flowdock/vote'
require_relative 'lib/flowdock/routes'
require_relative 'lib/flowdock/new_option'
require_relative 'lib/flowdock/unvote'
require_relative 'models/poll'
require_relative 'models/option'
require_relative 'models/vote'
require_relative 'models/comment'
require_relative 'models/user'
require 'securerandom'
require 'rack/csrf'
register Flowdock::Routes
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use Rack::Csrf
@environment = ENV['RACK_ENV'] || "development"
@dbconfig = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)
ActiveRecord::Base.establish_connection @dbconfig[@environment]
require_relative 'schema'
def current_user
user = User.find_by(session_token: session[:token])
if !user
user = User.create!(
session_token: SecureRandom.hex,
name: "Anonymous",
email: "[email protected]",
nick: "Anonymous"
)
session[:token] = user.session_token
end
user
end
before do
current_user
end
get '/' do
@integration_set = session.has_key?(:integration_token)
@open_polls = Poll.where(status: "open")
@closed_polls = Poll.where(status: "closed")
slim :index
end
get '/create' do
slim :create
end
post '/create' do
poll = Poll.create!(
title: params[:title].strip(),
status: "open"
)
options = params[:options].split(",")
for option in options
poll.options.push Option.create!(poll: poll, title: option.strip())
end
Flowdock::CreatePoll.new(poll, current_user).save()
redirect to("/")
end
post '/:poll_id/vote' do
poll = Poll.find(params[:poll_id])
option = poll.options.find(params[:option])
vote = Vote.create!(option: option, user: current_user)
Flowdock::Vote.new(vote, current_user).save()
if params[:redirect]
redirect to("/")
else
redirect to("/" + params[:poll_id])
end
end
delete '/:poll_id/unvote' do
poll = Poll.find(params[:poll_id])
option = poll.options.find(params[:option])
vote = Vote.find_by(option: option, user: current_user)
vote.destroy!
Flowdock::UnVote.new(vote, current_user).save()
if params[:redirect]
redirect to("/")
else
redirect to("/" + params[:poll_id])
end
end
post '/:poll_id/close' do
poll = Poll.find(params[:poll_id])
poll.update!(status: "closed")
Flowdock::ClosePoll.new(poll, current_user).save()
redirect to("/" + params[:poll_id])
end
post '/:poll_id/comment' do
poll = Poll.find(params[:poll_id])
comment = Comment.create!(poll: poll, comment: params[:comment].strip())
Flowdock::CommentPoll.new(comment, current_user).save()
redirect to("/" + params[:poll_id])
end
post '/:poll_id/add_option' do
poll = Poll.find(params[:poll_id])
option = Option.create!(poll: poll, title: params[:title].strip())
Flowdock::NewOption.new(option, current_user).save()
redirect to("/" + params[:poll_id])
end
get '/:poll_id' do
begin
@poll = Poll.find(params[:poll_id])
rescue ActiveRecord::RecordNotFound
halt(404)
end
slim :poll
end