Skip to content

Commit

Permalink
Add redirect_forward_or_to. (#8)
Browse files Browse the repository at this point in the history
I'm keen to explore more controller language that focuses on advance/forward or regress,
in other words movements.
E.g. a successful action moves you forward to another action, while an unsuccessful action makes you recede.

I don't have more on that right now, but I explored something like it here: excid3/rails#2 (comment)

So `redirect_forward_or_to` starts establishing that, but it's really just
syntactic sugar around `redirect_to redirect_url || fallback_url`.
  • Loading branch information
kaspth authored Jul 6, 2024
1 parent b63cced commit b0502ed
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/action_controller/stashed_redirects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ def derive_stash_redirect_url_from(url)
end
end

# Looks up a redirect URL from `params[:redirect_url]` using
# `url_from` as the protection mechanism to ensure it's a valid internal redirect.
# Syntactic sugar for redirecting to `redirect_url` with a fallback.
#
# Can be passed to `redirect_to` with a fallback:
# redirect_forward_or_to root_url # => redirect_to redirect_url || root_url
def redirect_forward_or_to(fallback_url)
redirect_to redirect_url || fallback_url
end

# Looks up a redirect URL from `params[:redirect_url]` using
# Rails' `url_from` as the protection mechanism to ensure it's a valid internal redirect.
# See the `url_from` docs: https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-url_from
#
# redirect_to redirect_url || users_url
# You probably want to use `redirect_forward_or_to`.
def redirect_url = url_from(params[:redirect_url])

DEFAULT_URL = Object.new
Expand Down
8 changes: 8 additions & 0 deletions test/action_controller/stashed_redirects_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class ActionController::StashedRedirectsTest < ActionDispatch::IntegrationTest
get new_session_url
assert_response :internal_server_error
end

test "redirect_forward_or_to" do
get forward_sessions_redirects_url, params: { redirect_url: users_url }
assert_redirected_to users_url

get forward_sessions_redirects_url
assert_redirected_to root_url
end
end

class ActionController::StashedRedirects::HooksTest < ActiveSupport::TestCase
Expand Down
4 changes: 4 additions & 0 deletions test/boot/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ class RedirectsController < ApplicationController
def create
redirect_from_stashed :sign_in
end

def forward
redirect_forward_or_to root_url
end
end
end
6 changes: 5 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ class ActionController::StashedRedirects::Application < Rails::Application
Rails.application.routes.draw do
resources :sessions
namespace :sessions do
resources :redirects
resources :redirects do
get :forward, on: :collection
end
end

resources :users

root to: "sessions#new"
end

require_relative "boot/action_controller"

0 comments on commit b0502ed

Please sign in to comment.