Sha256: 7d1286be81d2af2340b89880b98232b688d62bb8a7495dc20acf570a7dea3952

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

#          Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

module Ramaze

  # RedirectHelper actually takes advantage of LinkHelper.link_raw to build the links
  # it redirects to.
  # It doesn't do much else than this:
  #     setting a status-code of 303 and a response['Location'] = link
  # returning some nice text for visitors who insist on ignoring those hints :P
  #
  # Usage:
  #   redirect Rs()
  #   redirect R(MainController)
  #   redirect R(MainController, :foo)
  #   redirect 'foo/bar'
  #   redirect 'foo/bar', :status => 301
  #
  # TODO:
  #   - maybe some more options, like a delay
  #

  module RedirectHelper

    private

    # render to the browser directly, ignoring any templates
    def respond *args
      response.build(*args)
      throw :respond
    end

    # Usage:
    #   redirect Rs()
    #   redirect R(MainController)
    #   redirect R(MainController, :foo)
    #   redirect 'foo/bar'
    #   redirect 'foo/bar', :status => 301

    def redirect target, opts = {}
      target = target.to_s

      unless target =~ %r!^https?://!
        target[0,0] = '/' unless target =~ %r!^/!
        if host = request.env['HTTP_HOST']
          target[0,0] = "http://#{host}"
        end
      end

      header = {'Location' => target}
      status = opts[:status] || STATUS_CODE["See Other"]
      body = %{You are being redirected, please follow <a href="#{target}">this link to: #{target}</a>!}

      Inform.info("Redirect to '#{target}'")
      throw(:redirect, [body, status, header])
    end

    # redirect to the location the browser says it's coming from.

    def redirect_referer
      redirect request.referer
    end
    alias redirect_referrer redirect_referer
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ramaze-0.2.1 lib/ramaze/helper/redirect.rb
ramaze-0.2.0 lib/ramaze/helper/redirect.rb