padrino-core 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/lib/padrino-core.rb +4 -4
  3. data/lib/padrino-core/application.rb +6 -195
  4. data/lib/padrino-core/application/application_setup.rb +199 -0
  5. data/lib/padrino-core/application/routing.rb +66 -25
  6. data/lib/padrino-core/cli/base.rb +8 -5
  7. data/lib/padrino-core/cli/rake.rb +12 -9
  8. data/lib/padrino-core/loader.rb +1 -1
  9. data/lib/padrino-core/logger.rb +25 -0
  10. data/lib/padrino-core/mounter.rb +8 -3
  11. data/lib/padrino-core/reloader.rb +2 -2
  12. data/lib/padrino-core/server.rb +50 -17
  13. data/lib/padrino-core/version.rb +1 -1
  14. data/padrino-core.gemspec +2 -10
  15. data/test/fixtures/apps/demo_app.rb +7 -0
  16. data/test/fixtures/apps/demo_demo.rb +7 -0
  17. data/test/helper.rb +6 -37
  18. data/test/test_application.rb +12 -13
  19. data/test/test_core.rb +12 -13
  20. data/test/test_csrf_protection.rb +49 -23
  21. data/test/test_dependencies.rb +7 -7
  22. data/test/test_filters.rb +41 -17
  23. data/test/test_flash.rb +24 -24
  24. data/test/test_locale.rb +1 -1
  25. data/test/test_logger.rb +39 -27
  26. data/test/test_mounter.rb +34 -20
  27. data/test/test_reloader_complex.rb +5 -6
  28. data/test/test_reloader_simple.rb +23 -20
  29. data/test/test_reloader_system.rb +10 -7
  30. data/test/test_restful_routing.rb +1 -1
  31. data/test/test_router.rb +7 -7
  32. data/test/test_routing.rb +177 -141
  33. metadata +14 -53
  34. data/lib/padrino-core/application/rendering.rb +0 -325
  35. data/lib/padrino-core/application/rendering/extensions/erubis.rb +0 -68
  36. data/lib/padrino-core/application/rendering/extensions/haml.rb +0 -29
  37. data/lib/padrino-core/application/rendering/extensions/slim.rb +0 -21
  38. data/lib/padrino-core/locale/cs.yml +0 -33
  39. data/lib/padrino-core/locale/da.yml +0 -33
  40. data/lib/padrino-core/locale/de.yml +0 -33
  41. data/lib/padrino-core/locale/en.yml +0 -33
  42. data/lib/padrino-core/locale/es.yml +0 -33
  43. data/lib/padrino-core/locale/fr.yml +0 -33
  44. data/lib/padrino-core/locale/hu.yml +0 -33
  45. data/lib/padrino-core/locale/it.yml +0 -39
  46. data/lib/padrino-core/locale/ja.yml +0 -33
  47. data/lib/padrino-core/locale/lv.yml +0 -33
  48. data/lib/padrino-core/locale/nl.yml +0 -33
  49. data/lib/padrino-core/locale/no.yml +0 -33
  50. data/lib/padrino-core/locale/pl.yml +0 -33
  51. data/lib/padrino-core/locale/pt_br.yml +0 -39
  52. data/lib/padrino-core/locale/ro.yml +0 -33
  53. data/lib/padrino-core/locale/ru.yml +0 -34
  54. data/lib/padrino-core/locale/sv.yml +0 -33
  55. data/lib/padrino-core/locale/tr.yml +0 -33
  56. data/lib/padrino-core/locale/uk.yml +0 -33
  57. data/lib/padrino-core/locale/zh_cn.yml +0 -33
  58. data/lib/padrino-core/locale/zh_tw.yml +0 -33
  59. data/lib/padrino-core/support_lite.rb +0 -259
  60. data/test/fixtures/apps/.components +0 -6
  61. data/test/fixtures/apps/.gitignore +0 -7
  62. data/test/fixtures/apps/render.rb +0 -13
  63. data/test/fixtures/apps/views/blog/post.erb +0 -1
  64. data/test/fixtures/layouts/layout.erb +0 -1
  65. data/test/mini_shoulda.rb +0 -45
  66. data/test/test_rendering.rb +0 -606
  67. data/test/test_rendering_extensions.rb +0 -14
  68. data/test/test_support_lite.rb +0 -56
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef4204b99783ae0f813af61c5ae43a5edb5b9c99
4
- data.tar.gz: 9752a0075e390a6fd24c7b56f60b76f1719d3bf1
3
+ metadata.gz: f503cfe1974ffddc13bc280ad2abfbe7a47f397b
4
+ data.tar.gz: 9d7b43288edeb327d60848ac5a7f539e05f5ec0b
5
5
  SHA512:
6
- metadata.gz: 8f716e4b5e8f068047a87ae18d19dbf2abcdf0ff4bcce8c538a4e05dd7933ac0c3374e80174340d74f80f9d4862da4de097d8d328e4d703e24281dde03aac722
7
- data.tar.gz: 3917aed536dedd954528f03e4e650830bc721235b68c8ae07fccb546bef08d7c77a382c95d35ad05a868181d0babf6486ee79474415f53d71c12e5c74bfe7bea
6
+ metadata.gz: 5503c652282579166fbafe304e349f97b8956555eaabb7edfae2952daaec2e18c5656f742055fb98a5e3386f3e0d47183ef00ea294a8eafe5a9c1cdc4ea6b14f
7
+ data.tar.gz: a250c8dc4dc33f17a6f5c3072f918733b9de07005753c1a8efc55662a6f9ba2db1d2c31a53c988283ac3c45f098a25ffdbcf2b5881006ddd20fff493c58f18a9
data/lib/padrino-core.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'sinatra/base'
2
2
  require 'padrino-core/version'
3
- require 'padrino-core/support_lite'
3
+ require 'padrino-support'
4
4
  require 'padrino-core/application'
5
5
 
6
6
  require 'padrino-core/caller'
@@ -14,9 +14,9 @@ require 'padrino-core/server'
14
14
  require 'padrino-core/tasks'
15
15
  require 'padrino-core/module'
16
16
 
17
- if ENV["PADRINO_ENV"]
17
+ if ENV["PADRINO_ENV"] || defined?(PADRINO_ENV)
18
18
  warn 'Environment variable PADRINO_ENV is deprecated. Please, use RACK_ENV.'
19
- ENV["RACK_ENV"] ||= ENV["PADRINO_ENV"]
19
+ ENV["RACK_ENV"] ||= ENV["PADRINO_ENV"] ||= PADRINO_ENV
20
20
  end
21
21
  RACK_ENV = ENV["RACK_ENV"] ||= "development" unless defined?(RACK_ENV)
22
22
  PADRINO_ROOT = ENV["PADRINO_ROOT"] ||= File.dirname(Padrino.first_caller) unless defined?(PADRINO_ROOT)
@@ -66,7 +66,7 @@ module Padrino
66
66
  # No applications were mounted.
67
67
  #
68
68
  def application
69
- raise ApplicationLoadError, "At least one app must be mounted!" unless Padrino.mounted_apps.present?
69
+ warn 'WARNING! No apps are mounted. Please, mount apps in `config/apps.rb`' unless Padrino.mounted_apps.present?
70
70
  router = Padrino::Router.new
71
71
  Padrino.mounted_apps.each { |app| app.map_onto(router) }
72
72
  middleware.present? ? add_middleware(router) : router
@@ -1,8 +1,8 @@
1
1
  require 'padrino-core/application/flash'
2
- require 'padrino-core/application/rendering'
3
2
  require 'padrino-core/application/routing'
4
3
  require 'padrino-core/application/show_exceptions'
5
4
  require 'padrino-core/application/authenticity_token'
5
+ require 'padrino-core/application/application_setup'
6
6
 
7
7
  module Padrino
8
8
  ##
@@ -12,7 +12,7 @@ module Padrino
12
12
  # Padrino applications as well.
13
13
  #
14
14
  class Application < Sinatra::Base
15
- # Support for advanced routing, controllers, url_for
15
+ register Padrino::ApplicationSetup
16
16
  register Padrino::Routing
17
17
 
18
18
  ##
@@ -61,9 +61,9 @@ module Padrino
61
61
  reset_router!
62
62
  Padrino.require_dependencies(settings.app_file, :force => true)
63
63
  require_dependencies
64
- default_filters!
65
- default_routes!
66
- default_errors!
64
+ default_filters
65
+ default_routes
66
+ default_errors
67
67
  I18n.reload! if defined?(I18n)
68
68
  true
69
69
  end
@@ -78,7 +78,7 @@ module Padrino
78
78
  #
79
79
  def reset_routes!
80
80
  reset_router!
81
- default_routes!
81
+ default_routes
82
82
  true
83
83
  end
84
84
 
@@ -112,26 +112,6 @@ module Padrino
112
112
  view_path("layouts/#{layout}")
113
113
  end
114
114
 
115
- ##
116
- # Setup the application by registering initializers, load paths and logger.
117
- # Invoked automatically when an application is first instantiated.
118
- #
119
- # @return [TrueClass]
120
- #
121
- def setup_application!
122
- return if @_configured
123
- settings.require_dependencies
124
- settings.default_filters!
125
- settings.default_routes!
126
- settings.default_errors!
127
- if defined?(I18n)
128
- Reloader.special_files += settings.locale_path
129
- I18n.load_path << settings.locale_path
130
- I18n.reload!
131
- end
132
- @_configured = true
133
- end
134
-
135
115
  ##
136
116
  # Run the Padrino app as a self-hosted server using
137
117
  # Thin, Mongrel or WEBrick (in that order).
@@ -207,101 +187,6 @@ module Padrino
207
187
 
208
188
  protected
209
189
 
210
- ##
211
- # Defines default settings for Padrino application.
212
- #
213
- def default_configuration!
214
- set :app_file, File.expand_path(caller_files.first || $0)
215
- set :app_name, settings.to_s.underscore.to_sym
216
-
217
- set :environment, Padrino.env
218
- set :reload, Proc.new { development? }
219
- set :logging, Proc.new { development? }
220
-
221
- set :method_override, true
222
- set :default_builder, 'StandardFormBuilder'
223
-
224
- # TODO: Remove this hack after getting rid of thread-unsafe http_router:
225
- set :init_mutex, Mutex.new
226
-
227
- # TODO: Remove this line after sinatra version up.
228
- set :add_charset, %w[javascript xml xhtml+xml].map {|t| "application/#{t}" }
229
-
230
- default_paths!
231
- default_security!
232
- global_configuration!
233
- setup_prerequisites!
234
- end
235
-
236
- def setup_prerequisites!
237
- prerequisites.concat(default_prerequisites).uniq!
238
- Padrino.require_dependencies(prerequisites)
239
- end
240
-
241
- def default_paths!
242
- set :locale_path, Proc.new { Dir.glob File.join(root, 'locale/**/*.{rb,yml}') }
243
- set :views, Proc.new { File.join(root, 'views') }
244
-
245
- set :uri_root, '/'
246
- set :public_folder, Proc.new { Padrino.root('public', uri_root) }
247
- set :images_path, Proc.new { File.join(public_folder, 'images') }
248
- end
249
-
250
- def default_security!
251
- set :protection, :except => :path_traversal
252
- set :authentication, false
253
- set :sessions, false
254
- set :protect_from_csrf, false
255
- set :allow_disabled_csrf, false
256
- end
257
-
258
- ##
259
- # Applies global padrino configuration blocks to current application.
260
- #
261
- def global_configuration!
262
- Padrino.global_configurations.each do |configuration|
263
- class_eval(&configuration)
264
- end
265
- end
266
-
267
- ##
268
- # We need to add almost __sinatra__ images.
269
- #
270
- def default_routes!
271
- configure :development do
272
- get '*__sinatra__/:image.png' do
273
- content_type :png
274
- filename = File.dirname(__FILE__) + "/images/#{params[:image]}.png"
275
- send_file filename
276
- end
277
- end
278
- end
279
-
280
- ##
281
- # This filter it's used for know the format of the request, and
282
- # automatically set the content type.
283
- #
284
- def default_filters!
285
- before do
286
- response['Content-Type'] = 'text/html;charset=utf-8' unless @_content_type
287
- end
288
- end
289
-
290
- ##
291
- # This log errors for production environments.
292
- #
293
- def default_errors!
294
- configure :production do
295
- error ::Exception do
296
- boom = env['sinatra.error']
297
- logger.error ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n ")
298
- response.status = 500
299
- content_type 'text/html'
300
- '<h1>Internal Server Error</h1>'
301
- end unless errors.has_key?(::Exception)
302
- end
303
- end
304
-
305
190
  ##
306
191
  # Requires all files within the application load paths.
307
192
  #
@@ -309,80 +194,6 @@ module Padrino
309
194
  Padrino.set_load_paths(*load_paths)
310
195
  Padrino.require_dependencies(dependencies, :force => true)
311
196
  end
312
-
313
- ##
314
- # Returns globs of default paths of application prerequisites.
315
- #
316
- def default_prerequisites
317
- [
318
- '/models.rb',
319
- '/models/**/*.rb',
320
- '/lib.rb',
321
- '/lib/**/*.rb',
322
- ].map{ |glob| File.join(settings.root, glob) }
323
- end
324
-
325
- private
326
-
327
- # Overrides the default middleware for Sinatra based on Padrino conventions.
328
- # Also initializes the application after setting up the middleware.
329
- def setup_default_middleware(builder)
330
- setup_sessions builder
331
- builder.use Padrino::ShowExceptions if show_exceptions?
332
- builder.use Padrino::Logger::Rack, uri_root if Padrino.logger && logging?
333
- builder.use Padrino::Reloader::Rack if reload?
334
- builder.use Rack::MethodOverride if method_override?
335
- builder.use Rack::Head
336
- register Padrino::Flash
337
- setup_protection builder
338
- setup_csrf_protection builder
339
- setup_application!
340
- end
341
-
342
- # sets up csrf protection for the app:
343
- def setup_csrf_protection(builder)
344
- check_csrf_protection_dependency
345
-
346
- if protect_from_csrf?
347
- options = options_for_csrf_protection_setup
348
- options.merge!(protect_from_csrf) if protect_from_csrf.kind_of?(Hash)
349
- builder.use(options[:except] ? Padrino::AuthenticityToken : Rack::Protection::AuthenticityToken, options)
350
- end
351
- end
352
-
353
- # returns the options used in the builder for csrf protection setup
354
- def options_for_csrf_protection_setup
355
- options = { :logger => logger }
356
-
357
- if allow_disabled_csrf?
358
- options.merge!({
359
- :reaction => :report,
360
- :report_key => 'protection.csrf.failed'
361
- })
362
- end
363
- options
364
- end
365
-
366
- # throw an exception if the protect_from_csrf is active but sessions not.
367
- def check_csrf_protection_dependency
368
- if (protect_from_csrf? && !sessions?) && !defined?(Padrino::IGNORE_CSRF_SETUP_WARNING)
369
- warn(<<-ERROR)
370
- `protect_from_csrf` is activated, but `sessions` seem to be off. To enable csrf
371
- protection, use:
372
-
373
- enable :sessions
374
-
375
- or deactivate protect_from_csrf:
376
-
377
- disable :protect_from_csrf
378
-
379
- If you use a different session store, ignore this warning using:
380
-
381
- # in boot.rb:
382
- Padrino::IGNORE_CSRF_SETUP_WARNING = true
383
- ERROR
384
- end
385
- end
386
197
  end
387
198
  end
388
199
  end
@@ -0,0 +1,199 @@
1
+ module Padrino
2
+ ##
3
+ # Holds setup-oriented methods for Padrino::Application.
4
+ #
5
+ module ApplicationSetup
6
+ def self.registered(app)
7
+ app.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ ##
12
+ # Defines default settings for Padrino application.
13
+ #
14
+ def default_configuration!
15
+ set :app_file, File.expand_path(caller_files.first || $0)
16
+ set :app_name, settings.to_s.underscore.to_sym
17
+
18
+ set :environment, Padrino.env
19
+ set :reload, proc { development? }
20
+ set :logging, proc { development? }
21
+
22
+ set :method_override, true
23
+ set :default_builder, 'StandardFormBuilder'
24
+
25
+ # TODO: Remove this hack after getting rid of thread-unsafe http_router:
26
+ set :init_mutex, Mutex.new
27
+
28
+ # TODO: Remove this line after sinatra version up.
29
+ set :add_charset, %w[javascript xml xhtml+xml].map{ |type| "application/#{type}" }
30
+
31
+ default_paths
32
+ default_security
33
+ global_configuration
34
+ setup_prerequisites
35
+ end
36
+
37
+ ##
38
+ # Setup the application by registering initializers, load paths and logger.
39
+ # Invoked automatically when an application is first instantiated.
40
+ #
41
+ # @return [TrueClass]
42
+ #
43
+ def setup_application!
44
+ return if @_configured
45
+ require_dependencies
46
+ default_filters
47
+ default_routes
48
+ default_errors
49
+ setup_locale
50
+ @_configured = true
51
+ end
52
+
53
+ private
54
+
55
+ def default_paths
56
+ set :locale_path, proc { Dir.glob File.join(root, 'locale/**/*.{rb,yml}') }
57
+ set :views, proc { File.join(root, 'views') }
58
+
59
+ set :uri_root, '/'
60
+ set :public_folder, proc { Padrino.root('public', uri_root) }
61
+ set :images_path, proc { File.join(public_folder, 'images') }
62
+ end
63
+
64
+ def default_security
65
+ set :protection, :except => :path_traversal
66
+ set :sessions, false
67
+ set :protect_from_csrf, false
68
+ set :report_csrf_failure, false
69
+ set :allow_disabled_csrf, false
70
+ end
71
+
72
+ ##
73
+ # Applies global padrino configuration blocks to current application.
74
+ #
75
+ def global_configuration
76
+ Padrino.global_configurations.each do |configuration|
77
+ class_eval(&configuration)
78
+ end
79
+ end
80
+
81
+ def setup_prerequisites
82
+ prerequisites.concat(default_prerequisites).uniq!
83
+ Padrino.require_dependencies(prerequisites)
84
+ end
85
+
86
+ ##
87
+ # Returns globs of default paths of application prerequisites.
88
+ #
89
+ def default_prerequisites
90
+ [
91
+ '/models.rb',
92
+ '/models/**/*.rb',
93
+ '/lib.rb',
94
+ '/lib/**/*.rb',
95
+ ].map{ |glob| File.join(settings.root, glob) }
96
+ end
97
+
98
+ # Overrides the default middleware for Sinatra based on Padrino conventions.
99
+ # Also initializes the application after setting up the middleware.
100
+ def setup_default_middleware(builder)
101
+ setup_sessions builder
102
+ builder.use Padrino::ShowExceptions if show_exceptions?
103
+ builder.use Padrino::Logger::Rack, uri_root if Padrino.logger && logging?
104
+ builder.use Padrino::Reloader::Rack if reload?
105
+ builder.use Rack::MethodOverride if method_override?
106
+ builder.use Rack::Head
107
+ register Padrino::Flash
108
+ setup_protection builder
109
+ setup_csrf_protection builder
110
+ setup_application!
111
+ end
112
+
113
+ ##
114
+ # This filter it's used for know the format of the request, and
115
+ # automatically set the content type.
116
+ #
117
+ def default_filters
118
+ before do
119
+ response['Content-Type'] = 'text/html;charset=utf-8' unless @_content_type
120
+ end
121
+ end
122
+
123
+ ##
124
+ # We need to add almost __sinatra__ images.
125
+ #
126
+ def default_routes
127
+ configure :development do
128
+ get '*__sinatra__/:image.png' do
129
+ content_type :png
130
+ send_file(File.dirname(__FILE__) + "/../images/#{params[:image]}.png")
131
+ end
132
+ end
133
+ end
134
+
135
+ ##
136
+ # This log errors for production environments.
137
+ #
138
+ def default_errors
139
+ configure :production do
140
+ error ::Exception do
141
+ logger.exception env['sinatra.error']
142
+ halt(500, { 'Content-Type' => 'text/html' }, ['<h1>Internal Server Error</h1>'])
143
+ end unless errors.has_key?(::Exception)
144
+ end
145
+ end
146
+
147
+ def setup_locale
148
+ return unless defined? I18n
149
+ Reloader.special_files += locale_path
150
+ I18n.load_path << locale_path
151
+ I18n.reload!
152
+ end
153
+
154
+ # sets up csrf protection for the app
155
+ def setup_csrf_protection(builder)
156
+ check_csrf_protection_dependency
157
+
158
+ if protect_from_csrf?
159
+ options = options_for_csrf_protection_setup
160
+ options.merge!(protect_from_csrf) if protect_from_csrf.kind_of?(Hash)
161
+ builder.use(options[:except] ? Padrino::AuthenticityToken : Rack::Protection::AuthenticityToken, options)
162
+ end
163
+ end
164
+
165
+ # returns the options used in the builder for csrf protection setup
166
+ def options_for_csrf_protection_setup
167
+ options = { :logger => logger }
168
+ if report_csrf_failure? || allow_disabled_csrf?
169
+ options.merge!(
170
+ :reaction => :report,
171
+ :report_key => 'protection.csrf.failed'
172
+ )
173
+ end
174
+ options
175
+ end
176
+
177
+ # warn if the protect_from_csrf is active but sessions are not
178
+ def check_csrf_protection_dependency
179
+ if (protect_from_csrf? && !sessions?) && !defined?(Padrino::IGNORE_CSRF_SETUP_WARNING)
180
+ warn(<<-ERROR)
181
+ `protect_from_csrf` is activated, but `sessions` seem to be off. To enable csrf
182
+ protection, use:
183
+
184
+ enable :sessions
185
+
186
+ or deactivate protect_from_csrf:
187
+
188
+ disable :protect_from_csrf
189
+
190
+ If you use a different session store, ignore this warning using:
191
+
192
+ # in boot.rb:
193
+ Padrino::IGNORE_CSRF_SETUP_WARNING = true
194
+ ERROR
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end