5
5
6
6
around ( :each ) do |example |
7
7
with_temporary_database ( lambda do
8
- create_table :posts
8
+ create_table :posts do |t |
9
+ t . string :name
10
+ t . string :body
11
+ t . references :author , polymorphic : true
12
+ end
13
+
14
+ create_table :users do |t |
15
+ t . string :name
16
+ end
9
17
end ) do
10
18
# Clear cache just in case a test runs before this one
11
19
klass . instance_variable_set ( '@deserializable_cache' , { } )
12
- class Post < ActiveRecord ::Base ; end
20
+ class Post < ActiveRecord ::Base ; belongs_to :author , polymorphic : true ; end
21
+ class User < ActiveRecord ::Base ; has_many :posts ; end
13
22
example . run
14
23
end
15
24
end
@@ -21,24 +30,62 @@ class Post < ActiveRecord::Base; end
21
30
end
22
31
23
32
context 'deserializing a jsonapi document' do
24
- before ( :all ) do
25
- @payload = {
26
- 'data' => {
27
- 'id' => '1' ,
28
- 'type' => 'posts' ,
29
- 'attributes' => {
30
- 'name' => 'Name' ,
31
- 'body' => 'content'
33
+ context 'with attributes' do
34
+ before ( :all ) do
35
+ @payload = {
36
+ 'data' => {
37
+ 'id' => '1' ,
38
+ 'type' => 'posts' ,
39
+ 'attributes' => {
40
+ 'name' => 'Name' ,
41
+ 'body' => 'content'
42
+ } ,
43
+ 'relationships' => { }
32
44
}
33
45
}
34
- }
46
+ end
47
+
48
+ it 'pulls out the attributes' do
49
+ result = JSONAPI ::Rails . to_active_record_hash ( @payload , options : { } , klass : Post )
50
+ expected = { 'name' => 'Name' , 'body' => 'content' }
51
+
52
+ expect ( result ) . to eq expected
53
+ end
35
54
end
36
55
37
- it 'pulls out the attributes' do
38
- result = JSONAPI ::Rails . to_active_record_hash ( @payload , options : { } , klass : Post )
39
- expected = { 'name' => 'Name' , 'body' => 'content' }
56
+ context 'with polymorphic relationships' do
57
+ before ( :all ) do
58
+ @payload = {
59
+ 'data' => {
60
+ 'id' => '1' ,
61
+ 'type' => 'posts' ,
62
+ 'attributes' => {
63
+ 'name' => 'Name' ,
64
+ 'body' => 'content'
65
+ } ,
66
+ 'relationships' => {
67
+ 'author' => {
68
+ 'data' => {
69
+ 'id' => 1 ,
70
+ 'type' => 'users'
71
+ }
72
+ }
73
+ }
74
+ }
75
+ }
76
+ end
77
+
78
+ it 'pulls out the attributes' do
79
+ result = JSONAPI ::Rails . to_active_record_hash ( @payload , options : { } , klass : Post )
80
+ expected = {
81
+ 'name' => 'Name' ,
82
+ 'body' => 'content' ,
83
+ 'author_id' => 1 ,
84
+ 'author_type' => 'User'
85
+ }
40
86
41
- expect ( result ) . to eq expected
87
+ expect ( result ) . to eq expected
88
+ end
42
89
end
43
90
end
44
91
end
0 commit comments