@@ -13,6 +13,10 @@ class Specified < TestResource
1313 has_many :bars , class_name : "Owner"
1414end
1515
16+ class Shallowed < TestResource
17+ belongs_to :foo , class_name : "Property" , shallow_path : true
18+ end
19+
1620class PrefixedOwner < TestResource
1721 has_many :prefixed_properties
1822end
@@ -630,6 +634,14 @@ def test_belongs_to_path
630634 assert_equal ( "foos/%D0%99%D0%A6%D0%A3%D0%9A%D0%95%D0%9D/specifieds" , Specified . path ( { foo_id : 'ЙЦУКЕН' } ) )
631635 end
632636
637+ def test_belongs_to_shallowed_path
638+ assert_equal ( [ :foo_id ] , Shallowed . prefix_params )
639+ assert_equal "shalloweds" , Shallowed . path ( { } )
640+ assert_equal ( "foos/%{foo_id}/shalloweds" , Shallowed . path )
641+ assert_equal ( "foos/1/shalloweds" , Shallowed . path ( { foo_id : 1 } ) )
642+ assert_equal ( "foos/%D0%99%D0%A6%D0%A3%D0%9A%D0%95%D0%9D/shalloweds" , Shallowed . path ( { foo_id : 'ЙЦУКЕН' } ) )
643+ end
644+
633645 def test_find_belongs_to
634646 stub_request ( :get , "http://example.com/foos/1/specifieds" )
635647 . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
@@ -642,6 +654,30 @@ def test_find_belongs_to
642654 assert_equal ( 1 , specifieds . length )
643655 end
644656
657+ def test_find_belongs_to_shallowed
658+ stub_request ( :get , "http://example.com/foos/1/shalloweds" )
659+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
660+ data : [
661+ { id : 1 , type : "shalloweds" , attributes : { name : "nested" } }
662+ ]
663+ } . to_json )
664+
665+ stub_request ( :get , "http://example.com/shalloweds" )
666+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
667+ data : [
668+ { id : 1 , type : "shalloweds" , attributes : { name : "global" } }
669+ ]
670+ } . to_json )
671+
672+ nested_records = Shallowed . where ( foo_id : 1 ) . all
673+ assert_equal ( 1 , nested_records . length )
674+ assert_equal ( "nested" , nested_records . first . name )
675+
676+ global_records = Shallowed . all
677+ assert_equal ( 1 , global_records . length )
678+ assert_equal ( "global" , global_records . first . name )
679+ end
680+
645681 def test_can_handle_creating
646682 stub_request ( :post , "http://example.com/foos/10/specifieds" )
647683 . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
@@ -657,6 +693,28 @@ def test_can_handle_creating
657693 } )
658694 end
659695
696+ def test_can_handle_creating_shallowed
697+ stub_request ( :post , "http://example.com/foos/10/shalloweds" )
698+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
699+ data : { id : 12 , type : "shalloweds" , attributes : { name : "nested" } }
700+ } . to_json )
701+
702+ stub_request ( :post , "http://example.com/shalloweds" )
703+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
704+ data : { id : 13 , type : "shalloweds" , attributes : { name : "global" } }
705+ } . to_json )
706+
707+ Shallowed . create ( {
708+ :id => 12 ,
709+ :foo_id => 10 ,
710+ :name => "nested"
711+ } )
712+ Shallowed . create ( {
713+ :id => 13 ,
714+ :name => "global"
715+ } )
716+ end
717+
660718 def test_find_belongs_to_params_unchanged
661719 stub_request ( :get , "http://example.com/foos/1/specifieds" )
662720 . to_return ( headers : {
@@ -692,4 +750,19 @@ def test_nested_create
692750 Specified . create ( foo_id : 1 )
693751 end
694752
753+ def test_nested_create_from_scope
754+ stub_request ( :post , "http://example.com/foos/1/specifieds" )
755+ . to_return ( headers : {
756+ content_type : "application/vnd.api+json"
757+ } , body : {
758+ data : {
759+ id : 1 ,
760+ name : "Jeff Ching" ,
761+ bars : [ { id : 1 , attributes : { address : "123 Main St." } } ]
762+ }
763+ } . to_json )
764+
765+ Specified . where ( foo_id : 1 ) . create
766+ end
767+
695768end
0 commit comments