Skip to content

Commit df16278

Browse files
committed
Merge pull request #703 from corthmann/master
Fix dasherized paths for scoped routes
2 parents 8ac47d2 + f4dba0f commit df16278

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

lib/jsonapi/link_builder.rb

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

108108
unless scopes.empty?
109-
"/#{ scopes.map(&:underscore).join('/') }/"
109+
"/#{ scopes.map{ |scope| format_route(scope.to_s.underscore) }.join('/') }/"
110110
else
111111
"/"
112112
end

test/fixtures/active_record.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,13 @@ class PersonResource < JSONAPI::Resource
15591559
end
15601560
end
15611561

1562+
module DasherizedNamespace
1563+
module V1
1564+
class PersonResource < JSONAPI::Resource
1565+
end
1566+
end
1567+
end
1568+
15621569
module MyEngine
15631570
module Api
15641571
module V1
@@ -1573,6 +1580,13 @@ class PersonResource < JSONAPI::Resource
15731580
end
15741581
end
15751582
end
1583+
1584+
module DasherizedNamespace
1585+
module V1
1586+
class PersonResource < JSONAPI::Resource
1587+
end
1588+
end
1589+
end
15761590
end
15771591

15781592
module Legacy

test/test_helper.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ class CatResource < JSONAPI::Resource
374374
end
375375
end
376376

377+
namespace :dasherized_namespace, path: 'dasherized-namespace' do
378+
namespace :v1 do
379+
jsonapi_resources :people
380+
end
381+
end
382+
377383
namespace :pets do
378384
namespace :v1 do
379385
jsonapi_resources :cats
@@ -395,6 +401,12 @@ class CatResource < JSONAPI::Resource
395401
jsonapi_resources :people
396402
end
397403
end
404+
405+
namespace :dasherized_namespace, path: 'dasherized-namespace' do
406+
namespace :v1 do
407+
jsonapi_resources :people
408+
end
409+
end
398410
end
399411

400412
# Ensure backward compatibility with Minitest 4

test/unit/serializer/link_builder_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
class LinkBuilderTest < ActionDispatch::IntegrationTest
66
def setup
7+
# the route format is being set directly in test_helper and is being set differently depending on
8+
# the order in which the namespaces get loaded. in order to prevent random test seeds to fail we need to set the
9+
# default configuration in the test 'setup'.
10+
JSONAPI.configuration.route_format = :underscored_route
11+
712
@base_url = "http://example.com"
813
@route_formatter = JSONAPI.configuration.route_formatter
914
@steve = Person.create(name: "Steve Rogers", date_joined: "1941-03-01")
@@ -214,6 +219,20 @@ def test_query_link_for_regular_app_with_camel_case_scope
214219
assert_equal expected_link, builder.query_link(query)
215220
end
216221

222+
def test_query_link_for_regular_app_with_dasherized_scope
223+
config = {
224+
base_url: @base_url,
225+
route_formatter: DasherizedRouteFormatter,
226+
primary_resource_klass: DasherizedNamespace::V1::PersonResource
227+
}
228+
229+
query = { page: { offset: 0, limit: 12 } }
230+
builder = JSONAPI::LinkBuilder.new(config)
231+
expected_link = "#{ @base_url }/dasherized-namespace/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
232+
233+
assert_equal expected_link, builder.query_link(query)
234+
end
235+
217236
def test_query_link_for_engine
218237
config = {
219238
base_url: @base_url,
@@ -228,6 +247,20 @@ def test_query_link_for_engine
228247
assert_equal expected_link, builder.query_link(query)
229248
end
230249

250+
def test_query_link_for_engine_with_dasherized_scope
251+
config = {
252+
base_url: @base_url,
253+
route_formatter: DasherizedRouteFormatter,
254+
primary_resource_klass: MyEngine::DasherizedNamespace::V1::PersonResource
255+
}
256+
257+
query = { page: { offset: 0, limit: 12 } }
258+
builder = JSONAPI::LinkBuilder.new(config)
259+
expected_link = "#{ @base_url }/boomshaka/dasherized-namespace/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
260+
261+
assert_equal expected_link, builder.query_link(query)
262+
end
263+
231264
def test_query_link_for_engine_with_camel_case_scope
232265
config = {
233266
base_url: @base_url,

0 commit comments

Comments
 (0)