Permalink
Browse files

[BUG FIX] Now works with :cache => false properly.

Thanks to joseicosta for informing me about the issue.
  • Loading branch information...
1 parent b933cb2 commit 73a443d34b2c72892b301039bdb57d3e7ea42d5d @kematzy committed Apr 27, 2010
Showing with 69 additions and 2 deletions.
  1. +2 −2 lib/sinatra/templates.rb
  2. +67 −0 spec/sinatra/cache_spec.rb
View
@@ -26,7 +26,7 @@ def render(engine, data, options={}, locals={}, &block)
cache_enabled = settings.respond_to?(:cache_enabled) ? settings.send(:cache_enabled) : false
cache_output_dir = settings.send(:cache_output_dir) if settings.respond_to?(:cache_output_dir)
# raise Exception, "The Sinatra::Cache cache_output_dir variable is pointing to a non-existant directory cache_output_dir=[#{cache_output_dir}]" unless test(?d, cache_output_dir)
- cache_option = options.delete(:cache)
+ cache_option = options[:cache]
cache_option = true if cache_option.nil?
# compile and render template
@@ -39,7 +39,7 @@ def render(engine, data, options={}, locals={}, &block)
options = options.merge(:views => views, :layout => false)
output = render(engine, layout, options, locals) { output }
# Cache the content or just return it
- (cache_enabled && settings.send(:environment) == settings.cache_environment) ?
+ (cache_enabled && cache_option && settings.send(:environment) == settings.cache_environment) ?
cache_write_file(cache_file_path, output.gsub(/\n\r?$/,"")) : output
rescue Errno::ENOENT
end
View
@@ -24,6 +24,14 @@ class MyTestApp
set :cache_output_dir, "#{public_fixtures_path}/system/cache"
set :cache_fragments_output_dir, "#{public_fixtures_path}/system/cache_fragments"
+ # NB! Although without tests, the positioning of the custom method in relation to other
+ # Cache related declaration has no effect.
+ helpers do
+ def uncached_erb(template, options={})
+ erb(template, options.merge(:cache => false ))
+ end
+ end
+
get('/') { erb(:index) }
get('/erb/?') { erb(:index, :layout => false) }
@@ -46,6 +54,11 @@ class MyTestApp
end
end
+ ## NO CACHING
+ get('/uncached/erb'){ erb(:index, :cache => false) }
+ get('/uncached/erb/no/layout'){ erb(:index, :cache => false, :layout => false ) }
+ get('/uncached/uncached_erb'){ uncached_erb(:index) }
+
get '/css/screen.css' do
content_type 'text/css'
sass(:css, :style => :compact)
@@ -343,6 +356,60 @@ module Sinatra::Cache::Helpers
end #/ URLs with multiple levels and/or with ?params attached
+ describe "with :cache => false, :layout => false " do
+
+ before(:each) do
+ @cache_file = "#{public_fixtures_path}/system/cache/uncached/erb/no/layout.html"
+ get('/uncached/erb/no/layout')
+ end
+
+ it "should output the correct HTML as expected" do
+ body.should have_tag('h1', 'HOME')
+ end
+
+ it "should NOT cache the output" do
+ test(?d, File.dirname(@cache_file) ).should == false # testing for directory
+ test(?f, @cache_file).should == false
+ end
+
+ end #/ with :cache => false, :layout => false
+
+ describe "with :cache => false" do
+
+ before(:each) do
+ @cache_file = "#{public_fixtures_path}/system/cache/uncached/erb.html"
+ get('/uncached/erb')
+ end
+
+ it "should output the correct HTML as expected" do
+ body.should have_tag('h1', 'HOME')
+ end
+
+ it "should NOT cache the output" do
+ test(?d, File.dirname(@cache_file) ).should == false # testing for directory
+ test(?f, @cache_file).should == false
+ end
+
+ end #/ with :cache => false
+
+ describe "URLs with custom erb helpers, like :admin_erb().." do
+
+ before(:each) do
+ @cache_file = "#{public_fixtures_path}/system/cache/uncached/uncached_erb.html"
+ get('/uncached/uncached_erb')
+ end
+
+ it "should output the correct HTML as expected" do
+ body.should have_tag('html > body > h1', 'HOME')
+ end
+
+ it "should NOT cache the output" do
+ test(?d, File.dirname(@cache_file) ).should == false # testing for directory
+ test(?f, @cache_file).should == false
+ end
+
+ end #/ URLs with custom erb helpers, like :admin_erb()..
+
describe "CSS URLs with dynamic .sass files" do
describe "the URL ['/css/screen.css' => /css/screen.css]" do

0 comments on commit 73a443d

Please sign in to comment.