From ef6d7a6324e92e5db312d1ec23c5d4cd0c24ee88 Mon Sep 17 00:00:00 2001 From: Cory ODaniel Date: Mon, 16 Jun 2008 17:40:02 -0700 Subject: [PATCH] Route#redirect. Allow redirection from the router. r.match("/old/ur").redirect("http://example.com") --- lib/merb-core/dispatch/dispatcher.rb | 42 ++++++++++++++++++++++++---- lib/merb-core/dispatch/router/behavior.rb | 2 +- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/merb-core/dispatch/dispatcher.rb b/lib/merb-core/dispatch/dispatcher.rb index 1a78f71..c8753f7 100644 --- a/lib/merb-core/dispatch/dispatcher.rb +++ b/lib/merb-core/dispatch/dispatcher.rb @@ -31,6 +31,10 @@ class Merb::Dispatcher route_index, route_params = Merb::Router.match(request) + route = Merb::Router.routes[route_index] if route_index + + return dispatch_redirection(request,route) if route && route.behavior.redirects? + if route_params.empty? raise ::Merb::ControllerExceptions::NotFound, "No routes match the request, #{request.uri}" end @@ -64,12 +68,10 @@ class Merb::Dispatcher action = route_params[:action] - if route_index && route = Merb::Router.routes[route_index] - #Fixate the session ID if it is enabled on the route - if route.allow_fixation? && request.params.key?(Merb::Controller._session_id_key) - request.cookies[Merb::Controller._session_id_key] = request.params[Merb::Controller._session_id_key] - end - end + #Fixate the session ID if it is enabled on the route + if route.allow_fixation? && request.params.key?(Merb::Controller._session_id_key) + request.cookies[Merb::Controller._session_id_key] = request.params[Merb::Controller._session_id_key] + end controller = dispatch_action(klass, action, request) controller._benchmarks[:dispatch_time] = Time.now - start @@ -107,6 +109,34 @@ Stacktrace: end private + # Set up a faux controller to do redirection from the router + # + # ==== Parameters + # request:: + # The Merb::Request object that was created in #handle + # route:: Matched route object + # + # ==== Example + # r.match("/my/old/crusty/url").redirect("http://example.com/index.html") + # + # ==== Returns + # Merb::Controller:: + # Merb::Controller set with redirect headers and a 301/302 status + def dispatch_redirection(request,route) + status = route.behavior.redirect_status + url = route.behavior.redirect_url + + controller = Merb::Controller.new(request,status) + + Merb.logger.info("Dispatcher redirecting to: #{url} (#{status})") + + controller.headers['Location'] = url + controller.body = "You are being redirected." + controller + end + + + # Setup the controller and call the chosen action # # ==== Parameters diff --git a/lib/merb-core/dispatch/router/behavior.rb b/lib/merb-core/dispatch/router/behavior.rb index dc9c859..fdfb1bf 100644 --- a/lib/merb-core/dispatch/router/behavior.rb +++ b/lib/merb-core/dispatch/router/behavior.rb @@ -7,7 +7,7 @@ module Merb #--- # @public class Behavior - attr_reader :placeholders, :conditions, :params + attr_reader :placeholders, :conditions, :params, :redirect_url, :redirect_status attr_accessor :parent @@parent_resource = [] class << self -- 1.5.2.4