Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/lotus/action/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ module Redirect

private

# Redirect to the given URL
# Redirect to the given URL and halt the request
#
# @param url [String] the destination URL
# @param status [Fixnum] the http code
#
# @since 0.1.0
#
# @see Lotus::Action::Throwable#halt
#
# @example With default status code (302)
# require 'lotus/controller'
#
Expand Down Expand Up @@ -50,7 +52,7 @@ module Redirect
# action.call({}) # => [301, {'Location' => '/articles/23'}, '']
def redirect_to(url, status: 302)
headers[LOCATION] = url
self.status = status
halt(status)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/action/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def flash
# # The validation errors caused by Comments::Create are available
# # **after the redirect** in the context of Comments::Index.
def redirect_to(*args)
super
flash[ERRORS_KEY] = errors.to_a unless params.valid?
super
end

# Read errors from flash or delegate to the superclass
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,29 @@ def call(params)
end
end
end

module Users
class Show
include FullStack::Action

before :redirect_to_root
after :set_body

def call(params)
self.body = "call method shouldn't be called"
end

private

def redirect_to_root
redirect_to '/'
end

def set_body
self.body = "after callbacl shouldn't be called"
end
end
end
end

class Renderer
Expand Down Expand Up @@ -1080,6 +1103,10 @@ def initialize
get '/2', to: 'poll#step2'
post '/2', to: 'poll#step2'
end

namespace 'users' do
get '/1', to: 'users#show'
end
end

@renderer = Renderer.new
Expand Down
7 changes: 7 additions & 0 deletions test/integration/full_stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ def app
'book.author.name' => [Lotus::Validations::Error.new('book.author.name', :presence, true, nil)]
})
end

it "redirect in before action and call action method doesn't called" do
get 'users/1'

last_response.status.must_equal 302
last_response.body.must_equal 'Found' # This message is 302 status
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlfonsoUceda Can you please ensure that the body isn't send, as it's useless?

last_response.body.must_equal ''

Hint: status(302, nil)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok @jodosha ;) I'll do another PR

end
end