Skip to content

Commit e4b763f

Browse files
authored
Merge pull request #335 from senid231/334-access-to-assigned-relationship
access to assigned relationship
2 parents 8d5bbf5 + 18997e5 commit e4b763f

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ gem 'addressable', '~> 2.2'
1111
gem "codeclimate-test-reporter", group: :test, require: nil
1212

1313
group :development, :test do
14-
gem 'byebug', platforms: [:mri_20, :mri_21, :mri_22]
14+
gem 'byebug', '~> 10.0', platforms: [:mri_20, :mri_21, :mri_22]
1515
end

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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ def after_create_method
1818
class Author < TestResource
1919
end
2020

21+
class User < TestResource
22+
has_one :skill_level
23+
24+
properties :first_name, type: :string
25+
end
26+
27+
class SkillLevel < TestResource
28+
property :title, type: :string
29+
end
30+
2131
def stub_simple_creation
2232
stub_request(:post, "http://example.com/articles")
2333
.with(headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"}, body: {
@@ -341,4 +351,40 @@ def test_create_with_custom_type
341351
assert_equal '1', file.id
342352
end
343353

354+
def test_access_loaded_relationship_instance
355+
stub_request(:get, 'http://example.com/skill_levels/1')
356+
.to_return(headers: {content_type: 'application/vnd.api+json'}, body: {
357+
data: {
358+
type: 'skill_levels',
359+
id: '1',
360+
attributes: {
361+
title: 'newbie'
362+
}
363+
}
364+
}.to_json)
365+
366+
stub_request(:get, 'http://example.com/skill_levels/2')
367+
.to_return(headers: {content_type: 'application/vnd.api+json'}, body: {
368+
data: {
369+
type: 'skill_levels',
370+
id: '2',
371+
attributes: {
372+
title: 'pro'
373+
}
374+
}
375+
}.to_json)
376+
377+
skill_level = SkillLevel.find(1).first
378+
user = User.new(first_name: 'Joe', relationships: { skill_level: skill_level })
379+
380+
assert_equal ({'data'=>{'type'=>'skill_levels', 'id'=>'1'}}), user.relationships.skill_level
381+
assert_kind_of SkillLevel, user.skill_level
382+
assert_equal '1', user.skill_level.id
383+
# test that object is cached and not recreated each time
384+
assert_equal user.skill_level.object_id, user.skill_level.object_id
385+
386+
user.skill_level = SkillLevel.find(2).first
387+
assert_equal '2', user.skill_level.id
388+
end
389+
344390
end

0 commit comments

Comments
 (0)