Skip to content
Open
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
20 changes: 14 additions & 6 deletions lib/jsonapi/link_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,31 @@ 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)
"#{ base_url }#{ engine_resource_path(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?
Expand Down
6 changes: 6 additions & 0 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down