restpack_serializer 0.2.11 → 0.2.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/restpack_serializer/options.rb +7 -7
- data/lib/restpack_serializer/serializable/paging.rb +1 -1
- data/lib/restpack_serializer/serializable/resource.rb +1 -1
- data/lib/restpack_serializer/serializable/side_loading.rb +1 -1
- data/lib/restpack_serializer/version.rb +1 -1
- data/spec/serializable/options_spec.rb +1 -1
- data/spec/serializable/paging_spec.rb +3 -3
- data/spec/serializable/side_loading/belongs_to_spec.rb +4 -4
- data/spec/serializable/side_loading/has_many_spec.rb +2 -2
- data/spec/serializable/side_loading/side_loading_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d32bb32927a51cf68e6f76910199fc343c1f545
|
4
|
+
data.tar.gz: e531717bffc615184c588e1523fb461874a5b402
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7a53fd767fff3671ba6818ffbeab9a43cff08b27f6424e39e2427b7be65cde6be6402bb54cc703c593c2f4e60697ce9b89bac53912728892bbaa015eeea2a52
|
7
|
+
data.tar.gz: 4c68c689127a698f04f82670f241188cb621ae38fb600631c337b5967620de9f361bdeb03a548418bbe9506ee04edf83d0c19adbc8fd6636cdb1f0ebd468f527
|
@@ -1,15 +1,16 @@
|
|
1
1
|
module RestPack::Serializer
|
2
2
|
class Options
|
3
|
-
attr_accessor :page, :page_size, :includes, :filters, :model_class, :scope, :include_links
|
3
|
+
attr_accessor :page, :page_size, :includes, :filters, :serializer, :model_class, :scope, :include_links
|
4
4
|
|
5
|
-
def initialize(
|
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
10
|
@includes = []
|
11
|
-
@filters = filters_from_params(params,
|
12
|
-
@
|
11
|
+
@filters = filters_from_params(params, serializer)
|
12
|
+
@serializer = serializer
|
13
|
+
@model_class = serializer.model_class
|
13
14
|
@scope = scope || model_class.send(:scoped)
|
14
15
|
@include_links = true
|
15
16
|
|
@@ -41,10 +42,9 @@ module RestPack::Serializer
|
|
41
42
|
|
42
43
|
private
|
43
44
|
|
44
|
-
def filters_from_params(params,
|
45
|
-
serializer = RestPack::Serializer::Factory.create(model_class)
|
45
|
+
def filters_from_params(params, serializer)
|
46
46
|
filters = {}
|
47
|
-
serializer.
|
47
|
+
serializer.filterable_by.each do |filter|
|
48
48
|
[filter, "#{filter}s".to_sym].each do |key|
|
49
49
|
filters[filter] = params[key].split(',') if params[key]
|
50
50
|
end
|
@@ -3,7 +3,7 @@ module RestPack::Serializer::Paging
|
|
3
3
|
|
4
4
|
module ClassMethods
|
5
5
|
def page(params = {}, scope = nil)
|
6
|
-
page_with_options RestPack::Serializer::Options.new(self
|
6
|
+
page_with_options RestPack::Serializer::Options.new(self, params, scope)
|
7
7
|
end
|
8
8
|
|
9
9
|
def page_with_options(options)
|
@@ -3,7 +3,7 @@ module RestPack::Serializer::Resource
|
|
3
3
|
|
4
4
|
module ClassMethods
|
5
5
|
def resource(params = {}, scope = nil)
|
6
|
-
page_with_options RestPack::Serializer::Options.new(self
|
6
|
+
page_with_options RestPack::Serializer::Options.new(self, params, scope)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -91,7 +91,7 @@ module RestPack::Serializer::SideLoading
|
|
91
91
|
|
92
92
|
def side_load_has_many(association, models, serializer)
|
93
93
|
return {} if models.empty?
|
94
|
-
options = RestPack::Serializer::Options.new(serializer.class
|
94
|
+
options = RestPack::Serializer::Options.new(serializer.class)
|
95
95
|
options.filters = { association.foreign_key.to_sym => models.map(&:id) }
|
96
96
|
options.include_links = false
|
97
97
|
return serializer.class.page_with_options(options)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require './spec/spec_helper'
|
2
2
|
|
3
3
|
describe RestPack::Serializer::Options do
|
4
|
-
let(:subject) { RestPack::Serializer::Options.new(
|
4
|
+
let(:subject) { RestPack::Serializer::Options.new(SongSerializer, params, scope) }
|
5
5
|
let(:params) { {} }
|
6
6
|
let(:scope) { nil }
|
7
7
|
|
@@ -157,7 +157,7 @@ describe RestPack::Serializer::Paging do
|
|
157
157
|
context "#page_with_options" do
|
158
158
|
let(:page) { SongSerializer.page_with_options(options) }
|
159
159
|
let(:params) { {} }
|
160
|
-
let(:options) { RestPack::Serializer::Options.new(
|
160
|
+
let(:options) { RestPack::Serializer::Options.new(SongSerializer, params) }
|
161
161
|
|
162
162
|
context "with defaults" do
|
163
163
|
it "includes valid paging meta data" do
|
@@ -179,7 +179,7 @@ describe RestPack::Serializer::Paging do
|
|
179
179
|
|
180
180
|
context "paging with paged side-load" do
|
181
181
|
let(:page) { AlbumSerializer.page_with_options(options) }
|
182
|
-
let(:options) { RestPack::Serializer::Options.new(
|
182
|
+
let(:options) { RestPack::Serializer::Options.new(AlbumSerializer, { includes: 'songs' }) }
|
183
183
|
|
184
184
|
it "includes side-loaded paging data in meta data" do
|
185
185
|
page[:meta][:albums].should_not == nil
|
@@ -191,7 +191,7 @@ describe RestPack::Serializer::Paging do
|
|
191
191
|
|
192
192
|
context "paging with two paged side-loads" do
|
193
193
|
let(:page) { ArtistSerializer.page_with_options(options) }
|
194
|
-
let(:options) { RestPack::Serializer::Options.new(
|
194
|
+
let(:options) { RestPack::Serializer::Options.new(ArtistSerializer, { includes: 'albums,songs' }) }
|
195
195
|
|
196
196
|
it "includes side-loaded paging data in meta data" do
|
197
197
|
page[:meta][:albums].should_not == nil
|
@@ -14,7 +14,7 @@ describe RestPack::Serializer::SideLoading do
|
|
14
14
|
let(:models) { [] }
|
15
15
|
|
16
16
|
context "no side-loads" do
|
17
|
-
let(:options) { RestPack::Serializer::Options.new(
|
17
|
+
let(:options) { RestPack::Serializer::Options.new(SongSerializer) }
|
18
18
|
|
19
19
|
it "returns a hash with no data" do
|
20
20
|
side_loads.should == { :meta => {} }
|
@@ -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(
|
25
|
+
let(:options) { RestPack::Serializer::Options.new(SongSerializer, { "includes" => "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) { [Song.first] }
|
35
35
|
|
36
36
|
context "when including :albums" do
|
37
|
-
let(:options) { RestPack::Serializer::Options.new(
|
37
|
+
let(:options) { RestPack::Serializer::Options.new(SongSerializer, { "includes" => "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(
|
56
|
+
let(:options) { RestPack::Serializer::Options.new(SongSerializer, { "includes" => "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(
|
17
|
+
let(:options) { RestPack::Serializer::Options.new(ArtistSerializer, { "includes" => "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(
|
31
|
+
let(:options) { RestPack::Serializer::Options.new(ArtistSerializer, { "includes" => "albums" }) }
|
32
32
|
|
33
33
|
it "returns side-loaded albums" do
|
34
34
|
expected_count = @artist1.albums.count + @artist2.albums.count
|
@@ -12,7 +12,7 @@ describe RestPack::Serializer::SideLoading do
|
|
12
12
|
message = ":wrong is not a valid include for Song"
|
13
13
|
|
14
14
|
expect do
|
15
|
-
SongSerializer.side_loads([Song.first], RestPack::Serializer::Options.new(
|
15
|
+
SongSerializer.side_loads([Song.first], RestPack::Serializer::Options.new(SongSerializer, { "includes" => "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 Artist"
|
25
25
|
|
26
26
|
expect do
|
27
|
-
ArtistSerializer.side_loads([payment.artist], RestPack::Serializer::Options.new(
|
27
|
+
ArtistSerializer.side_loads([payment.artist], RestPack::Serializer::Options.new(ArtistSerializer, { "includes" => "payments" }))
|
28
28
|
end.to raise_error(exception, message)
|
29
29
|
end
|
30
30
|
end
|