jsonapi-consumer 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ebd6ed496bf4e33e58103e89a8ea1f8ec65e49c
4
- data.tar.gz: 4de1a846349a7e3606f45573fa70dd8c2b60bbdd
3
+ metadata.gz: 0279cf38de5603887f2d3f092240e8e687321ca9
4
+ data.tar.gz: 7b34f561d00437e45054114cc6f1b81177e30129
5
5
  SHA512:
6
- metadata.gz: f90b5cf975914fdc9dced9176b48e8705f98c3ee8b423c653629aa76513e11b3615ed7e771f4bd54c58416198ad5073198b955a9bd8e379672c5fde8b5fa9ec6
7
- data.tar.gz: 25da9ee3afd9b759a001834fb4989aeb73c1e6a8c0cfc59cecd51bd09fa1a17ac33350618db264089108a9fa7cacd68aef81f2a6f5c79fa0e602aab0cd5bb32b
6
+ metadata.gz: 598a4b6321e2e7d742f8d781b095fb9f9eb2ade74eadf1035b110c8cbbef244d7a34c256a6a50e15850e3d3177038b5bb0d6549ce5eb81a4239a2f2d17754833
7
+ data.tar.gz: e1068ea2f333ca27a0f536628e2c3165f2af473703a71331553d579b62994045d7d6533bc30b9f841d59c987e290a0224d6ed168b1d88fa1ced9dffbd81ae4b7
data/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ 0.1.1 / 2015-01-23
3
+ ==================
4
+
5
+ * Add internal helper #remove_links? for association payloads
6
+ * Do not send `links: {}` if new object, and empty associations
7
+
2
8
  0.1.0 / 2014-12-03
3
9
  ==================
4
10
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Jsonapi
2
2
  module Consumer
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  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
- it 'has links in output' do
52
- expect(payload_hash).to have_key(:links)
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.0
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: 2014-12-03 00:00:00.000000000 Z
11
+ date: 2015-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel