Skip to content

Commit 3c8c6e4

Browse files
committed
railsy result
1 parent b96211d commit 3c8c6e4

File tree

5 files changed

+76
-31
lines changed

5 files changed

+76
-31
lines changed

lib/jsonapi/rails/deserializable_resource.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Rails
1818
# - allow custom deserializable_classes?
1919
# - then this gem would just be a very light weight wrapper around
2020
# jsonapi/deserializable
21-
class DeserializableResource < JSONAPI::Deserializable::Resource
21+
class DeserializableResource
2222
require_relative 'deserializable_resource/builder'
2323

2424
class << self
@@ -66,7 +66,7 @@ def to_hash
6666
type = _hash['data']['type']
6767
klass = self.class.deserializable_class(type, _klass)
6868

69-
self.class[klass].call(_hash)
69+
self.class[klass].call(_hash).with_indifferent_access
7070
end
7171
end
7272
end

lib/jsonapi/rails/deserializable_resource/builder.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Builder
88

99
def for_class(klass)
1010
builder = self
11-
Class.new(JSONAPI::Rails::DeserializableResource) do
11+
Class.new(JSONAPI::Deserializable::Resource) do
1212
# All Attributes
1313
builder.define_attributes(self, klass)
1414

@@ -40,7 +40,7 @@ def define_associations(deserializable, klass)
4040
else
4141
has_one name do |rel|
4242
field "#{name}_id" => rel['data'] && rel['data']['id']
43-
field "#{name}_type" => rel['data'] && rel['data']['type']
43+
field "#{name}_type" => rel['data'] && rel['data']['type'].classify
4444
end
4545
end
4646
end

spec/integration/active_record/deserializable_resource_spec.rb

+62-15
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55

66
around(:each) do |example|
77
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
917
end) do
1018
# Clear cache just in case a test runs before this one
1119
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
1322
example.run
1423
end
1524
end
@@ -21,24 +30,62 @@ class Post < ActiveRecord::Base; end
2130
end
2231

2332
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' => {}
3244
}
3345
}
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
3554
end
3655

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+
}
4086

41-
expect(result).to eq expected
87+
expect(result).to eq expected
88+
end
4289
end
4390
end
4491
end

spec/unit/jsonapi/rails/deserializable_resource/builder_spec.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
let(:klass) { JSONAPI::Rails::DeserializableResource::Builder }
44

55
describe '.for_class' do
6-
it 'can be instantiated' do
6+
it 'returns a DeserializableResource' do
77
with_temporary_database(lambda do
88
create_table :posts
99
end) do
1010
class Post < ActiveRecord::Base; end
1111
deserializer = klass.for_class(Post)
1212

13-
expect(deserializer.new({})).to be_a_kind_of JSONAPI::Rails::DeserializableResource
13+
dummy_payload = {
14+
'data' => {
15+
'id' => '1',
16+
'type' => 'posts',
17+
'attributes' => {}
18+
}
19+
}
20+
21+
expect(deserializer.new(dummy_payload)).to be_a_kind_of JSONAPI::Deserializable::Resource
1422
end
1523
end
1624

spec/unit/jsonapi/rails/deserializable_resource_spec.rb

-10
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ class Post < ActiveRecord::Base; end
5454
end
5555
end
5656

57-
describe '.new' do
58-
it 'caches generated classes' do
59-
60-
end
61-
end
62-
63-
describe '#to_h' do
64-
65-
end
66-
6757
context 'options' do
6858
context 'polymorphic' do
6959

0 commit comments

Comments
 (0)