jsonapi-consumer 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0279cf38de5603887f2d3f092240e8e687321ca9
|
4
|
+
data.tar.gz: 7b34f561d00437e45054114cc6f1b81177e30129
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 598a4b6321e2e7d742f8d781b095fb9f9eb2ade74eadf1035b110c8cbbef244d7a34c256a6a50e15850e3d3177038b5bb0d6549ce5eb81a4239a2f2d17754833
|
7
|
+
data.tar.gz: e1068ea2f333ca27a0f536628e2c3165f2af473703a71331553d579b62994045d7d6533bc30b9f841d59c987e290a0224d6ed168b1d88fa1ced9dffbd81ae4b7
|
data/CHANGELOG.md
CHANGED
@@ -7,15 +7,13 @@ module JSONAPI::Consumer::Resource
|
|
7
7
|
|
8
8
|
self.each_association do |name, association, options|
|
9
9
|
@hash[:links] ||= {}
|
10
|
-
# unless options[:embed] == :ids
|
11
|
-
# @hash[:linked] ||= {}
|
12
|
-
# end
|
13
10
|
|
14
|
-
if association.respond_to?(:each)
|
11
|
+
if association.respond_to?(:each) or _association_type(name) == :has_many
|
15
12
|
add_links(name, association, options)
|
16
13
|
else
|
17
14
|
add_link(name, association, options)
|
18
15
|
end
|
16
|
+
@hash.delete(:links) if remove_links?
|
19
17
|
end
|
20
18
|
|
21
19
|
@hash
|
@@ -23,7 +21,7 @@ module JSONAPI::Consumer::Resource
|
|
23
21
|
|
24
22
|
def add_links(name, association, options)
|
25
23
|
@hash[:links][name] ||= []
|
26
|
-
@hash[:links][name] += association.map do |obj|
|
24
|
+
@hash[:links][name] += (association || []).map do |obj|
|
27
25
|
case obj.class
|
28
26
|
when String, Integer
|
29
27
|
obj
|
@@ -31,11 +29,6 @@ module JSONAPI::Consumer::Resource
|
|
31
29
|
obj.to_param
|
32
30
|
end
|
33
31
|
end
|
34
|
-
|
35
|
-
# unless options[:embed] == :ids
|
36
|
-
# @hash[:linked][name] ||= []
|
37
|
-
# @hash[:linked][name] += association.map { |item| item.attributes(options) }
|
38
|
-
# end
|
39
32
|
end
|
40
33
|
|
41
34
|
def add_link(name, association, options)
|
@@ -47,18 +40,24 @@ module JSONAPI::Consumer::Resource
|
|
47
40
|
else
|
48
41
|
association.to_param
|
49
42
|
end
|
50
|
-
|
51
|
-
# unless options[:embed] == :ids
|
52
|
-
# plural_name = name.to_s.pluralize.to_sym
|
53
|
-
|
54
|
-
# @hash[:linked][plural_name] ||= []
|
55
|
-
# @hash[:linked][plural_name].push association.attributes(options)
|
56
|
-
# end
|
57
43
|
end
|
58
44
|
|
59
45
|
def to_json(options={})
|
60
46
|
serializable_hash(options).to_json
|
61
47
|
end
|
62
48
|
|
49
|
+
private
|
50
|
+
|
51
|
+
def remove_links?
|
52
|
+
if persisted?
|
53
|
+
false
|
54
|
+
else # not persisted, new object
|
55
|
+
if @hash[:links].length == 0 or @hash[:links].values.flatten.empty?
|
56
|
+
true
|
57
|
+
else
|
58
|
+
false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
63
62
|
end
|
64
63
|
end
|
@@ -48,8 +48,33 @@ RSpec.describe 'Associations', 'has_many' do
|
|
48
48
|
describe 'the links payload' do
|
49
49
|
subject(:payload_hash) { user_instance.serializable_hash }
|
50
50
|
|
51
|
-
|
52
|
-
|
51
|
+
describe 'when populated' do
|
52
|
+
before do
|
53
|
+
user_instance.posts = ['1']
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'has links in output, if present' do
|
57
|
+
expect(payload_hash).to have_key(:links)
|
58
|
+
expect(payload_hash[:links]).to eql({posts: ['1']})
|
59
|
+
expect(payload_hash[:links][:posts]).to eql(["1"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'when empty' do
|
64
|
+
it 'has no links in output' do
|
65
|
+
expect(payload_hash).to_not have_key(:links)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'when persisted and empty' do
|
70
|
+
before do
|
71
|
+
allow(user_instance).to receive(:persisted?).and_return(true)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'has a links hash in the output' do
|
75
|
+
expect(payload_hash).to have_key(:links)
|
76
|
+
expect(payload_hash[:links]).to eql({posts: []})
|
77
|
+
end
|
53
78
|
end
|
54
79
|
end
|
55
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-consumer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Smestad
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|