Class: Cuprum::Rails::Responses::Html::RedirectBackResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/rails/responses/html/redirect_back_response.rb

Overview

Encapsulates an HTML response that redirects to the previous path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fallback_location: '/', flash: {}, status: 302) ⇒ RedirectBackResponse

Returns a new instance of RedirectBackResponse.

Parameters:

  • fallback_location (String) (defaults to: '/')

    the path or url to redirect to if the previous location cannot be determined.

  • flash (Hash) (defaults to: {})

    the flash messages to set.

  • status (Integer) (defaults to: 302)

    the HTTP status of the response.



12
13
14
15
16
# File 'lib/cuprum/rails/responses/html/redirect_back_response.rb', line 12

def initialize(fallback_location: '/', flash: {}, status: 302)
  @fallback_location = fallback_location
  @flash             = flash
  @status            = status
end

Instance Attribute Details

#fallback_locationString (readonly)

Returns the path or url to redirect to if the previous location cannot be determined.

Returns:

  • (String)

    the path or url to redirect to if the previous location cannot be determined.



20
21
22
# File 'lib/cuprum/rails/responses/html/redirect_back_response.rb', line 20

def fallback_location
  @fallback_location
end

#flashHash (readonly)

Returns the flash messages to set.

Returns:

  • (Hash)

    the flash messages to set.



23
24
25
# File 'lib/cuprum/rails/responses/html/redirect_back_response.rb', line 23

def flash
  @flash
end

#statusInteger (readonly)

Returns the HTTP status of the response.

Returns:

  • (Integer)

    the HTTP status of the response.



26
27
28
# File 'lib/cuprum/rails/responses/html/redirect_back_response.rb', line 26

def status
  @status
end

Instance Method Details

#call(renderer) ⇒ Object

Calls the renderer’s #redirect_back_or_to method with the status.

Parameters:

  • renderer (#redirect_back_or_to)

    the context for executing the response, such as a Rails controller.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cuprum/rails/responses/html/redirect_back_response.rb', line 32

def call(renderer)
  assign_flash(renderer)

  # :nocov:
  if Rails.version >= '7.0' # @todo Rails 6
    renderer.redirect_back_or_to(fallback_location, status: status)
  else
    renderer.redirect_back(
      fallback_location: fallback_location,
      status:            status
    )
  end
  # :nocov:
end