Skip to content

Commit 71a6e3f

Browse files
nathanpalmerlgebhardt
authored andcommitted
Making sure we return the right path for a rails engine that doesn't have additional namespaces
(cherry picked from commit ed05887)
1 parent 3962e38 commit 71a6e3f

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
@@ -1652,6 +1652,11 @@ class PersonResource < JSONAPI::Resource
16521652
end
16531653
end
16541654

1655+
module ApiV2Engine
1656+
class PersonResource < JSONAPI::Resource
1657+
end
1658+
end
1659+
16551660
module Legacy
16561661
class FlatPost < ActiveRecord::Base
16571662
self.table_name = "posts"

test/test_helper.rb

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

70+
module ApiV2Engine
71+
class Engine < ::Rails::Engine
72+
isolate_namespace ApiV2Engine
73+
end
74+
end
75+
7076
# Patch RAILS 4.0 to not use millisecond precision
7177
if Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR < 1
7278
module ActiveSupport
@@ -389,6 +395,7 @@ class CatResource < JSONAPI::Resource
389395
end
390396

391397
mount MyEngine::Engine => "/boomshaka", as: :my_engine
398+
mount ApiV2Engine::Engine => "/api_v2", as: :api_v2_engine
392399
end
393400

394401
MyEngine::Engine.routes.draw do
@@ -411,6 +418,10 @@ class CatResource < JSONAPI::Resource
411418
end
412419
end
413420

421+
ApiV2Engine::Engine.routes.draw do
422+
jsonapi_resources :people
423+
end
424+
414425
# Ensure backward compatibility with Minitest 4
415426
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
416427

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)