-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.rb
executable file
·243 lines (187 loc) · 6.89 KB
/
application.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# encoding: UTF-8 (magic comment)
Encoding.default_external = Encoding::UTF_8
# Encoding.default_internal = Encoding::UTF_8
require 'set' # to use Set data structure
require 'digest' # to hash passwords
require 'yaml'
require 'cgi' # "Common Gateway Interface", HTTP tools (escape query string, for example)
require 'csv' # to generate CSV files for download
require 'haml'
require 'sass'
require 'redcarpet' # Markdown
# XXX: Because of some bug, 'active_record' needs to be required before 'pony'.
require 'active_record'
require 'pony' # Email
require 'reverse_markdown' # HTML to markdown for text email body
require 'sinatra/base'
if Sinatra::Base.development?
require 'debugger' # Ruby debugger
require 'better_errors' # better error pages
end
require 'sinatra/flash' # Session-based flash
require 'i18n' # Internationalisation
require 'active_record'
require_relative 'init'
module CTT2013
require_relative 'base_url'
# For internationalisation
LOCALES = [:en, :fr]
DEFAULT_LOCALE = :fr
# Create Sinatra web application
Application = Class.new(Sinatra::Base)
# Constants and private methods
class Application
# Pages
# -----
COMMON_HOME_PAGE = 'common/index.php'
COMB_PAGE_PREFIX = 'ldtg-mb/'
PUBLIC_PAGES =
[ 'index',
'program',
'scientific_committee',
'organising_committee',
'directions_to_get_here',
'funding',
'contacts',
'accommodation',
'participants',
'registration', # only displays that registration is closed
'useful_links'
].map{|p| "#{ COMB_PAGE_PREFIX }#{ p }" }
STATIC_PUBLIC_PAGES =
Set[ 'index',
'program',
'scientific_committee',
'organising_committee',
'directions_to_get_here',
'funding',
'contacts',
'registration',
'useful_links'
].map{|p| "#{ COMB_PAGE_PREFIX }#{ p }" }
COMB_HOME_PAGE = PUBLIC_PAGES[0]
PAGE_URL_FRAGMENTS = PUBLIC_PAGES.reduce({}){|h, p| h[p] = [p.to_s]; h }
PAGE_URL_FRAGMENTS[COMB_HOME_PAGE] << COMB_PAGE_PREFIX
# Model attributes
# ----------------
PARTICIPANT_ATTRIBUTE_NAMES_FOR = {}
PARTICIPANT_ATTRIBUTE_NAMES_FOR[:registration] =
Set[ :first_name, :last_name, :email,
:affiliation, :academic_position,
:country, :city, :post_code, :street_address, :phone,
:web_site,
:i_m_t_member, :g_d_r_member,
:invitation_needed, :visa_needed,
# :funding_requests,
:special_requests ]
PARTICIPANT_ATTRIBUTE_NAMES_FOR[:create] =
Set[ :first_name, :last_name, :email, :affiliation,
:academic_position,
:country, :city, :post_code, :street_address, :phone,
:web_site,
:i_m_t_member, :g_d_r_member,
:invitation_needed, :visa_needed,
:funding_requests,
:special_requests ]
PARTICIPANT_ATTRIBUTE_NAMES_FOR[:update] =
PARTICIPANT_ATTRIBUTE_NAMES_FOR[:create]
TALK_ATTRIBUTE_NAMES_FOR = {}
TALK_ATTRIBUTE_NAMES_FOR[:create] =
Set[ :type, :participant_id, :title, :abstract,
:equipment,
:date, :time, :room_or_auditorium ]
TALK_ATTRIBUTE_NAMES_FOR[:update] =
TALK_ATTRIBUTE_NAMES_FOR[:create]
HOTEL_ATTRIBUTE_NAMES_FOR = {}
HOTEL_ATTRIBUTE_NAMES_FOR[:create] =
Set[:name, :address, :phone, :web_site]
HOTEL_ATTRIBUTE_NAMES_FOR[:update] = HOTEL_ATTRIBUTE_NAMES_FOR[:create]
PARTICIPANT_ACCOMMODATION_ATTRIBUTE_NAMES_FOR_CREATE =
Set[ :hotel_id, :arrival_date, :departure_date ]
PARTICIPANT_ACCOMMODATION_ATTRIBUTE_NAMES_FOR_UPDATE =
Set[ :arrival_date, :departure_date ]
# Internationalisation
# --------------------
LOCALE_FROM_URL_LOCALE_FRAGMENT = CTT2013::LOCALES.reduce({}) { |h, locale|
h["#{ locale }/"] = locale
h
}
# XXX: The assignment of the `DEFAULT_LOCALE` to `''` is not
# completely consistent with the case of explicit locales.
LOCALE_FROM_URL_LOCALE_FRAGMENT[''] = CTT2013::DEFAULT_LOCALE
private
def set_locale(locale)
I18n.locale = @locale = locale
@other_locales = CTT2013::LOCALES - [@locale]
end
def locale
@locale || CTT2013::DEFAULT_LOCALE
end
def set_page(page)
@page = page
@base_title = t('base_co_m_b_page_title')
@title =
"#{ @base_title } | #{ t(:title, :scope => page_i18n_scope(@page)) }"
end
def page
@page
end
# def locale_from_user_input(suggested_locale)
# suggested_locale = suggested_locale.to_s.downcase
# CTT2013::LOCALES.find{|l| l.to_s == suggested_locale } || CTT2013::DEFAULT_LOCALE
# end
# def page_from_user_input(suggested_page)
# suggested_page = suggested_page.to_s.downcase
# PUBLIC_PAGES.find{|p| p.to_s == suggested_page } || COMB_HOME_PAGE
# end
end
require_relative 'lib/for_sass'
require_relative 'lib/simple_relation_filter'
require_relative 'models/all'
require_relative 'helpers/all'
Application.helpers(*Helpers::ALL)
require_relative 'route_handlers/all'
# Settings
# ========
Application.instance_eval do
# NOTE: it seems common to put settings inside a block passed to
# `configure` method, but apparently it is useless
set :app_file, __FILE__
set :root, File.dirname(settings.app_file)
set :views => File.join(settings.root, 'view_templates'),
:public_folder => File.join(settings.root, 'public_folder')
enable :method_override # enable "_method" hack for POST requests
# Enable/disable cookie based sessions
# enable for flash messages in registration form and authentication
set :sessions, :path => BASE_URL
# set :bind, 'localhost' # server hostname or IP address
# set :port, 4567 # server port
# set :lock, true # ensure single request concurrency with a mutex lock
# Unfortunately this does not work:
# set :markdown, :tables => true
# This seems to be needed to automatically close connections at the end of
# each request. Not sure if and how this works. This does not work when
# the applicaiton is run from the command line with "Thin" web server.
# For that case, connections need to be closed explicitely in an "after"
# filter.
use ActiveRecord::ConnectionAdapters::ConnectionManagement
if development?
use BetterErrors::Middleware
BetterErrors.application_root = settings.root
end
if production?
# Do not "pretty print" HTML for better performance
set :haml, { :ugly => true }
end
# Internationalisation
# --------------------
I18n.load_path =
Dir[::File.join(settings.root, 'internationalisation/**/*.{rb,yml}')]
I18n.default_locale = DEFAULT_LOCALE
# Sessions
# --------
# Session-based flash
register Sinatra::Flash
end
end
CTT2013::Application.run! if __FILE__ == $0