@@ -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+
344390end
0 commit comments