forked from rafaelzlisboa/awayday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awayday.rb
116 lines (90 loc) · 2.71 KB
/
awayday.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
require 'rack-flash'
require 'sinatra/base'
require 'sinatra/redirect_with_flash'
require 'sinatra/assetpack'
require 'mongoid'
require 'haml'
Dir["./models/**/*.rb"].each { |model| require model }
Dir["./helpers/**/*.rb"].each { |helper| require helper }
class AwayDayApp < Sinatra::Base
set :root, File.dirname(__FILE__)
register Sinatra::AssetPack
enable :sessions
use Rack::Flash, :sweep => true
DEADLINE = Time.new(2013, 8, 26, 9, 0, 0, "-03:00")
assets do
serve '/css', from: '/assets/css'
serve '/images', from: '/assets/images'
serve '/fonts', from: '/assets/fonts'
css :hint, [ 'css/hint.css' ]
css :awayday, [ 'css/awayday.css' ]
css_compression :sass
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
after do
session[:params], session[:errors] = {}, {} unless status == 302
end
get '/' do
redirect "/talks", flash[:error] = "Submissions closed." if deadline_reached?
session[:params] ||= {}
haml :form, :locals => {
:durations => Talk::DURATIONS,
:categories => Talk::CATEGORIES,
:languages => Talk::LANGUAGES,
:params => session[:params]
}
end
post '/talk' do
talk = Talk.new :title => params[:title],
:summary => params[:summary],
:category => params[:category],
:duration => params[:duration],
:languages => params[:languages]
presenter = Presenter.where(:name => params[:name], :email => params[:email]).first
presenter ||= Presenter.new :name => params[:name], :email => params[:email]
presenter.talks << talk
if talk.valid? & presenter.valid?
presenter.save
redirect "/", flash[:notice] = success_message_for(presenter)
else
session[:errors] = errors_in(presenter, talk)
session[:params] = copy_of params
redirect "/", flash[:error] = error_message
end
end
get '/talks' do
haml :talks, :locals => {:talks => Talk.all}
end
get '/talks.csv' do
csv = CsvHelper.csv_from Talk.all
attachment("talks_" + Time.now.strftime("%m-%d-%Y") + ".csv")
response.headers['Content-Type'] = "text/csv;charset=utf-8"
response.write(csv)
end
get '/faq' do
haml :faq
end
private
def success_message_for(presenter)
"Congratulations #{presenter.name}. Your proposal was sent."
end
def error_message
"Ooops. Something went wrong. Take a look at the following list:"
end
def errors_in(presenter, talk)
presenter.errors.messages.merge talk.errors.messages
end
def copy_of(params)
params_copy = {}
params.each_pair do |key, value|
params_copy[key.to_sym] = value
end
params_copy
end
def deadline_reached?
Time.new.localtime("-03:00") > DEADLINE
end
end