restpack_serializer 0.4.6 → 0.4.7

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: d02fa321367307ada522ac8c84c68895ad53187b
4
- data.tar.gz: 74d285e96499587df39af7183da04b5578f5a736
3
+ metadata.gz: 3daa5bd16f4c61dc8e0e46a1aa91234ea6eee250
4
+ data.tar.gz: 0ccaa8526f5811905ca6aee4c6c762fedd2576e9
5
5
  SHA512:
6
- metadata.gz: c68fe0d43e8bb85c3743f5963c5526e33ac3f8b1914457056464dc69b1ce08edfd02743b74d0974b15d388ca01a89e343db14f1d4d302ab51890f9803e1fde05
7
- data.tar.gz: 683ebf1933b2cdd9cb55148290d83b00ffac99b9bb5cd7a7178641b6581bd7e8df0dc6456c034667862c78bb12e1525533ede864393c34b5607cc25dc8be5425
6
+ metadata.gz: ac15ac3b387840b892689ccb93047f7356bf0e65a493228efde51916a1c67f4c196d59852b26519fa01975b8ab75ea6ee80e20f3e38ba532a4563c92afbd106c
7
+ data.tar.gz: 6cb929625f66ff9ae579b6d28d46a9c75d771a59f9b1f16681aa7b7b097a3d400d3c93931be4d4f54ea99d6918a3cc429b1643956fa5eec0977aed137f8beb65
data/README.md CHANGED
@@ -120,7 +120,7 @@ http://restpack-serializer-sample.herokuapp.com/songs.json?page=2&page_size=3 yi
120
120
  "page": 2,
121
121
  "page_size": 3,
122
122
  "count": 42,
123
- "includes": [],
123
+ "include": [],
124
124
  "page_count": 14,
125
125
  "previous_page": 1,
126
126
  "next_page": 3,
@@ -159,7 +159,7 @@ class AlbumSerializer
159
159
  end
160
160
  ```
161
161
 
162
- In this example, we are allowing related `songs` and `artists` to be included in API responses. Side-loads can be specifed by using the `includes` parameter:
162
+ In this example, we are allowing related `songs` and `artists` to be included in API responses. Side-loads can be specifed by using the `include` parameter:
163
163
 
164
164
  #### No side-loads
165
165
 
@@ -167,7 +167,7 @@ In this example, we are allowing related `songs` and `artists` to be included in
167
167
 
168
168
  #### Side-load related Artists
169
169
 
170
- * http://restpack-serializer-sample.herokuapp.com/albums.json?includes=artists
170
+ * http://restpack-serializer-sample.herokuapp.com/albums.json?include=artists
171
171
 
172
172
  which yields:
173
173
 
@@ -216,7 +216,7 @@ which yields:
216
216
  "page": 1,
217
217
  "page_size": 10,
218
218
  "count": 4,
219
- "includes": [
219
+ "include": [
220
220
  "artists"
221
221
  ],
222
222
  "page_count": 1,
@@ -269,7 +269,7 @@ which yields:
269
269
 
270
270
  #### Side-load related Songs
271
271
 
272
- * http://restpack-serializer-sample.herokuapp.com/albums.json?includes=songs
272
+ * http://restpack-serializer-sample.herokuapp.com/albums.json?include=songs
273
273
 
274
274
  An album `:has_many` songs, so the side-loaded songs are paged. The `meta.songs` includes `previous_href` and `next_href` which point to the previous and next page of this side-loaded data. These URLs take the form:
275
275
 
@@ -277,11 +277,11 @@ An album `:has_many` songs, so the side-loaded songs are paged. The `meta.songs`
277
277
 
278
278
  #### Side-load related Artists and Songs
279
279
 
280
- * http://restpack-serializer-sample.herokuapp.com/albums.json?includes=artists,songs
280
+ * http://restpack-serializer-sample.herokuapp.com/albums.json?include=artists,songs
281
281
 
282
282
  ## Filtering
283
283
 
284
- Simple filtering based on primary and foreign keys is possible:
284
+ Simple filtering based on primary and foreign keys is supported by default:
285
285
 
286
286
  #### By primary key:
287
287
 
@@ -293,6 +293,19 @@ Simple filtering based on primary and foreign keys is possible:
293
293
  * http://restpack-serializer-sample.herokuapp.com/albums.json?artist_id=1
294
294
  * http://restpack-serializer-sample.herokuapp.com/albums.json?artist_ids=2,3
295
295
 
296
+ #### Custom filters:
297
+
298
+ Custom filters can be defined with the `can_filter_by` option:
299
+
300
+ ```ruby
301
+ class Account
302
+ include RestPack::Serializer
303
+ attributes :id, :application_id, :created_by, :name, :href
304
+
305
+ can_filter_by :application_id
306
+ end
307
+ ```
308
+
296
309
  Side-loading is available when filtering:
297
310
 
298
- * http://restpack-serializer-sample.herokuapp.com/albums.json?artist_ids=2,3&includes=artists,songs
311
+ * http://restpack-serializer-sample.herokuapp.com/albums.json?artist_ids=2,3&include=artists,songs
@@ -1,13 +1,13 @@
1
1
  module RestPack::Serializer
2
2
  class Options
3
- attr_accessor :page, :page_size, :includes, :filters, :serializer, :model_class, :scope, :include_links
3
+ attr_accessor :page, :page_size, :include, :filters, :serializer, :model_class, :scope, :include_links
4
4
 
5
5
  def initialize(serializer, params = {}, scope = nil)
6
6
  params.symbolize_keys! if params.respond_to?(:symbolize_keys!)
7
7
 
8
8
  @page = 1
9
9
  @page_size = RestPack::Serializer.config.page_size
10
- @includes = []
10
+ @include = []
11
11
  @filters = filters_from_params(params, serializer)
12
12
  @serializer = serializer
13
13
  @model_class = serializer.model_class
@@ -16,7 +16,7 @@ module RestPack::Serializer
16
16
 
17
17
  @page = params[:page].to_i if params[:page]
18
18
  @page_size = params[:page_size].to_i if params[:page_size]
19
- @includes = params[:includes].split(',').map(&:to_sym) if params[:includes]
19
+ @include = params[:include].split(',').map(&:to_sym) if params[:include]
20
20
  end
21
21
 
22
22
  def scope_with_filters
@@ -51,6 +51,5 @@ module RestPack::Serializer
51
51
  end
52
52
  filters
53
53
  end
54
-
55
54
  end
56
55
  end
@@ -21,7 +21,7 @@ module RestPack::Serializer::Paging
21
21
 
22
22
  if options.include_links
23
23
  result[:links] = self.links
24
- Array(RestPack::Serializer::Factory.create(*options.includes)).each do |serializer|
24
+ Array(RestPack::Serializer::Factory.create(*options.include)).each do |serializer|
25
25
  result[:links].merge! serializer.class.links
26
26
  end
27
27
  end
@@ -42,7 +42,7 @@ module RestPack::Serializer::Paging
42
42
  page: options.page,
43
43
  page_size: options.page_size,
44
44
  count: page.total_entries,
45
- includes: options.includes
45
+ include: options.include
46
46
  }
47
47
 
48
48
  meta[:page_count] = ((page.total_entries - 1) / options.page_size) + 1
@@ -62,7 +62,7 @@ module RestPack::Serializer::Paging
62
62
  params = []
63
63
  params << "page=#{page}" unless page == 1
64
64
  params << "page_size=#{options.page_size}" unless options.default_page_size?
65
- params << "includes=#{options.includes.join(',')}" if options.includes.any?
65
+ params << "include=#{options.include.join(',')}" if options.include.any?
66
66
  params << options.filters_as_url_params if options.filters.any?
67
67
 
68
68
  url += '?' + params.join('&') if params.any?
@@ -6,9 +6,9 @@ module RestPack::Serializer::SideLoading
6
6
  side_loads = {
7
7
  :meta => { }
8
8
  }
9
- return side_loads if models.empty? || options.includes.nil?
9
+ return side_loads if models.empty? || options.include.nil?
10
10
 
11
- options.includes.each do |include|
11
+ options.include.each do |include|
12
12
  side_load_data = side_load(include, models, options)
13
13
  side_loads[:meta].merge!(side_load_data[:meta] || {})
14
14
  side_loads.merge! side_load_data.except(:meta)
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.4.6'
3
+ VERSION = '0.4.7'
4
4
  end
5
5
  end
@@ -7,7 +7,7 @@ describe RestPack::Serializer::Options do
7
7
 
8
8
  describe 'default values' do
9
9
  it { subject.model_class.should == MyApp::Song }
10
- it { subject.includes.should == [] }
10
+ it { subject.include.should == [] }
11
11
  it { subject.page.should == 1 }
12
12
  it { subject.page_size.should == 10 }
13
13
  it { subject.filters.should == {} }
@@ -22,9 +22,9 @@ describe RestPack::Serializer::Options do
22
22
  it { subject.page_size.should == 8 }
23
23
  end
24
24
 
25
- describe 'with includes' do
26
- let(:params) { { 'includes' => 'model1,model2' } }
27
- it { subject.includes.should == [:model1, :model2] }
25
+ describe 'with include' do
26
+ let(:params) { { 'include' => 'model1,model2' } }
27
+ it { subject.include.should == [:model1, :model2] }
28
28
  end
29
29
 
30
30
  context 'with filters' do
@@ -98,29 +98,29 @@ describe RestPack::Serializer::Paging do
98
98
  end
99
99
 
100
100
  context "when sideloading" do
101
- let(:params) { { includes: 'albums' } }
101
+ let(:params) { { include: 'albums' } }
102
102
 
103
103
  it "includes side-loaded models" do
104
104
  page[:albums].should_not == nil
105
105
  end
106
106
 
107
107
  it "includes the side-loads in the main meta data" do
108
- page[:meta][:songs][:includes].should == [:albums]
108
+ page[:meta][:songs][:include].should == [:albums]
109
109
  end
110
110
 
111
111
  it "includes the side-loads in page hrefs" do
112
- page[:meta][:songs][:next_href].should == '/songs.json?page=2&includes=albums'
112
+ page[:meta][:songs][:next_href].should == '/songs.json?page=2&include=albums'
113
113
  end
114
114
 
115
115
  context "with includes as comma delimited string" do
116
- let(:params) { { includes: "albums,artists" } }
116
+ let(:params) { { include: "albums,artists" } }
117
117
  it "includes side-loaded models" do
118
118
  page[:albums].should_not == nil
119
119
  page[:artists].should_not == nil
120
120
  end
121
121
 
122
122
  it "includes the side-loads in page hrefs" do
123
- page[:meta][:songs][:next_href].should == '/songs.json?page=2&includes=albums,artists'
123
+ page[:meta][:songs][:next_href].should == '/songs.json?page=2&include=albums,artists'
124
124
  end
125
125
 
126
126
  it "includes links" do
@@ -195,7 +195,7 @@ describe RestPack::Serializer::Paging do
195
195
 
196
196
  context "paging with paged side-load" do
197
197
  let(:page) { MyApp::AlbumSerializer.page_with_options(options) }
198
- let(:options) { RestPack::Serializer::Options.new(MyApp::AlbumSerializer, { includes: 'songs' }) }
198
+ let(:options) { RestPack::Serializer::Options.new(MyApp::AlbumSerializer, { include: 'songs' }) }
199
199
 
200
200
  it "includes side-loaded paging data in meta data" do
201
201
  page[:meta][:albums].should_not == nil
@@ -207,7 +207,7 @@ describe RestPack::Serializer::Paging do
207
207
 
208
208
  context "paging with two paged side-loads" do
209
209
  let(:page) { MyApp::ArtistSerializer.page_with_options(options) }
210
- let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { includes: 'albums,songs' }) }
210
+ let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { include: 'albums,songs' }) }
211
211
 
212
212
  it "includes side-loaded paging data in meta data" do
213
213
  page[:meta][:albums].should_not == nil
@@ -15,7 +15,7 @@ describe RestPack::Serializer::Resource do
15
15
  end
16
16
 
17
17
  describe "side-loading" do
18
- let(:params) { { id: @song.id, includes: 'albums' } }
18
+ let(:params) { { id: @song.id, include: 'albums' } }
19
19
 
20
20
  it "includes side-loaded models" do
21
21
  resource[:albums].count.should == 1
@@ -23,7 +23,7 @@ describe RestPack::Serializer::Resource do
23
23
  end
24
24
 
25
25
  it "includes the side-loads in the main meta data" do
26
- resource[:meta][:songs][:includes].should == [:albums]
26
+ resource[:meta][:songs][:include].should == [:albums]
27
27
  end
28
28
  end
29
29
 
@@ -22,7 +22,7 @@ describe RestPack::Serializer::SideLoading do
22
22
  end
23
23
 
24
24
  context "when including :albums" do
25
- let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "includes" => "albums" }) }
25
+ let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "albums" }) }
26
26
 
27
27
  it "returns a hash with no data" do
28
28
  side_loads.should == { :meta => {} }
@@ -34,7 +34,7 @@ describe RestPack::Serializer::SideLoading do
34
34
  let(:models) { [MyApp::Song.first] }
35
35
 
36
36
  context "when including :albums" do
37
- let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "includes" => "albums" }) }
37
+ let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "albums" }) }
38
38
 
39
39
  it "returns side-loaded albums" do
40
40
  side_loads.should == {
@@ -53,7 +53,7 @@ describe RestPack::Serializer::SideLoading do
53
53
  let(:models) { [song1, song2] }
54
54
 
55
55
  context "when including :albums" do
56
- let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "includes" => "albums" }) }
56
+ let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "albums" }) }
57
57
 
58
58
  it "returns side-loaded albums" do
59
59
  side_loads.should == {
@@ -14,7 +14,7 @@ describe RestPack::Serializer::SideLoading do
14
14
  let(:models) { [@artist1] }
15
15
 
16
16
  context "when including :albums" do
17
- let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "includes" => "albums" }) }
17
+ let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "albums" }) }
18
18
 
19
19
  it "returns side-loaded albums" do
20
20
  side_loads[:albums].count.should == @artist1.albums.count
@@ -28,7 +28,7 @@ describe RestPack::Serializer::SideLoading do
28
28
  let(:models) { [@artist1, @artist2] }
29
29
 
30
30
  context "when including :albums" do
31
- let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "includes" => "albums" }) }
31
+ let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "albums" }) }
32
32
 
33
33
  it "returns side-loaded albums" do
34
34
  expected_count = @artist1.albums.count + @artist2.albums.count
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::SideLoading do
4
- context "invalid :includes" do
4
+ context "invalid :include" do
5
5
  before(:each) do
6
6
  FactoryGirl.create(:song)
7
7
  end
@@ -12,7 +12,7 @@ describe RestPack::Serializer::SideLoading do
12
12
  message = ":wrong is not a valid include for MyApp::Song"
13
13
 
14
14
  expect do
15
- MyApp::SongSerializer.side_loads([MyApp::Song.first], RestPack::Serializer::Options.new(MyApp::SongSerializer, { "includes" => "wrong" }))
15
+ MyApp::SongSerializer.side_loads([MyApp::Song.first], RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "wrong" }))
16
16
  end.to raise_error(exception, message)
17
17
  end
18
18
  end
@@ -24,7 +24,7 @@ describe RestPack::Serializer::SideLoading do
24
24
  message = ":payments is not a valid include for MyApp::Artist"
25
25
 
26
26
  expect do
27
- MyApp::ArtistSerializer.side_loads([payment.artist], RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "includes" => "payments" }))
27
+ MyApp::ArtistSerializer.side_loads([payment.artist], RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "payments" }))
28
28
  end.to raise_error(exception, message)
29
29
  end
30
30
  end
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.6
4
+ version: 0.4.7
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-09-21 00:00:00.000000000 Z
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -267,7 +267,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
267
267
  version: '0'
268
268
  requirements: []
269
269
  rubyforge_project:
270
- rubygems_version: 2.0.5
270
+ rubygems_version: 2.0.7
271
271
  signing_key:
272
272
  specification_version: 4
273
273
  summary: Model serialization, paging, side-loading and filtering