Skip to content

Commit 435fa91

Browse files
committed
fix link_builder for regular app with dasherized namespace
1 parent 06d9845 commit 435fa91

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

lib/jsonapi/link_builder.rb

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

105105
unless scopes.empty?
106-
"/#{ scopes.map(&:underscore).join('/') }/"
106+
"/#{ scopes.map{ |scope| format_route(scope.to_s.underscore) }.join('/') }/"
107107
else
108108
"/"
109109
end

test/fixtures/active_record.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,13 @@ class PersonResource < JSONAPI::Resource
14421442
end
14431443
end
14441444

1445+
module DasherizedNamespace
1446+
module V1
1447+
class PersonResource < JSONAPI::Resource
1448+
end
1449+
end
1450+
end
1451+
14451452
module MyEngine
14461453
module Api
14471454
module V1
@@ -1456,6 +1463,13 @@ class PersonResource < JSONAPI::Resource
14561463
end
14571464
end
14581465
end
1466+
1467+
module DasherizedNamespace
1468+
module V1
1469+
class PersonResource < JSONAPI::Resource
1470+
end
1471+
end
1472+
end
14591473
end
14601474

14611475
module Legacy

test/test_helper.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ class CatResource < JSONAPI::Resource
248248
end
249249
end
250250

251+
namespace :dasherized_namespace, path: 'dasherized-namespace' do
252+
namespace :v1 do
253+
jsonapi_resources :people
254+
end
255+
end
256+
251257
namespace :pets do
252258
namespace :v1 do
253259
jsonapi_resources :cats
@@ -269,6 +275,12 @@ class CatResource < JSONAPI::Resource
269275
jsonapi_resources :people
270276
end
271277
end
278+
279+
namespace :dasherized_namespace, path: 'dasherized-namespace' do
280+
namespace :v1 do
281+
jsonapi_resources :people
282+
end
283+
end
272284
end
273285

274286
# Ensure backward compatibility with Minitest 4

test/unit/serializer/link_builder_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ def test_query_link_for_regular_app_with_camel_case_scope
214214
assert_equal expected_link, builder.query_link(query)
215215
end
216216

217+
def test_query_link_for_regular_app_with_dasherized_scope
218+
config = {
219+
base_url: @base_url,
220+
route_formatter: DasherizedRouteFormatter,
221+
primary_resource_klass: DasherizedNamespace::V1::PersonResource
222+
}
223+
224+
query = { page: { offset: 0, limit: 12 } }
225+
builder = JSONAPI::LinkBuilder.new(config)
226+
expected_link = "#{ @base_url }/dasherized-namespace/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
227+
228+
assert_equal expected_link, builder.query_link(query)
229+
end
230+
217231
def test_query_link_for_engine
218232
config = {
219233
base_url: @base_url,
@@ -228,6 +242,20 @@ def test_query_link_for_engine
228242
assert_equal expected_link, builder.query_link(query)
229243
end
230244

245+
def test_query_link_for_engine_with_dasherized_scope
246+
config = {
247+
base_url: @base_url,
248+
route_formatter: DasherizedRouteFormatter,
249+
primary_resource_klass: MyEngine::DasherizedNamespace::V1::PersonResource
250+
}
251+
252+
query = { page: { offset: 0, limit: 12 } }
253+
builder = JSONAPI::LinkBuilder.new(config)
254+
expected_link = "#{ @base_url }/boomshaka/dasherized-namespace/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
255+
256+
assert_equal expected_link, builder.query_link(query)
257+
end
258+
231259
def test_query_link_for_engine_with_camel_case_scope
232260
config = {
233261
base_url: @base_url,

0 commit comments

Comments
 (0)