From cfe72087d3179c2e80f923f9061d9450714a1547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 11 Sep 2011 00:47:01 +0200 Subject: implement Node#redirect + add tests in sample.ru --- examples/sample.ru | 7 ++++++- lib/zorglub/node.rb | 28 +++++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/examples/sample.ru b/examples/sample.ru index 5692ef5..00d1b7a 100644 --- a/examples/sample.ru +++ b/examples/sample.ru @@ -42,6 +42,10 @@ class Node1 < Zorglub::Node "should never be seeen" end # + def jump *args + redirect r(:index,1,2,3) + end + # end # APP = Zorglub::App.new do @@ -106,7 +110,7 @@ class Node0 < Zorglub::Node # def index html = "" @@ -119,6 +123,7 @@ Node1::LINKS= [ [Node1.r('index','arg1','arg2','arg3'),'index'], [Node1.r('meth0'),'meth0'], [Node1.r('meth1','one','two'),'meth1 with args'], + [Node1.r('jump','one','two'),'test redirect'], [Node0.r,'back'], ] # diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb index 357b97f..a161cb6 100644 --- a/lib/zorglub/node.rb +++ b/lib/zorglub/node.rb @@ -55,13 +55,27 @@ module Zorglub end # def realize - @content = self.send @action[:method], *@action[:args] - e, v, l = Config.engine_proc(@action[:engine]), view, layout - # TODO compile and cache - @content = e.call v, self if e and File.exists? v - @content = e.call l, self if e and File.exists? l - response.write @content - response.finish + catch(:stop_realize) { + @content = self.send @action[:method], *@action[:args] + e, v, l = Config.engine_proc(@action[:engine]), view, layout + # TODO compile and cache + @content = e.call v, self if e and File.exists? v + @content = e.call l, self if e and File.exists? l + response.write @content + response.finish + response + } + end + # + def redirect target, options={}, &block + status = options[:status] || 302 + body = options[:body] || redirect_body(target) + header = response.header.merge('Location' => target.to_s) + throw :stop_realize, Rack::Response.new(body, status, header, &block) + end + # + def redirect_body target + "You are being redirected, please follow this link to: #{target}!" end # def engine engine=nil -- cgit v1.1-2-g2b99