padrino-core 0.11.3 → 0.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +5 -5
  3. data/lib/padrino-core.rb +3 -9
  4. data/lib/padrino-core/application.rb +39 -35
  5. data/lib/padrino-core/application/flash.rb +7 -7
  6. data/lib/padrino-core/application/rendering.rb +136 -139
  7. data/lib/padrino-core/application/rendering/extensions/erubis.rb +15 -6
  8. data/lib/padrino-core/application/routing.rb +371 -369
  9. data/lib/padrino-core/application/showexceptions.rb +13 -12
  10. data/lib/padrino-core/caller.rb +40 -40
  11. data/lib/padrino-core/cli/adapter.rb +4 -4
  12. data/lib/padrino-core/cli/base.rb +40 -39
  13. data/lib/padrino-core/cli/rake.rb +2 -2
  14. data/lib/padrino-core/cli/rake_tasks.rb +2 -4
  15. data/lib/padrino-core/command.rb +2 -2
  16. data/lib/padrino-core/loader.rb +14 -15
  17. data/lib/padrino-core/locale/cs.yml +0 -1
  18. data/lib/padrino-core/locale/da.yml +0 -1
  19. data/lib/padrino-core/locale/de.yml +0 -1
  20. data/lib/padrino-core/locale/en.yml +0 -1
  21. data/lib/padrino-core/locale/es.yml +1 -2
  22. data/lib/padrino-core/locale/fr.yml +2 -3
  23. data/lib/padrino-core/locale/hu.yml +1 -2
  24. data/lib/padrino-core/locale/it.yml +0 -1
  25. data/lib/padrino-core/locale/ja.yml +1 -2
  26. data/lib/padrino-core/locale/lv.yml +0 -1
  27. data/lib/padrino-core/locale/nl.yml +0 -1
  28. data/lib/padrino-core/locale/no.yml +0 -2
  29. data/lib/padrino-core/locale/pl.yml +0 -1
  30. data/lib/padrino-core/locale/pt_br.yml +0 -1
  31. data/lib/padrino-core/locale/ro.yml +0 -1
  32. data/lib/padrino-core/locale/ru.yml +0 -1
  33. data/lib/padrino-core/locale/sv.yml +0 -1
  34. data/lib/padrino-core/locale/tr.yml +0 -1
  35. data/lib/padrino-core/locale/uk.yml +0 -1
  36. data/lib/padrino-core/locale/zh_cn.yml +0 -1
  37. data/lib/padrino-core/locale/zh_tw.yml +0 -1
  38. data/lib/padrino-core/logger.rb +26 -27
  39. data/lib/padrino-core/module.rb +3 -3
  40. data/lib/padrino-core/mounter.rb +59 -59
  41. data/lib/padrino-core/reloader.rb +23 -23
  42. data/lib/padrino-core/router.rb +10 -13
  43. data/lib/padrino-core/server.rb +17 -19
  44. data/lib/padrino-core/tasks.rb +3 -3
  45. data/lib/padrino-core/version.rb +1 -1
  46. data/padrino-core.gemspec +1 -1
  47. data/test/test_application.rb +7 -7
  48. data/test/test_rendering.rb +15 -2
  49. data/test/test_routing.rb +34 -10
  50. metadata +13 -27
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4eaaf6b58a471d045e8141109e191b8932937605
4
+ data.tar.gz: f84f79482e130307c53a7fd152b8db5b2e6aec2d
5
+ SHA512:
6
+ metadata.gz: d19c9ccab9b572cb7d624e884f1e3948b8c1d4b7cd6e22cba8a8a4c16d0cc139b97365f8fdcf5e9fca288aef8b5d12d0308a193fd685d505c51c7378986cb644
7
+ data.tar.gz: f8c01eaba904cc2ba5dbab8ddb28d323721ed00b9ff56ac95619dc999cbd711172751fed794d249febfd33b932bc44a22c430c24f47182a101f7af622d956eab
@@ -22,7 +22,7 @@ Here is a brief overview of functionality provided by the Padrino framework:
22
22
  Agnostic:: Full support for many popular testing, templating, mocking, and data storage choices.
23
23
  Generators:: Create Padrino applications, models, controllers i.e: padrino-gen project.
24
24
  Mountable:: Unlike other ruby frameworks, principally designed for mounting multiple apps.
25
- Routing:: Full url named routes, named params, respond_to support, before/after filter support.
25
+ Routing:: Full url named routes, named params, before/after filter support.
26
26
  Tag Helpers:: View helpers such as: tag, content_tag, input_tag.
27
27
  Asset Helpers:: View helpers such as: link_to, image_tag, javascript_include_tag.
28
28
  Form Helpers:: Builder support such as: form_tag, form_for, field_set_tag, text_field.
@@ -146,7 +146,7 @@ as well as mapping the route aliases to an explicit url:
146
146
  end
147
147
  end
148
148
 
149
- and even configure the respond_to for each route:
149
+ and even configure the +provides+ for each route:
150
150
 
151
151
  # app/controllers/example.rb
152
152
  SimpleApp.controllers :admin do
@@ -154,7 +154,7 @@ and even configure the respond_to for each route:
154
154
  "Url is /admin/show/#{params[:id]}.#{params[:format]}"
155
155
  end
156
156
 
157
- get :other, with => [:id, :name], respond_to => [:html, :json] do
157
+ get :other, :with => [:id, :name], :provides => [:html, :json] do
158
158
  case content_type
159
159
  when :js then ... end
160
160
  when :json then ... end
@@ -166,7 +166,7 @@ or auto lookup for current locale or content_type
166
166
 
167
167
  # app/controllers/example.rb
168
168
  SimpleApp.controllers :admin do
169
- get :show, :with => :id, :provides => [html, :js] do
169
+ get :show, :with => :id, :provides => [:html, :js] do
170
170
  render "admin/show"
171
171
  end
172
172
  end
@@ -291,4 +291,4 @@ For a complete overview of Padrino terminal commands, check out the
291
291
 
292
292
  == Copyright
293
293
 
294
- Copyright (c) 2011 Padrino. See LICENSE for details.
294
+ Copyright (c) 2011-2013 Padrino. See LICENSE for details.
@@ -15,9 +15,7 @@ require 'padrino-core/tasks'
15
15
  require 'padrino-core/module'
16
16
 
17
17
 
18
- # The Padrino environment (falls back to the rack env or finally develop)
19
18
  PADRINO_ENV = ENV["PADRINO_ENV"] ||= ENV["RACK_ENV"] ||= "development" unless defined?(PADRINO_ENV)
20
- # The Padrino project root path (falls back to the first caller)
21
19
  PADRINO_ROOT = ENV["PADRINO_ROOT"] ||= File.dirname(Padrino.first_caller) unless defined?(PADRINO_ROOT)
22
20
 
23
21
  module Padrino
@@ -181,26 +179,22 @@ module Padrino
181
179
  #
182
180
  # @returns The root path of the loaded gem
183
181
  def gem(name, main_module)
184
- _,spec = Gem.loaded_specs.find { |spec_name, spec| spec_name == name }
182
+ _, spec = Gem.loaded_specs.find{|spec_pair| spec_pair[0] == name }
185
183
  gems << spec
186
184
  modules << main_module
187
185
  spec.full_gem_path
188
186
  end
189
187
 
190
188
  ##
191
- # Returns all currently known padrino gems.
192
- #
193
189
  # @returns [Gem::Specification]
194
190
  def gems
195
191
  @gems ||= []
196
192
  end
197
193
 
198
194
  ##
199
- # All loaded Padrino modules.
200
- #
201
195
  # @returns [<Padrino::Module>]
202
196
  def modules
203
197
  @modules ||= []
204
198
  end
205
- end # self
206
- end # Padrino
199
+ end
200
+ end
@@ -1,15 +1,17 @@
1
+ require 'padrino-core/application/flash'
1
2
  require 'padrino-core/application/rendering'
2
3
  require 'padrino-core/application/routing'
3
- require 'padrino-core/application/flash'
4
4
  require 'padrino-core/application/showexceptions'
5
5
 
6
6
  module Padrino
7
- class ApplicationSetupError < RuntimeError # @private
7
+ class ApplicationSetupError < RuntimeError
8
8
  end
9
9
 
10
10
  ##
11
- # Subclasses of this become independent Padrino applications (stemming from Sinatra::Application)
12
- # These subclassed applications can be easily mounted into other Padrino applications as well.
11
+ # Subclasses of this become independent Padrino applications
12
+ # (stemming from Sinatra::Application).
13
+ # These subclassed applications can be easily mounted into other
14
+ # Padrino applications as well.
13
15
  #
14
16
  class Application < Sinatra::Base
15
17
  # Support for advanced routing, controllers, url_for
@@ -25,8 +27,7 @@ module Padrino
25
27
  end
26
28
 
27
29
  class << self
28
-
29
- def inherited(base) # @private
30
+ def inherited(base)
30
31
  begun_at = Time.now
31
32
  CALLERS_TO_IGNORE.concat(PADRINO_IGNORE_CALLERS)
32
33
  base.default_configuration!
@@ -42,7 +43,7 @@ module Padrino
42
43
  end
43
44
 
44
45
  ##
45
- # Reloads the application files from all defined load paths
46
+ # Reloads the application files from all defined load paths.
46
47
  #
47
48
  # This method is used from our Padrino Reloader during development mode
48
49
  # in order to reload the source files.
@@ -54,19 +55,19 @@ module Padrino
54
55
  #
55
56
  def reload!
56
57
  logger.devel "Reloading #{settings}"
57
- reset! # Reset sinatra app
58
- reset_router! # Reset all routes
58
+ reset!
59
+ reset_router!
59
60
  Padrino.require_dependencies(settings.app_file, :force => true) # Reload the app file
60
- require_dependencies # Reload dependencies
61
- default_filters! # Reload filters
62
- default_routes! # Reload default routes
63
- default_errors! # Reload our errors
64
- I18n.reload! if defined?(I18n) # Reload also our translations
61
+ require_dependencies
62
+ default_filters!
63
+ default_routes!
64
+ default_errors!
65
+ I18n.reload! if defined?(I18n)
65
66
  true
66
67
  end
67
68
 
68
69
  ##
69
- # Resets application routes to only routes not defined by the user
70
+ # Resets application routes to only routes not defined by the user.
70
71
  #
71
72
  # @return [TrueClass]
72
73
  #
@@ -90,8 +91,8 @@ module Padrino
90
91
  end
91
92
 
92
93
  ##
93
- # Setup the application by registering initializers, load paths and logger
94
- # Invoked automatically when an application is first instantiated
94
+ # Setup the application by registering initializers, load paths and logger.
95
+ # Invoked automatically when an application is first instantiated.
95
96
  #
96
97
  # @return [TrueClass]
97
98
  #
@@ -111,7 +112,7 @@ module Padrino
111
112
 
112
113
  ##
113
114
  # Run the Padrino app as a self-hosted server using
114
- # Thin, Mongrel or WEBrick (in that order)
115
+ # Thin, Mongrel or WEBrick (in that order).
115
116
  #
116
117
  # @see Padrino::Server#start
117
118
  #
@@ -130,8 +131,8 @@ module Padrino
130
131
  end
131
132
 
132
133
  ##
133
- # Returns default list of path globs to load as dependencies
134
- # Appends custom dependency patterns to the be loaded for your Application
134
+ # Returns default list of path globs to load as dependencies.
135
+ # Appends custom dependency patterns to the be loaded for your Application.
135
136
  #
136
137
  # @return [Array]
137
138
  # list of path globs to load as dependencies
@@ -148,7 +149,8 @@ module Padrino
148
149
  end
149
150
 
150
151
  ##
151
- # An array of file to load before your app.rb, basically are files wich our app depends on.
152
+ # An array of file to load before your app.rb, basically are files
153
+ # which our app depends on.
152
154
  #
153
155
  # By default we look for files:
154
156
  #
@@ -158,7 +160,7 @@ module Padrino
158
160
  # yourapp/lib.rb
159
161
  # yourapp/lib/**/*.rb
160
162
  #
161
- # @example Adding a custom perequisite
163
+ # @example Adding a custom prerequisite
162
164
  # MyApp.prerequisites << Padrino.root('my_app', 'custom_model.rb')
163
165
  #
164
166
  def prerequisites
@@ -167,7 +169,7 @@ module Padrino
167
169
 
168
170
  protected
169
171
  ##
170
- # Defines default settings for Padrino application
172
+ # Defines default settings for Padrino application.
171
173
  #
172
174
  def default_configuration!
173
175
  # Overwriting Sinatra defaults
@@ -181,15 +183,17 @@ module Padrino
181
183
  set :views, Proc.new { File.join(root, 'views') }
182
184
  set :images_path, Proc.new { File.join(public_folder, 'images') }
183
185
  set :protection, true
184
- # Haml specific
186
+
185
187
  set :haml, { :ugly => (Padrino.env == :production) } if defined?(Haml)
188
+
186
189
  # Padrino specific
187
190
  set :uri_root, '/'
188
191
  set :app_name, settings.to_s.underscore.to_sym
189
192
  set :default_builder, 'StandardFormBuilder'
190
193
  set :authentication, false
191
- # Padrino locale
194
+
192
195
  set :locale_path, Proc.new { Dir[File.join(settings.root, '/locale/**/*.{rb,yml}')] }
196
+
193
197
  # Authenticity token
194
198
  set :protect_from_csrf, false
195
199
  set :allow_disabled_csrf, false
@@ -211,19 +215,19 @@ module Padrino
211
215
  end
212
216
 
213
217
  ##
214
- # This filter it's used for know the format of the request, and automatically set the content type.
218
+ # This filter it's used for know the format of the request, and
219
+ # automatically set the content type.
215
220
  #
216
221
  def default_filters!
217
222
  before do
218
223
  unless @_content_type
219
- @_content_type = :html
220
224
  response['Content-Type'] = 'text/html;charset=utf-8'
221
225
  end
222
226
  end
223
227
  end
224
228
 
225
229
  ##
226
- # This log errors for production environments
230
+ # This log errors for production environments.
227
231
  #
228
232
  def default_errors!
229
233
  configure :production do
@@ -238,7 +242,7 @@ module Padrino
238
242
  end
239
243
 
240
244
  ##
241
- # Requires all files within the application load paths
245
+ # Requires all files within the application load paths.
242
246
  #
243
247
  def require_dependencies
244
248
  Padrino.set_load_paths(*load_paths)
@@ -246,8 +250,8 @@ module Padrino
246
250
  end
247
251
 
248
252
  private
249
- # Overrides the default middleware for Sinatra based on Padrino conventions
250
- # Also initializes the application after setting up the middleware
253
+ # Overrides the default middleware for Sinatra based on Padrino conventions.
254
+ # Also initializes the application after setting up the middleware.
251
255
  def setup_default_middleware(builder)
252
256
  setup_sessions builder
253
257
  builder.use Padrino::ShowExceptions if show_exceptions?
@@ -261,7 +265,7 @@ module Padrino
261
265
  setup_application!
262
266
  end
263
267
 
264
- # sets up csrf protection for the app
268
+ # sets up csrf protection for the app:
265
269
  def setup_csrf_protection(builder)
266
270
  if protect_from_csrf? && !sessions?
267
271
  raise(<<-ERROR)
@@ -288,6 +292,6 @@ ERROR
288
292
  end
289
293
  end
290
294
  end
291
- end # self
292
- end # Application
293
- end # Padrino
295
+ end
296
+ end
297
+ end
@@ -2,14 +2,13 @@ module Padrino
2
2
  module Flash
3
3
 
4
4
  class << self
5
- # @private
6
5
  def registered(app)
7
6
  app.helpers Helpers
8
7
  app.after do
9
8
  session[:_flash] = @_flash.next if @_flash
10
9
  end
11
10
  end
12
- end # self
11
+ end
13
12
 
14
13
  class Storage
15
14
  include Enumerable
@@ -178,7 +177,8 @@ module Padrino
178
177
 
179
178
  module Helpers
180
179
  ###
181
- # Overloads the existing redirect helper in-order to provide support for flash messages
180
+ # Overloads the existing redirect helper in-order to provide support for
181
+ # flash messages.
182
182
  #
183
183
  # @overload redirect(url)
184
184
  # @param [String] url
@@ -215,7 +215,7 @@ module Padrino
215
215
  alias_method :redirect_to, :redirect
216
216
 
217
217
  ###
218
- # Returns the flash storage object
218
+ # Returns the flash storage object.
219
219
  #
220
220
  # @return [Storage]
221
221
  #
@@ -224,6 +224,6 @@ module Padrino
224
224
  def flash
225
225
  @_flash ||= Storage.new(env['rack.session'] ? session[:_flash] : {})
226
226
  end
227
- end # Helpers
228
- end # Flash
229
- end # Padrino
227
+ end
228
+ end
229
+ end
@@ -10,7 +10,6 @@ module Padrino
10
10
  ##
11
11
  # A SafeTemplate assumes that its output is safe.
12
12
  #
13
- # @api private
14
13
  module SafeTemplate
15
14
  def render(*)
16
15
  super.html_safe
@@ -35,13 +34,13 @@ module Padrino
35
34
  ] unless defined?(IGNORE_FILE_PATTERN)
36
35
 
37
36
  ##
38
- # Default options used in the #resolve_template-method.
37
+ # Default options used in the resolve_template-method.
39
38
  #
40
39
  DEFAULT_RENDERING_OPTIONS = { :strict_format => false, :raise_exceptions => true } unless defined?(DEFAULT_RENDERING_OPTIONS)
41
40
 
42
41
  class << self
43
42
  ##
44
- # Default engine configurations for Padrino::Rendering
43
+ # Default engine configurations for Padrino::Rendering.
45
44
  #
46
45
  # @return {Hash<Symbol,Hash>}
47
46
  # The configurations, keyed by engine.
@@ -49,9 +48,6 @@ module Padrino
49
48
  @engine_configurations ||= {}
50
49
  end
51
50
 
52
- ##
53
- # Main class that register this extension.
54
- #
55
51
  def registered(app)
56
52
  included(app)
57
53
  engine_configurations.each do |engine, configs|
@@ -93,7 +89,8 @@ module Padrino
93
89
  end
94
90
 
95
91
  ##
96
- # Returns the cached template file to render for a given url, content_type and locale.
92
+ # Returns the cached template file to render for a given url,
93
+ # content_type and locale.
97
94
  #
98
95
  # @param [Array<template_path, content_type, locale>] render_options
99
96
  #
@@ -102,7 +99,7 @@ module Padrino
102
99
  end
103
100
 
104
101
  ##
105
- # Caches the template file for the given rendering options
102
+ # Caches the template file for the given rendering options.
106
103
  #
107
104
  # @param [String] template_file
108
105
  # The path of the template file.
@@ -168,152 +165,152 @@ module Padrino
168
165
  end
169
166
 
170
167
  private
171
- ##
172
- # Enhancing Sinatra render functionality for:
173
- #
174
- # * Using layout similar to rails
175
- # * Use render 'path/to/my/template' (without symbols)
176
- # * Use render 'path/to/my/template' (with engine lookup)
177
- # * Use render 'path/to/template.haml' (with explicit engine lookup)
178
- # * Use render 'path/to/template', :layout => false
179
- # * Use render 'path/to/template', :layout => false, :engine => 'haml'
180
- #
181
- def render(engine, data=nil, options={}, locals={}, &block)
168
+ ##
169
+ # Enhancing Sinatra render functionality for:
170
+ #
171
+ # * Using layout similar to rails
172
+ # * Use render 'path/to/my/template' (without symbols)
173
+ # * Use render 'path/to/my/template' (with engine lookup)
174
+ # * Use render 'path/to/template.haml' (with explicit engine lookup)
175
+ # * Use render 'path/to/template', :layout => false
176
+ # * Use render 'path/to/template', :layout => false, :engine => 'haml'
177
+ #
178
+ def render(engine, data=nil, options={}, locals={}, &block)
182
179
 
183
- # If engine is nil, ignore engine parameter and shift up all arguments
184
- # render nil, "index", { :layout => true }, { :localvar => "foo" }
185
- engine, data, options = data, options, locals if engine.nil? && data
180
+ # If engine is nil, ignore engine parameter and shift up all arguments
181
+ # render nil, "index", { :layout => true }, { :localvar => "foo" }
182
+ engine, data, options = data, options, locals if engine.nil? && data
186
183
 
187
- # Data is a hash of options when no engine isn't explicit
188
- # render "index", { :layout => true }, { :localvar => "foo" }
189
- # Data is options, and options is locals in this case
190
- data, options, locals = nil, data, options if data.is_a?(Hash)
184
+ # Data is a hash of options when no engine isn't explicit
185
+ # render "index", { :layout => true }, { :localvar => "foo" }
186
+ # Data is options, and options is locals in this case
187
+ data, options, locals = nil, data, options if data.is_a?(Hash)
191
188
 
192
- # If data is unassigned then this is a likely a template to be resolved
193
- # This means that no engine was explicitly defined
194
- data, engine = *resolve_template(engine, options.dup) if data.nil?
189
+ # If data is unassigned then this is a likely a template to be resolved
190
+ # This means that no engine was explicitly defined
191
+ data, engine = *resolve_template(engine, options.dup) if data.nil?
195
192
 
196
- # Setup root
197
- root = settings.respond_to?(:root) ? settings.root : ""
193
+ # Use @layout if it exists
194
+ layout_was = options[:layout]
195
+ options[:layout] = @layout if options[:layout].nil? || options[:layout] == true
196
+ # Resolve layouts similar to in Rails
197
+ if options[:layout].nil? && !settings.templates.has_key?(:layout)
198
+ layout_path, layout_engine = *resolved_layout
198
199
 
199
- # Use @layout if it exists
200
- layout_was = options[:layout]
201
- options[:layout] = @layout if options[:layout].nil? || options[:layout] == true
202
- # Resolve layouts similar to in Rails
203
- if options[:layout].nil? && !settings.templates.has_key?(:layout)
204
- layout_path, layout_engine = *resolved_layout
205
- options[:layout] = layout_path || false # We need to force layout false so sinatra don't try to render it
206
- options[:layout] = false unless layout_engine == engine # TODO allow different layout engine
207
- options[:layout_engine] = layout_engine || engine if options[:layout]
208
- elsif options[:layout].present?
209
- options[:layout] = settings.fetch_layout_path(options[:layout] || @layout)
210
- end
211
- # Default to original layout value if none found
212
- options[:layout] ||= layout_was
200
+ # We need to force layout false so sinatra don't try to render it
201
+ options[:layout] = layout_path || false
202
+ options[:layout] = false unless layout_engine == engine # TODO allow different layout engine
203
+ options[:layout_engine] = layout_engine || engine if options[:layout]
204
+ elsif options[:layout].present?
205
+ options[:layout] = settings.fetch_layout_path(options[:layout] || @layout)
206
+ end
207
+ # Default to original layout value if none found.
208
+ options[:layout] ||= layout_was
213
209
 
214
- # Cleanup the template
215
- @current_engine, engine_was = engine, @current_engine
216
- @_out_buf, _buf_was = ActiveSupport::SafeBuffer.new, @_out_buf
210
+ # Cleanup the template.
211
+ @current_engine, engine_was = engine, @current_engine
212
+ @_out_buf, _buf_was = ActiveSupport::SafeBuffer.new, @_out_buf
217
213
 
218
- # Pass arguments to Sinatra render method
219
- super(engine, data, options.dup, locals, &block)
220
- ensure
221
- @current_engine = engine_was
222
- @_out_buf = _buf_was
223
- end
214
+ # Pass arguments to Sinatra render method.
215
+ super(engine, data, options.dup, locals, &block)
216
+ ensure
217
+ @current_engine = engine_was
218
+ @_out_buf = _buf_was
219
+ end
224
220
 
225
- ##
226
- # Returns the located layout tuple to be used for the rendered template
227
- # (if available).
228
- #
229
- # @example
230
- # resolve_layout
231
- # # => ["/layouts/custom", :erb]
232
- # # => [nil, nil]
233
- #
234
- def resolved_layout
235
- located_layout = resolve_template(settings.fetch_layout_path, :raise_exceptions => false, :strict_format => true)
236
- located_layout ? located_layout : [nil, nil]
237
- end
221
+ ##
222
+ # Returns the located layout tuple to be used for the rendered template
223
+ # (if available).
224
+ #
225
+ # @example
226
+ # resolve_layout
227
+ # # => ["/layouts/custom", :erb]
228
+ # # => [nil, nil]
229
+ #
230
+ def resolved_layout
231
+ located_layout = resolve_template(settings.fetch_layout_path, :raise_exceptions => false, :strict_format => true)
232
+ located_layout ? located_layout : [nil, nil]
233
+ end
238
234
 
239
- ##
240
- # Returns the template path and engine that match content_type (if present),
241
- # I18n.locale.
242
- #
243
- # @param [String] template_path
244
- # The path of the template.
245
- #
246
- # @param [Hash] options
247
- # Additional options.
248
- #
249
- # @option options [Boolean] :strict_format (false)
250
- # The resolved template must match the content_type of the request.
251
- #
252
- # @option options [Boolean] :raise_exceptions (false)
253
- # Raises a {TemplateNotFound} exception if the template cannot be located.
254
- #
255
- # @return [Array<Symbol, Symbol>]
256
- # The path and format of the template.
257
- #
258
- # @raise [TemplateNotFound]
259
- # The template could not be found.
260
- #
261
- # @example
262
- # get "/foo", :provides => [:html, :js] do; render 'path/to/foo'; end
263
- # # If you request "/foo.js" with I18n.locale == :ru => [:"/path/to/foo.ru.js", :erb]
264
- # # If you request "/foo" with I18n.locale == :de => [:"/path/to/foo.de.haml", :haml]
265
- #
266
- def resolve_template(template_path, options={})
267
- began_at = Time.now
268
- # Fetch cached template for rendering options
269
- template_path = template_path.to_s[0] == ?/ ? template_path.to_s : "/#{template_path}"
270
- rendering_options = [template_path, content_type, locale]
271
- cached_template = settings.fetch_template_file(rendering_options)
272
- if cached_template
273
- logger.debug :cached, began_at, cached_template[0] if settings.logging? && defined?(logger)
274
- return cached_template
275
- end
235
+ ##
236
+ # Returns the template path and engine that match content_type (if present),
237
+ # I18n.locale.
238
+ #
239
+ # @param [String] template_path
240
+ # The path of the template.
241
+ #
242
+ # @param [Hash] options
243
+ # Additional options.
244
+ #
245
+ # @option options [Boolean] :strict_format (false)
246
+ # The resolved template must match the content_type of the request.
247
+ #
248
+ # @option options [Boolean] :raise_exceptions (false)
249
+ # Raises a {TemplateNotFound} exception if the template cannot be located.
250
+ #
251
+ # @return [Array<Symbol, Symbol>]
252
+ # The path and format of the template.
253
+ #
254
+ # @raise [TemplateNotFound]
255
+ # The template could not be found.
256
+ #
257
+ # @example
258
+ # get "/foo", :provides => [:html, :js] do; render 'path/to/foo'; end
259
+ # # If you request "/foo.js" with I18n.locale == :ru => [:"/path/to/foo.ru.js", :erb]
260
+ # # If you request "/foo" with I18n.locale == :de => [:"/path/to/foo.de.haml", :haml]
261
+ #
262
+ def resolve_template(template_path, options={})
263
+ began_at = Time.now
264
+ _content_type = content_type || :html
265
+ # Fetch cached template for rendering options
266
+ template_path = template_path.to_s[0] == ?/ ? template_path.to_s : "/#{template_path}"
267
+ rendering_options = [template_path, _content_type, locale]
268
+ cached_template = settings.fetch_template_file(rendering_options)
269
+ if cached_template
270
+ logger.debug :cached, began_at, cached_template[0] if settings.logging? && defined?(logger)
271
+ return cached_template
272
+ end
276
273
 
277
- # Resolve view path and options
278
- options.reverse_merge!(DEFAULT_RENDERING_OPTIONS)
279
- view_path = options.delete(:views) || settings.views || "./views"
280
- target_extension = File.extname(template_path)[1..-1] || "none" # explicit template extension
281
- template_path = template_path.chomp(".#{target_extension}")
274
+ # Resolve view path and options.
275
+ options.reverse_merge!(DEFAULT_RENDERING_OPTIONS)
276
+ view_path = options.delete(:views) || settings.views || "./views"
277
+ target_extension = File.extname(template_path)[1..-1] || "none" # explicit template extension
278
+ template_path = template_path.chomp(".#{target_extension}")
282
279
 
283
- # Generate potential template candidates
284
- templates = Dir[File.join(view_path, template_path) + ".*"].map do |file|
285
- template_engine = File.extname(file)[1..-1].to_sym # retrieves engine extension
286
- template_file = file.sub(view_path, '').chomp(".#{template_engine}").to_sym # retrieves template filename
287
- [template_file, template_engine] unless IGNORE_FILE_PATTERN.any? { |pattern| template_engine.to_s =~ pattern }
288
- end
280
+ # Generate potential template candidates
281
+ templates = Dir[File.join(view_path, template_path) + ".*"].map do |file|
282
+ template_engine = File.extname(file)[1..-1].to_sym # Retrieves engine extension
283
+ template_file = file.sub(view_path, '').chomp(".#{template_engine}").to_sym # retrieves template filename
284
+ [template_file, template_engine] unless IGNORE_FILE_PATTERN.any? { |pattern| template_engine.to_s =~ pattern }
285
+ end
289
286
 
290
- # Check if we have a simple content type
291
- simple_content_type = [:html, :plain].include?(content_type)
287
+ # Check if we have a simple content type
288
+ simple_content_type = [:html, :plain].include?(_content_type)
292
289
 
293
- # Resolve final template to render
294
- located_template =
295
- templates.find { |file, e| file.to_s == "#{template_path}.#{locale}.#{content_type}" } ||
296
- templates.find { |file, e| file.to_s == "#{template_path}.#{locale}" && simple_content_type } ||
297
- templates.find { |file, e| File.extname(file.to_s) == ".#{target_extension}" or e.to_s == target_extension.to_s } ||
298
- templates.find { |file, e| file.to_s == "#{template_path}.#{content_type}" } ||
299
- templates.find { |file, e| file.to_s == "#{template_path}" && simple_content_type } ||
300
- (!options[:strict_format] && templates.first) # If not strict, fall back to the first located template
290
+ # Resolve final template to render
291
+ located_template =
292
+ templates.find { |file, e| file.to_s == "#{template_path}.#{locale}.#{_content_type}" } ||
293
+ templates.find { |file, e| file.to_s == "#{template_path}.#{locale}" && simple_content_type } ||
294
+ templates.find { |file, e| File.extname(file.to_s) == ".#{target_extension}" or e.to_s == target_extension.to_s } ||
295
+ templates.find { |file, e| file.to_s == "#{template_path}.#{_content_type}" } ||
296
+ templates.find { |file, e| file.to_s == "#{template_path}" && simple_content_type } ||
297
+ (!options[:strict_format] && templates.first) # If not strict, fall back to the first located template.
301
298
 
302
- raise TemplateNotFound, "Template '#{template_path}' not found in '#{view_path}'!" if !located_template && options[:raise_exceptions]
303
- settings.cache_template_file!(located_template, rendering_options) unless settings.reload_templates?
304
- logger.debug :template, began_at, located_template[0] if located_template && settings.logging? && defined?(logger)
305
- located_template
306
- end
299
+ raise TemplateNotFound, "Template '#{template_path}' not found in '#{view_path}'!" if !located_template && options[:raise_exceptions]
300
+ settings.cache_template_file!(located_template, rendering_options) unless settings.reload_templates?
301
+ logger.debug :template, began_at, located_template[0] if located_template && settings.logging? && defined?(logger)
302
+ located_template
303
+ end
307
304
 
308
- ##
309
- # Return the I18n.locale if I18n is defined.
310
- #
311
- def locale
312
- I18n.locale if defined?(I18n)
313
- end
314
- end # InstanceMethods
315
- end # Rendering
316
- end # Padrino
305
+ ##
306
+ # Return the I18n.locale if I18n is defined.
307
+ #
308
+ def locale
309
+ I18n.locale if defined?(I18n)
310
+ end
311
+ end
312
+ end
313
+ end
317
314
 
318
315
  require 'padrino-core/application/rendering/extensions/haml'
319
316
  require 'padrino-core/application/rendering/extensions/erubis'