Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions lib/jsonapi/resource_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/routing_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})*/}
Expand Down
6 changes: 6 additions & 0 deletions test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'simplecov'
require 'minitest/mock'

# To run tests with coverage:
# COVERAGE=true rake test
Expand Down Expand Up @@ -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
Expand Down