Module: Ramaze::Helper::Redirect

Defined in:
lib/ramaze/helper/redirect.rb

Overview

Helper::Redirect actually takes advantage of Helper::Link.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

Instance Method Summary collapse

Instance Method Details

#raw_redirect(target, opts = {}) ⇒ Object

Do not perform any mutations on the target like #redirect does. Suitable if you have to redirect to a different subdomain or host.



59
60
61
62
63
64
65
66
67
68
# File 'lib/ramaze/helper/redirect.rb', line 59

def raw_redirect(target, opts = {})
  target = target.to_s
  header = {'Location' => target}
  status = opts[:status] || 302 # Found
  body = %{You are being redirected, please follow <a href="#{target}">this link to: #{target}</a>!}

  Log.info("Redirect to '#{target}'")
  request[:redirected] = true
  throw(:redirect, [body, status, header])
end

#redirect(target, opts = {}) ⇒ Object

Usage:

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


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ramaze/helper/redirect.rb', line 44

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] = "#{request.protocol}://#{host}"
    end
  end

  raw_redirect(target, opts)
end

#redirect_referer(fallback = R(:/)) ⇒ Object Also known as: redirect_referrer

Redirect to the location the browser says it’s coming from. If the current address is the same as the referrer or no referrer exists yet, we will redirect to fallback.

NOTE:

* In some cases this may result in a double redirect, given that the
  request query parameters may change order. We don't have a nice way
  of handling that yet, but it should be very, very rare


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ramaze/helper/redirect.rb', line 84

def redirect_referer(fallback = R(:/))
  if referer = request.referer and url = request.url
    referer_uri = URI(referer)
    request_uri = URI(url)

    if referer_uri == request_uri
      redirect fallback
    else
      redirect referer
    end
  else
    redirect fallback
  end
end

#redirected?Boolean

Are we being redirected?

Returns:

  • (Boolean)


71
72
73
# File 'lib/ramaze/helper/redirect.rb', line 71

def redirected?
  request[:redirected]
end

#respond(*args) ⇒ Object

render to the browser directly, ignoring any templates

Usage:

respond 'Page not found', 404
respond render_template('forbidden.erb'), 403
respond File.open('file.jpg'), 200, 'Content-Type' => 'image/jpeg'


32
33
34
35
# File 'lib/ramaze/helper/redirect.rb', line 32

def respond(*args)
  response.build(*args)
  throw(:respond)
end