Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Use tilt to support other templating languages, like haml.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Youngman committed Jul 20, 2010
1 parent 1973662 commit d6800c1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -18,7 +18,7 @@ And, of course, install the gem:

Done! From now on users with IE version lower than 7.0 will get a nice error message saying that their browsers need to be updated... Ok, ok, not so nice message, you can personalize it by putting an <tt>upgrade.html</tt> file inside the <tt>/public</tt> directory of your application.

<em>- Cool!, what about something more dynamic... like ERB?</em>
<em>- Cool!, what about something more dynamic... like ERB or HAML?</em>

Granted! You'll have to add to the config which template to use. In <tt>environment.rb</tt>:

Expand Down
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -23,6 +23,7 @@ begin
gemspec.summary = "Rack Middleware for filtering by user agent"
gemspec.add_dependency('rack', '>= 0.9.1')
gemspec.add_dependency('useragent', '>=0.1.4')
gemspec.add_dependency('tilt', '>=1.0.0')
end
rescue LoadError
end
3 changes: 2 additions & 1 deletion lib/rack/user_agent/filter.rb
@@ -1,5 +1,6 @@
require 'user_agent'
require 'erb'
require 'tilt'
require 'ostruct'

module Rack::UserAgent
Expand Down Expand Up @@ -35,7 +36,7 @@ def detection_disabled_by_cookie?(cookies)
def render_page(browser)
if @template && File.exists?(@template)
@browser = browser # for the template
ERB.new(File.read(@template)).result(binding)
Tilt.new(@template).render(self)
else
"Sorry, your browser is not supported. Please upgrade"
end
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/upgrade.haml
@@ -0,0 +1 @@
= "Hello, #{@browser.browser} #{@browser.version}!"
18 changes: 18 additions & 0 deletions test/user_agent_filter_test.rb
Expand Up @@ -54,6 +54,24 @@ def app

end

context "custom page (haml)" do
setup do
def app
Rack::Builder.new do
use Rack::UserAgent::Filter, [{:browser => "Internet Explorer", :version => "7.0"}], :template => File.dirname(__FILE__) + "/fixtures/upgrade.haml"
run SampleApp.new
end
end
end

should "work" do
header "User-Agent", @outdated_browser
get '/'
assert_equal "Hello, Internet Explorer 6.0!\n", last_response.body
end

end

context "cookie" do

setup do
Expand Down

0 comments on commit d6800c1

Please sign in to comment.