restpack_serializer 0.4.7 → 0.4.8

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: 3daa5bd16f4c61dc8e0e46a1aa91234ea6eee250
4
- data.tar.gz: 0ccaa8526f5811905ca6aee4c6c762fedd2576e9
3
+ metadata.gz: aa7cfdbe2a9599b02355d695b51fb1ba6bd50bed
4
+ data.tar.gz: aa3d9b0c277baf98f13fb7ab9c798a445c903a7a
5
5
  SHA512:
6
- metadata.gz: ac15ac3b387840b892689ccb93047f7356bf0e65a493228efde51916a1c67f4c196d59852b26519fa01975b8ab75ea6ee80e20f3e38ba532a4563c92afbd106c
7
- data.tar.gz: 6cb929625f66ff9ae579b6d28d46a9c75d771a59f9b1f16681aa7b7b097a3d400d3c93931be4d4f54ea99d6918a3cc429b1643956fa5eec0977aed137f8beb65
6
+ metadata.gz: e9adc45be506cf71e5d2edb1d71dd09e5d92132b6f2252d0894ac47537096840bf8b9b7cc77551ef75445eeaefdfb99314ebc075e5dc706c4322ae2136e62e6a
7
+ data.tar.gz: ba50400c0c244ea96efcd89988026c58f4f9c11cdae1a53b0c0ea691243218b4d78181710f1fc3ee43a1a03e3db2caf2bbc713a12ff01673ce036d73ec8f9752
@@ -28,7 +28,8 @@ module RestPack::Serializer::Paging
28
28
 
29
29
  side_load_data = side_loads(page, options)
30
30
  result[:meta].merge!(side_load_data[:meta] || {})
31
- result.merge side_load_data.except(:meta)
31
+ result = result.merge side_load_data.except(:meta)
32
+ inject_to_many_links result
32
33
  end
33
34
 
34
35
  private
@@ -68,5 +69,32 @@ module RestPack::Serializer::Paging
68
69
  url += '?' + params.join('&') if params.any?
69
70
  url
70
71
  end
72
+
73
+ def inject_to_many_links(result) #TODO: GJ: extract this into a result class and refactor
74
+ keys = result.keys - [:meta, :links]
75
+
76
+ keys.each do |key|
77
+ result[key].each do |item|
78
+ if item[:links]
79
+ item[:links].each do |link_key, link_value|
80
+ unless link_value.is_a? Array
81
+ plural_linked_key = "#{link_key}s".to_sym
82
+
83
+ if result[plural_linked_key]
84
+ linked_resource = result[plural_linked_key].find { |i| i[:id] == link_value }
85
+ if linked_resource
86
+ linked_resource[:links] ||= {}
87
+ linked_resource[:links][key] ||= []
88
+ linked_resource[:links][key] << item[:id]
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ result
98
+ end
71
99
  end
72
100
  end
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.4.7'
3
+ VERSION = '0.4.8'
4
4
  end
5
5
  end
@@ -112,6 +112,19 @@ describe RestPack::Serializer::Paging do
112
112
  page[:meta][:songs][:next_href].should == '/songs.json?page=2&include=albums'
113
113
  end
114
114
 
115
+ it "includes links between documents" do
116
+ song = page[:songs].first
117
+ song_model = MyApp::Song.find(song[:id])
118
+ song[:links][:album].should == song_model.album_id.to_s
119
+ song[:links][:artist].should == song_model.artist_id.to_s
120
+
121
+ album = page[:albums].first
122
+ album_model = MyApp::Album.find(album[:id])
123
+
124
+ album[:links][:artist].should == album_model.artist_id.to_s
125
+ album[:links][:songs].should == page[:songs].map { |song| song[:id] }
126
+ end
127
+
115
128
  context "with includes as comma delimited string" do
116
129
  let(:params) { { include: "albums,artists" } }
117
130
  it "includes side-loaded models" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restpack_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-07 00:00:00.000000000 Z
11
+ date: 2013-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord