Skip to content

Commit 1089a45

Browse files
committed
added optional route formatting feature
1 parent 3a02a4d commit 1089a45

4 files changed

Lines changed: 62 additions & 1 deletion

File tree

lib/jsonapi/link_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def formatted_module_path_from_class(klass)
115115
scopes = module_scopes_from_class(klass)
116116

117117
unless scopes.empty?
118-
"/#{ scopes.map{ |scope| format_route(scope.to_s.underscore) }.join('/') }/"
118+
"/#{ scopes.map{ |scope| format_route(scope.to_s.underscore) }.compact.join('/') }/"
119119
else
120120
"/"
121121
end

test/fixtures/active_record.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,13 @@ class PersonResource < JSONAPI::Resource
19291929
end
19301930
end
19311931

1932+
module OptionalNamespace
1933+
module V1
1934+
class PersonResource < JSONAPI::Resource
1935+
end
1936+
end
1937+
end
1938+
19321939
module MyEngine
19331940
module Api
19341941
module V1
@@ -1950,6 +1957,13 @@ class PersonResource < JSONAPI::Resource
19501957
end
19511958
end
19521959
end
1960+
1961+
module OptionalNamespace
1962+
module V1
1963+
class PersonResource < JSONAPI::Resource
1964+
end
1965+
end
1966+
end
19531967
end
19541968

19551969
module ApiV2Engine

test/test_helper.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ class CatResource < JSONAPI::Resource
421421
jsonapi_resources :people
422422
end
423423
end
424+
425+
namespace :optional_namespace, path: 'optional_namespace' do
426+
namespace :v1, path: '' do
427+
jsonapi_resources :people
428+
end
429+
end
424430
end
425431

426432
ApiV2Engine::Engine.routes.draw do
@@ -670,3 +676,16 @@ def unformat(value)
670676
end
671677
end
672678
end
679+
680+
class OptionalRouteFormatter < JSONAPI::RouteFormatter
681+
class << self
682+
def format(route)
683+
return if route == 'v1'
684+
super
685+
end
686+
687+
def unformat(formatted_route)
688+
super
689+
end
690+
end
691+
end

test/unit/serializer/link_builder_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,20 @@ def test_query_link_for_regular_app_with_dasherized_scope
302302
assert_equal expected_link, builder.query_link(query)
303303
end
304304

305+
def test_query_link_for_regular_app_with_optional_scope
306+
config = {
307+
base_url: @base_url,
308+
route_formatter: OptionalRouteFormatter,
309+
primary_resource_klass: OptionalNamespace::V1::PersonResource
310+
}
311+
312+
query = { page: { offset: 0, limit: 12 } }
313+
builder = JSONAPI::LinkBuilder.new(config)
314+
expected_link = "#{ @base_url }/optional_namespace/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
315+
316+
assert_equal expected_link, builder.query_link(query)
317+
end
318+
305319
def test_query_link_for_engine
306320
config = {
307321
base_url: @base_url,
@@ -344,6 +358,20 @@ def test_query_link_for_engine_with_dasherized_scope
344358
assert_equal expected_link, builder.query_link(query)
345359
end
346360

361+
def test_query_link_for_engine_with_optional_scope
362+
config = {
363+
base_url: @base_url,
364+
route_formatter: OptionalRouteFormatter,
365+
primary_resource_klass: MyEngine::OptionalNamespace::V1::PersonResource
366+
}
367+
368+
query = { page: { offset: 0, limit: 12 } }
369+
builder = JSONAPI::LinkBuilder.new(config)
370+
expected_link = "#{ @base_url }/boomshaka/optional_namespace/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
371+
372+
assert_equal expected_link, builder.query_link(query)
373+
end
374+
347375
def test_query_link_for_engine_with_camel_case_scope
348376
config = {
349377
base_url: @base_url,

0 commit comments

Comments
 (0)