-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathlinkage_spec.rb
46 lines (39 loc) · 1.23 KB
/
linkage_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'spec_helper'
describe JSONAPI::Serializable::Resource, '.linkage' do
let(:posts) { [Post.new(id: 1), Post.new(id: 2)] }
let(:user) { User.new(id: 'foo', posts: posts) }
let(:inferrer) do
Hash.new { |h, k| h[k] = Object.const_get("Serializable#{k}") }
end
it 'defaults to forcing standard linkage' do
klass = Class.new(JSONAPI::Serializable::Resource) do
type 'users'
relationship :posts, class: SerializablePost do
linkage always: true
end
end
resource = klass.new(object: user, _class: inferrer)
actual = resource.as_jsonapi[:relationships][:posts]
expected = {
data: [{ type: :posts, id: '1' },
{ type: :posts, id: '2' }]
}
expect(actual).to eq(expected)
end
it 'overrides standard linkage' do
klass = Class.new(JSONAPI::Serializable::Resource) do
type 'users'
relationship :posts, class: SerializablePost do
linkage do
[{ type: :posts, id: '5' }]
end
end
end
resource = klass.new(object: user, _class: inferrer)
actual = resource.as_jsonapi(include: [:posts])[:relationships][:posts]
expected = {
data: [{ type: :posts, id: '5' }]
}
expect(actual).to eq(expected)
end
end