Skip to content

Commit

Permalink
Work around Rack::Static bug.
Browse files Browse the repository at this point in the history
If the :index isn't provided it defaults to index.html. There is no way, in
the current gem version, to set :index to not trigger, so you can't have
a route on / when using Rack::Static. This is fixed in Rack master, just
need a new rack version pushed and we can change back to '/'.
  • Loading branch information
dj2 committed Jan 19, 2012
1 parent b81e1b3 commit 340e8a0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
7 changes: 4 additions & 3 deletions examples/template.rb
Expand Up @@ -24,8 +24,8 @@ class Template < Goliath::API
include Goliath::Rack::Templates # render templated files from ./views

use(Rack::Static, # render static files from ./public
:root => Goliath::Application.app_path("public"),
:urls => ["/favicon.ico", '/stylesheets', '/javascripts', '/images'])
:root => Goliath::Application.app_path("public"),
:urls => ["/favicon.ico", '/stylesheets', '/javascripts', '/images'])

plugin Goliath::Plugin::Latency # ask eventmachine reactor to track its latency

Expand All @@ -35,7 +35,8 @@ def recent_latency

def response(env)
case env['PATH_INFO']
when '/' then [200, {}, haml(:root)]
# TODO(dj2): change /root -> / when rack > 1.4.0 is released
when '/root' then [200, {}, haml(:root)]
when '/haml_str' then [200, {}, haml("%h1 Header")]
when '/debug' then [200, {}, haml(:debug)]
when '/oops' then [200, {}, haml(:no_such_template)] # will 500
Expand Down
8 changes: 4 additions & 4 deletions lib/goliath/rack/templates.rb
Expand Up @@ -320,26 +320,26 @@ def render(engine, data, options = {}, locals = {}, &block)
else
Tilt.new(layout_filename, nil, options)
end

# mimic sinatra behavior, if a string is given consider it as a template source
# otherwise a symbol is expected to be a template path
if data.is_a?(Symbol)
template_filename = find_template(views, data, engine)
unless template_filename
raise Goliath::Validation::InternalServerError, "Template #{data} not found in #{views} for #{engine}"
end

template = Tilt.new(template_filename, nil, options)
output = layout_template.render(scope, locals) do
template.render(scope, locals)
end

else
template = Tilt[engine].new(nil, nil, options){ data }
output = layout_template.render(scope, locals) do
template.render(scope, locals)
end

end

output.extend(ContentTyped).content_type = content_type if content_type
Expand Down
8 changes: 2 additions & 6 deletions spec/integration/template_spec.rb
@@ -1,26 +1,22 @@
require 'spec_helper'
require File.join(File.dirname(__FILE__), '../../', 'examples/template')


describe Template do

def config_file
File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'examples', 'config', 'template.rb'))
end

let(:api_options) { { :config => config_file } }

it 'renders haml template with default haml layout' do

with_api(Template, api_options) do
get_request do |c|
get_request(:path => '/root') do |c|
c.response.should =~ %r{<li><a href="/joke">Tell me a joke</a></li>}
end
end
end

it 'renders haml template from string with default haml layout' do

with_api(Template, api_options) do
get_request(:path => '/haml_str') do |c|
c.response.should =~ %r{<h1>Header</h1>}
Expand Down

0 comments on commit 340e8a0

Please sign in to comment.