Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor and test rendering from Path-able objects.

  • Loading branch information...
commit e49e5407b3ba19e08aa7403c2d13411d1c8ff180 1 parent 38de911
@blambeau authored
Showing with 29 additions and 5 deletions.
  1. +7 −5 lib/sinatra/base.rb
  2. +22 −0 test/templates_test.rb
View
12 lib/sinatra/base.rb
@@ -782,12 +782,14 @@ def tilt_compile(view, greedy={})
tilt_compile(greedy)
when String
tilt_compile(Proc.new{ view }, greedy)
- when Path
- greedy[:engine] ||= find_view_engine(view.to_s, greedy)
- greedy[:location] = [view.to_s, 1]
- tilt_compile(greedy)
else
- raise ArgumentError, "Sorry, don't know how to render #{view.inspect}."
+ path = view.path if view.respond_to?(:path)
+ path ||= view.to_path if view.respond_to?(:to_path)
+ path ||= view.to_s if defined?(Pathname) and Pathname===view
+ raise ArgumentError, "Sorry, don't know how to render #{view.inspect}." unless path
+ greedy[:engine] ||= find_view_engine(path, greedy)
+ greedy[:location] = [path, 1]
+ tilt_compile(greedy)
end
end
View
22 test/templates_test.rb
@@ -52,6 +52,28 @@ def with_default_layout
assert_equal "Hello World!\n", body
end
+ it 'renders Pathname from the underlying file' do
+ require 'pathname'
+ path = Pathname.new(File.dirname(__FILE__) + '/views/hello.test')
+ render_app{ render(:test, path) }
+ assert ok?
+ assert_equal "Hello World!\n", body
+ end
+
+ it 'renders objects responding to :path from the file' do
+ path = Struct.new(:path).new(File.dirname(__FILE__) + '/views/hello.test')
+ render_app{ render(:test, path) }
+ assert ok?
+ assert_equal "Hello World!\n", body
+ end
+
+ it 'renders objects responding to :to_path from the file' do
+ path = Struct.new(:to_path).new(File.dirname(__FILE__) + '/views/hello.test')
+ render_app{ render(:test, path) }
+ assert ok?
+ assert_equal "Hello World!\n", body
+ end
+
it 'uses the default layout template if not explicitly overridden' do
with_default_layout do
render_app { render(:test, :hello) }
Please sign in to comment.
Something went wrong with that request. Please try again.