Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirect_back functionality #1168

Merged
merged 3 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions spec/lucky/action_redirect_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ describe Lucky::Action do
should_redirect(action, to: "/somewhere", status: 301)
end

it "redirects back" do
request = build_request("POST")
request.headers["Referer"] = "https://www.example.com/coming/from"
action = RedirectAction.new(build_context(request), params)
action.redirect_back fallback: "/fallback"
should_redirect(action, to: "https://www.example.com/coming/from", status: 302)

action = RedirectAction.new(build_context, params)
action.redirect_back fallback: "/fallback"
should_redirect(action, to: "/fallback", status: 302)

action = RedirectAction.new(build_context, params)
action.redirect_back fallback: RedirectAction.route
should_redirect(action, to: RedirectAction.path, status: 302)

action = RedirectAction.new(build_context, params)
action.redirect_back fallback: RedirectAction
should_redirect(action, to: RedirectAction.path, status: 302)

action = RedirectAction.new(build_context, params)
action.redirect_back fallback: RedirectAction, status: 301
should_redirect(action, to: RedirectAction.path, status: 301)
end

it "turbolinks redirects after a XHR POST form submission" do
request = build_request("POST")
request.headers["Accept"] = "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"
Expand Down
40 changes: 40 additions & 0 deletions src/lucky/redirectable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,46 @@
# method that takes a `String`. However, it's recommended you pass a
# `Lucky::Action` class if possible because it guarantees runtime safety.
module Lucky::Redirectable
# Redirect back with a `Lucky::Action` fallback
#
# ```crystal
# redirect_back fallback: Users::Index
# ```
def redirect_back(fallback : Lucky::Action.class, status = 302)
redirect_back fallback.route, status
end

# Redirect back with a `Lucky::RouteHelper` fallback
#
# ```crystal
# redirect_back fallback: Users::Show.with(user.id)
# ```
def redirect_back(fallback : Lucky::RouteHelper, status = 302)
redirect_back fallback.path, status
end

# Redirects the browser to the page that issued the request (the referrer)
# if possible, otherwise redirects to the provided default fallback
# location.
#
# The referrer information is pulled from the 'Referer' header on
# the request. This is an optional header, and if the request
# is missing this header the *fallback* will be used.
#
# ```crystal
# redirect_back fallback: "/users"
# ```
#
# A redirect status can be specified
#
# ```crystal
# redirect_back fallback: "/home", status: 301
# ```
def redirect_back(fallback : String, status = 302)
referer = request.headers["Referer"]?
redirect to: (referer || fallback), status: status
end

# Redirect using a `Lucky::RouteHelper`
#
# ```crystal
Expand Down