From 4015488b90eb0ad7c87360b29908986d82966f28 Mon Sep 17 00:00:00 2001 From: Kostas Diamantis Date: Mon, 13 Jun 2016 21:46:43 +0300 Subject: [PATCH] Add missing support of Rails.application.config.action_controller.relative_url_root (#4146) * Add Devise::FailureApp#{relative_url_root, relative_url_root?} Also support missing action_controller.relative_url_root configuration. * Dry assignment of relative_url_root Also this commit adds support for Rails.application.config.action_controller.relative_url_root --- lib/devise/failure_app.rb | 26 ++++++++++++++++---------- test/failure_app_test.rb | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/devise/failure_app.rb b/lib/devise/failure_app.rb index 2353e43a..d3696867 100644 --- a/lib/devise/failure_app.rb +++ b/lib/devise/failure_app.rb @@ -50,13 +50,11 @@ module Devise end def recall - config = Rails.application.config - - header_info = if config.try(:relative_url_root) - base_path = Pathname.new(config.relative_url_root) + header_info = if relative_url_root? + base_path = Pathname.new(relative_url_root) full_path = Pathname.new(attempted_path) - { "SCRIPT_NAME" => config.relative_url_root, + { "SCRIPT_NAME" => relative_url_root, "PATH_INFO" => '/' + full_path.relative_path_from(base_path).to_s } else { "PATH_INFO" => attempted_path } @@ -144,11 +142,7 @@ module Devise opts[:format] = request_format unless skip_format? - config = Rails.application.config - - if config.respond_to?(:relative_url_root) && config.relative_url_root.present? - opts[:script_name] = config.relative_url_root - end + opts[:script_name] = relative_url_root if relative_url_root? router_name = Devise.mappings[scope].router_name || Devise.available_router_name context = send(router_name) @@ -252,5 +246,17 @@ module Devise def request_format @request_format ||= request.format.try(:ref) end + + def relative_url_root + @relative_url_root ||= begin + config = Rails.application.config + + config.try(:relative_url_root) || config.action_controller.try(:relative_url_root) + end + end + + def relative_url_root? + relative_url_root.present? + end end end diff --git a/test/failure_app_test.rb b/test/failure_app_test.rb index dd400d75..ee6d19fb 100644 --- a/test/failure_app_test.rb +++ b/test/failure_app_test.rb @@ -131,6 +131,24 @@ class FailureTest < ActiveSupport::TestCase end end + if Rails.application.config.action_controller.respond_to?(:relative_url_root) + test "returns to the default redirect location considering action_controller's relative url root" do + swap Rails.application.config.action_controller, relative_url_root: "/sample" do + call_failure + assert_equal 302, @response.first + assert_equal 'http://test.host/sample/users/sign_in', @response.second['Location'] + end + end + + test "returns to the default redirect location considering action_controller's relative url root and subdomain" do + swap Rails.application.config.action_controller, relative_url_root: "/sample" do + call_failure('warden.options' => { scope: :subdomain_user }) + assert_equal 302, @response.first + assert_equal 'http://sub.test.host/sample/subdomain_users/sign_in', @response.second['Location'] + end + end + end + test 'uses the proxy failure message as symbol' do call_failure('warden' => OpenStruct.new(message: :invalid)) assert_equal 'Invalid Email or password.', @request.flash[:alert]