diff --git a/lib/jsonapi/link_builder.rb b/lib/jsonapi/link_builder.rb index 1799152fc..2381d49bd 100644 --- a/lib/jsonapi/link_builder.rb +++ b/lib/jsonapi/link_builder.rb @@ -77,14 +77,22 @@ def engine_primary_resources_url def engine_resource_path(source) resource_path_name = engine_resource_path_name_from_source(source) - engine_name.routes.url_helpers.public_send(resource_path_name, source.id) + + path_parameters = source.context.try(:[], :path_parameters) || [] + path_parameters << source.id + engine_name.routes.url_helpers.public_send(resource_path_name, *path_parameters) end def engine_resource_path_name_from_source(source) - scopes = module_scopes_from_class(source.class)[1..-1] - base_path_name = scopes.map { |scope| scope.underscore }.join("_") + scopes = scopes_from_resource_class(source.class) + base_path_name = scopes.map { |scope| scope.to_s.underscore }.join('_') end_path_name = source.class._type.to_s.singularize - [base_path_name, end_path_name, "path"].reject(&:blank?).join("_") + [base_path_name, end_path_name, 'path'].reject(&:blank?).join('_') + end + + def scopes_from_resource_class(resource_klass) + # Extension point -- allow overriding scopes in Resource definition, e.g. for nested resources. + resource_klass.custom_route_scopes || module_scopes_from_class(resource_klass)[1..-1] end def engine_resource_url(source) @@ -92,8 +100,8 @@ def engine_resource_url(source) end def engine_resources_path_name_from_class(klass) - scopes = module_scopes_from_class(klass)[1..-1] - base_path_name = scopes.map { |scope| scope.underscore }.join("_") + scopes = scopes_from_resource_class(klass) + base_path_name = scopes.map { |scope| scope.to_s.underscore }.join('_') end_path_name = klass._type.to_s if base_path_name.blank? diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index 14c34d417..54c98d922 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -169,6 +169,12 @@ def custom_links(_options) {} end + # Override this to add a custom route scope. + # e.g. to support nested resource routes. + def self.custom_route_scopes + nil + end + def preloaded_fragments # A hash of hashes @preloaded_fragments ||= Hash.new