From a1fdcb3ddb5055311ce7e3179098ad77b52a73f5 Mon Sep 17 00:00:00 2001 From: Tobias Grasse <834914+tobias-grasse@users.noreply.github.com> Date: Wed, 4 Sep 2019 08:39:35 +0200 Subject: [PATCH 1/3] Consider relative URL root configuration When Rails is deployed to a subdirectory per [the official guide instructions](https://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root), JSON:API should take this into account. Implementation detail: Switched from string concatenation (`+`) to string interpolation because `Rails.application.config.relative_url_root` might be `nil`, which would cause an error as @hlogmans [already stated](https://github.com/cerebris/jsonapi-resources/issues/473#issuecomment-153719383). Tested with: - Setting `RAILS_RELATIVE_URL_ROOT=/subdirectory` - Adding `config.relative_url_root = '/subdirectory'` in application.rb or .rb Fixes #473 --- lib/jsonapi/acts_as_resource_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jsonapi/acts_as_resource_controller.rb b/lib/jsonapi/acts_as_resource_controller.rb index b8d75ae74..032841608 100644 --- a/lib/jsonapi/acts_as_resource_controller.rb +++ b/lib/jsonapi/acts_as_resource_controller.rb @@ -152,7 +152,7 @@ def resource_serializer_klass end def base_url - @base_url ||= request.protocol + request.host_with_port + @base_url ||= "#{request.protocol}#{request.host_with_port}#{Rails.application.config.relative_url_root}" end def resource_klass_name From 4cc1e5ebbcd1be96720ca1dbec78ff1961bf0cb4 Mon Sep 17 00:00:00 2001 From: Tobias Grasse Date: Thu, 5 Sep 2019 01:04:08 +0200 Subject: [PATCH 2/3] Add test for relative root in links Signed-off-by: Tobias Grasse --- test/controllers/controller_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 7d266792f..b865c410b 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -10,6 +10,12 @@ def setup JSONAPI.configuration.always_include_to_one_linkage_data = false end + def test_links_include_relative_root + Rails.application.config.relative_url_root = '/subdir' + assert_cacheable_get :index + assert json_response['data'][0]['links']['self'].include?('/subdir') + end + def test_index assert_cacheable_get :index assert_response :success From 37fa80d8b1c741446c589e3f72ee940685e45358 Mon Sep 17 00:00:00 2001 From: Tobias Grasse Date: Thu, 5 Sep 2019 01:22:09 +0200 Subject: [PATCH 3/3] Clean up after test Test suite runs fine without this locally (ruby 2.6.3, Rails 5.2.3), but Travis CI builds all fail because subsequent tests expect a clean base URL. Signed-off-by: Tobias Grasse --- test/controllers/controller_test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index b865c410b..f71370322 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -14,6 +14,7 @@ def test_links_include_relative_root Rails.application.config.relative_url_root = '/subdir' assert_cacheable_get :index assert json_response['data'][0]['links']['self'].include?('/subdir') + Rails.application.config.relative_url_root = nil end def test_index