Skip to content

Commit c95bb94

Browse files
sebasjimenez10Sebastian Jimenez V
authored and
Sebastian Jimenez V
committed
Use the associations to lookup relationship class
1 parent 462e59e commit c95bb94

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/json_api_client/utils.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ def self.compute_type(klass, type_name)
77
# the type_name is an absolute reference.
88
return type_name.constantize if type_name.match(/^::/)
99

10+
# Check the klass association definitions
11+
association_klass_match = klass.associations.find { |a| a.attr_name.to_s.singularize == type_name.underscore }
12+
association_klass = association_klass_match.options[:class] if association_klass_match
13+
return association_klass if association_klass
14+
1015
# Build a list of candidates to search for
1116
candidates = []
1217
klass.name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }

test/unit/association_test.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ class Employee < TestResource
8989
has_one :chief, klass: 'Employee'
9090
end
9191

92+
module CrossNamespaceTwo
93+
class Nail < TestResource
94+
property :size
95+
end
96+
end
97+
98+
module CrossNamespaceOne
99+
class Hammer < TestResource
100+
property :brand
101+
102+
has_many :nails, class: CrossNamespaceTwo::Nail
103+
end
104+
end
105+
92106
class AssociationTest < MiniTest::Test
93107

94108
def test_default_properties_no_changes
@@ -1070,4 +1084,62 @@ def test_does_not_load_include_from_dataset
10701084
assert_nil(records.first.chief)
10711085
end
10721086

1087+
def test_cross_namespace_resource_references
1088+
stub_request(:get, 'http://example.com/hammers?include=nails')
1089+
.to_return(
1090+
headers: {
1091+
content_type: 'application/vnd.api+json'
1092+
}, body: {
1093+
data: [
1094+
{
1095+
id: '1',
1096+
type: 'hammers',
1097+
attributes: {
1098+
brand: 'Hardware Store'
1099+
},
1100+
relationships: {
1101+
nails: { data: [{id: '2', type: 'nails'}] }
1102+
}
1103+
},
1104+
{
1105+
id: '2',
1106+
type: 'hammers',
1107+
attributes: {
1108+
brand: 'Hardware Store'
1109+
},
1110+
relationships: {
1111+
nails: { data: [{id: '3', type: 'nails'}] }
1112+
}
1113+
}
1114+
],
1115+
included: [
1116+
{
1117+
id: '2',
1118+
type: 'nails',
1119+
attributes: {
1120+
size: 10
1121+
}
1122+
},
1123+
{
1124+
id: '3',
1125+
type: 'nails',
1126+
attributes: {
1127+
size: 8
1128+
}
1129+
}
1130+
]
1131+
}.to_json)
1132+
1133+
records = CrossNamespaceOne::Hammer.includes(:nails).to_a
1134+
1135+
assert_equal(2, records.size)
1136+
assert_equal('1', records.first.id)
1137+
assert_equal('2', records.second.id)
1138+
assert_equal('2', records.first.nails.first.id)
1139+
assert_equal('3', records.second.nails.first.id)
1140+
1141+
assert_equal(1, records.first.nails.size)
1142+
assert_equal(1, records.second.nails.size)
1143+
end
1144+
10731145
end

0 commit comments

Comments
 (0)