Class: ActionDispatch::Routing::Redirect

Inherits:
Endpoint show all
Defined in:
actionpack/lib/action_dispatch/routing/redirection.rb

Overview

:nodoc:

Direct Known Subclasses

OptionRedirect, PathRedirect

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Endpoint

#app, #dispatcher?, #engine?, #matches?, #rack_app

Constructor Details

#initialize(status, block) ⇒ Redirect

Returns a new instance of Redirect.



13
14
15
16
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 13

def initialize(status, block)
  @status = status
  @block  = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block



11
12
13
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 11

def block
  @block
end

#statusObject (readonly)

Returns the value of attribute status



11
12
13
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 11

def status
  @status
end

Instance Method Details

#build_response(req) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 33

def build_response(req)
  uri = URI.parse(path(req.path_parameters, req))

  unless uri.host
    if relative_path?(uri.path)
      uri.path = "#{req.script_name}/#{uri.path}"
    elsif uri.path.empty?
      uri.path = req.script_name.empty? ? "/" : req.script_name
    end
  end

  uri.scheme ||= req.scheme
  uri.host   ||= req.host
  uri.port   ||= req.port unless req.standard_port?

  req.commit_flash

  body = ""

  headers = {
    "Location" => uri.to_s,
    "Content-Type" => "text/html; charset=#{ActionDispatch::Response.default_charset}",
    "Content-Length" => body.length.to_s
  }

  ActionDispatch::Response.new(status, headers, body)
end

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 20

def call(env)
  ActiveSupport::Notifications.instrument("redirect.action_dispatch") do |payload|
    request = Request.new(env)
    response = build_response(request)

    payload[:status] = @status
    payload[:location] = response.headers["Location"]
    payload[:request] = request

    response.to_a
  end
end

#inspectObject



65
66
67
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 65

def inspect
  "redirect(#{status})"
end

#path(params, request) ⇒ Object



61
62
63
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 61

def path(params, request)
  block.call params, request
end

#redirect?Boolean

Returns:

  • (Boolean)


18
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 18

def redirect?; true; end