Skip to content

Commit ed05887

Browse files
committed
Making sure we return the right path for a rails engine that doesn't have additional namespaces
1 parent 3c06c3c commit ed05887

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

lib/jsonapi/link_builder.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ def engine_resources_path_name_from_class(klass)
9595
scopes = module_scopes_from_class(klass)[1..-1]
9696
base_path_name = scopes.map { |scope| scope.underscore }.join("_")
9797
end_path_name = klass._type.to_s
98-
"#{ base_path_name }_#{ end_path_name }_path"
98+
99+
if base_path_name.blank?
100+
"#{ end_path_name }_path"
101+
else
102+
"#{ base_path_name }_#{ end_path_name }_path"
103+
end
99104
end
100105

101106
def format_route(route)

test/fixtures/active_record.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,11 @@ class PersonResource < JSONAPI::Resource
16231623
end
16241624
end
16251625

1626+
module ApiV2Engine
1627+
class PersonResource < JSONAPI::Resource
1628+
end
1629+
end
1630+
16261631
module Legacy
16271632
class FlatPost < ActiveRecord::Base
16281633
self.table_name = "posts"

test/test_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class Engine < ::Rails::Engine
6565
end
6666
end
6767

68+
module ApiV2Engine
69+
class Engine < ::Rails::Engine
70+
isolate_namespace ApiV2Engine
71+
end
72+
end
73+
6874
# Monkeypatch ActionController::TestCase to delete the RAW_POST_DATA on subsequent calls in the same test.
6975
if Rails::VERSION::MAJOR >= 5
7076
module ClearRawPostHeader
@@ -362,6 +368,7 @@ class CatResource < JSONAPI::Resource
362368
end
363369

364370
mount MyEngine::Engine => "/boomshaka", as: :my_engine
371+
mount ApiV2Engine::Engine => "/api_v2", as: :api_v2_engine
365372
end
366373

367374
MyEngine::Engine.routes.draw do
@@ -384,6 +391,10 @@ class CatResource < JSONAPI::Resource
384391
end
385392
end
386393

394+
ApiV2Engine::Engine.routes.draw do
395+
jsonapi_resources :people
396+
end
397+
387398
# Ensure backward compatibility with Minitest 4
388399
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
389400

test/unit/serializer/link_builder_test.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def test_engine_boolean
1919
primary_resource_klass: MyEngine::Api::V1::PersonResource
2020
).engine?, "MyEngine should be considered an Engine"
2121

22+
assert JSONAPI::LinkBuilder.new(
23+
primary_resource_klass: ApiV2Engine::PersonResource
24+
).engine?, "ApiV2 shouldn't be considered an Engine"
25+
2226
refute JSONAPI::LinkBuilder.new(
2327
primary_resource_klass: Api::V1::PersonResource
2428
).engine?, "Api shouldn't be considered an Engine"
@@ -30,6 +34,11 @@ def test_engine_name
3034
primary_resource_klass: MyEngine::Api::V1::PersonResource
3135
).engine_name
3236

37+
assert_equal ApiV2Engine::Engine,
38+
JSONAPI::LinkBuilder.new(
39+
primary_resource_klass: ApiV2Engine::PersonResource
40+
).engine_name
41+
3342
assert_equal nil,
3443
JSONAPI::LinkBuilder.new(
3544
primary_resource_klass: Api::V1::PersonResource
@@ -53,6 +62,22 @@ def test_self_link_regular_app
5362
end
5463

5564
def test_self_link_with_engine_app
65+
primary_resource_klass = ApiV2Engine::PersonResource
66+
67+
config = {
68+
base_url: @base_url,
69+
route_formatter: @route_formatter,
70+
primary_resource_klass: primary_resource_klass,
71+
}
72+
73+
builder = JSONAPI::LinkBuilder.new(config)
74+
source = primary_resource_klass.new(@steve, nil)
75+
expected_link = "#{ @base_url }/api_v2/people/#{ source.id }"
76+
77+
assert_equal expected_link, builder.self_link(source)
78+
end
79+
80+
def test_self_link_with_engine_namespaced_app
5681
primary_resource_klass = MyEngine::Api::V1::PersonResource
5782

5883
config = {
@@ -98,6 +123,19 @@ def test_primary_resources_url_for_regular_app
98123
end
99124

100125
def test_primary_resources_url_for_engine
126+
config = {
127+
base_url: @base_url,
128+
route_formatter: @route_formatter,
129+
primary_resource_klass: ApiV2Engine::PersonResource
130+
}
131+
132+
builder = JSONAPI::LinkBuilder.new(config)
133+
expected_link = "#{ @base_url }/api_v2/people"
134+
135+
assert_equal expected_link, builder.primary_resources_url
136+
end
137+
138+
def test_primary_resources_url_for_namespaced_engine
101139
config = {
102140
base_url: @base_url,
103141
route_formatter: @route_formatter,
@@ -127,6 +165,22 @@ def test_relationships_self_link_for_regular_app
127165
end
128166

129167
def test_relationships_self_link_for_engine
168+
config = {
169+
base_url: @base_url,
170+
route_formatter: @route_formatter,
171+
primary_resource_klass: ApiV2Engine::PersonResource
172+
}
173+
174+
builder = JSONAPI::LinkBuilder.new(config)
175+
source = ApiV2Engine::PersonResource.new(@steve, nil)
176+
relationship = JSONAPI::Relationship::ToMany.new("posts", {})
177+
expected_link = "#{ @base_url }/api_v2/people/#{ @steve.id }/relationships/posts"
178+
179+
assert_equal expected_link,
180+
builder.relationships_self_link(source, relationship)
181+
end
182+
183+
def test_relationships_self_link_for_namespaced_engine
130184
config = {
131185
base_url: @base_url,
132186
route_formatter: @route_formatter,
@@ -159,6 +213,22 @@ def test_relationships_related_link_for_regular_app
159213
end
160214

161215
def test_relationships_related_link_for_engine
216+
config = {
217+
base_url: @base_url,
218+
route_formatter: @route_formatter,
219+
primary_resource_klass: ApiV2Engine::PersonResource
220+
}
221+
222+
builder = JSONAPI::LinkBuilder.new(config)
223+
source = ApiV2Engine::PersonResource.new(@steve, nil)
224+
relationship = JSONAPI::Relationship::ToMany.new("posts", {})
225+
expected_link = "#{ @base_url }/api_v2/people/#{ @steve.id }/posts"
226+
227+
assert_equal expected_link,
228+
builder.relationships_related_link(source, relationship)
229+
end
230+
231+
def test_relationships_related_link_for_namespaced_engine
162232
config = {
163233
base_url: @base_url,
164234
route_formatter: @route_formatter,
@@ -234,6 +304,20 @@ def test_query_link_for_regular_app_with_dasherized_scope
234304
end
235305

236306
def test_query_link_for_engine
307+
config = {
308+
base_url: @base_url,
309+
route_formatter: @route_formatter,
310+
primary_resource_klass: ApiV2Engine::PersonResource
311+
}
312+
313+
query = { page: { offset: 0, limit: 12 } }
314+
builder = JSONAPI::LinkBuilder.new(config)
315+
expected_link = "#{ @base_url }/api_v2/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
316+
317+
assert_equal expected_link, builder.query_link(query)
318+
end
319+
320+
def test_query_link_for_namespaced_engine
237321
config = {
238322
base_url: @base_url,
239323
route_formatter: @route_formatter,

0 commit comments

Comments
 (0)