diff --git a/lib/jsonapi/resource_serializer.rb b/lib/jsonapi/resource_serializer.rb index 130a7ca80..923d3a69a 100644 --- a/lib/jsonapi/resource_serializer.rb +++ b/lib/jsonapi/resource_serializer.rb @@ -20,6 +20,7 @@ def initialize(primary_resource_klass, options = {}) @include = options.fetch(:include, []) @include_directives = options[:include_directives] @key_formatter = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter) + @scope_id = options.fetch(:scope_id, nil) @url_generator = generate_link_builder(primary_resource_klass, options) @always_include_to_one_linkage_data = options.fetch(:always_include_to_one_linkage_data, JSONAPI.configuration.always_include_to_one_linkage_data) @@ -197,6 +198,15 @@ def relationship_data(source, include_directives) end end + def formatted_module_path(source) + if source.class.name =~ /::[^:]+\Z/ + path = (@route_formatter.format($`).freeze.gsub('::', '/') + '/').downcase + @scope_id ? "#{path}#{@scope_id}/" : path + else + '' + end + end + def relationship_links(source) links = {} links[:self] = url_generator.self_link(source) diff --git a/lib/jsonapi/routing_ext.rb b/lib/jsonapi/routing_ext.rb index bda8e5b26..80da24faf 100644 --- a/lib/jsonapi/routing_ext.rb +++ b/lib/jsonapi/routing_ext.rb @@ -66,7 +66,7 @@ def jsonapi_resources(*resources, &_block) options[:param] = :id - options[:path] = format_route(@resource_type) + options[:path] = format_route(options[:path] || @resource_type) if res.resource_key_type == :uuid options[:constraints] = {id: /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(,[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})*/} diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index 840dd8f52..c0ec63f8b 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -910,4 +910,10 @@ def test_sort_parameter_not_allowed ensure JSONAPI.configuration.allow_sort = true end + + def test_scoped_resources + get '/api/v1/123/people' + assert_equal 200, status + assert_hash_equals('http://www.example.com/api/v1/123/people/1', json_response['data'].first['links']['self']) + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 961b36aee..415d30338 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,5 @@ require 'simplecov' +require 'minitest/mock' # To run tests with coverage: # COVERAGE=true rake test @@ -136,6 +137,10 @@ class CatResource < JSONAPI::Resource namespace :api do namespace :v1 do + scope ":section_id" do + jsonapi_resources :people + end + jsonapi_resources :people jsonapi_resources :comments jsonapi_resources :tags