Skip to content

Commit 3611780

Browse files
committed
retrieve assigned relationship record via association getter method
with caching
1 parent 43b0236 commit 3611780

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

lib/json_api_client/associations/base_association.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ def data(url)
2121
def from_result_set(result_set)
2222
result_set.to_a
2323
end
24+
25+
def load_records(data)
26+
data.map do |d|
27+
record_class = Utils.compute_type(klass, d["type"].classify)
28+
record_class.load id: d["id"]
29+
end
30+
end
2431
end
2532
end
2633
end

lib/json_api_client/associations/has_one.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ class Association < BaseAssociation
55
def from_result_set(result_set)
66
result_set.first
77
end
8+
9+
def load_records(data)
10+
record_class = Utils.compute_type(klass, data["type"].classify)
11+
record_class.load id: data["id"]
12+
end
813
end
914
end
1015
end
11-
end
16+
end

lib/json_api_client/resource.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,11 @@ def included_data_for(name, relationship_definition)
537537
def relationship_data_for(name, relationship_definition)
538538
# look in included data
539539
if relationship_definition.key?("data")
540-
return included_data_for(name, relationship_definition)
540+
if relationships.attribute_changed?(name)
541+
return relation_objects_for(name, relationship_definition)
542+
else
543+
return included_data_for(name, relationship_definition)
544+
end
541545
end
542546

543547
url = relationship_definition["links"]["related"]
@@ -548,6 +552,13 @@ def relationship_data_for(name, relationship_definition)
548552
nil
549553
end
550554

555+
def relation_objects_for(name, relationship_definition)
556+
data = relationship_definition["data"]
557+
assoc = association_for(name)
558+
return if data.nil? || assoc.nil?
559+
assoc.load_records(data)
560+
end
561+
551562
def method_missing(method, *args)
552563
relationship_definition = relationship_definition_for(method)
553564

test/unit/creation_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def test_access_loaded_relationship_instance
367367
.to_return(headers: {content_type: 'application/vnd.api+json'}, body: {
368368
data: {
369369
type: 'skill_levels',
370-
id: '1',
370+
id: '2',
371371
attributes: {
372372
title: 'pro'
373373
}
@@ -383,7 +383,7 @@ def test_access_loaded_relationship_instance
383383
# test that object is cached and not recreated each time
384384
assert_equal user.skill_level.object_id, user.skill_level.object_id
385385

386-
user.relationships.skill_level = SkillLevel.find(2).first
386+
user.skill_level = SkillLevel.find(2).first
387387
assert_equal '2', user.skill_level.id
388388
end
389389

0 commit comments

Comments
 (0)