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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d854b833f2b83d257bdf24cd7b81730a62dfa4e
4
- data.tar.gz: ea7b5d586a067f1b3bdae4ec2d46687015b319b0
3
+ metadata.gz: 5d32bb32927a51cf68e6f76910199fc343c1f545
4
+ data.tar.gz: e531717bffc615184c588e1523fb461874a5b402
5
5
  SHA512:
6
- metadata.gz: 49e5e2654db7e93b2654a993754e65d699ff9110c117ab287baf51fc8091d8c9d46eee5245c5251a5f11844cd57c7cfb77fe4c64c024f17fd170ffd33d7c9920
7
- data.tar.gz: 7d19b08a58a2675dd0fba237d3577ea69160506be25e1f013219e8746fc71aa299a00f1cf6c0767fa801cc81e3d7780da4e35d530273e3866d650e1916c94e7c
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(model_class, params = {}, scope = nil)
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, model_class)
12
- @model_class = model_class
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, model_class)
45
- serializer = RestPack::Serializer::Factory.create(model_class)
45
+ def filters_from_params(params, serializer)
46
46
  filters = {}
47
- serializer.class.filterable_by.each do |filter|
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.model_class, params, scope)
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.model_class, params, scope)
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.model_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,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.2.11'
3
+ VERSION = '0.2.12'
4
4
  end
5
5
  end
@@ -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(Song, params, scope) }
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(Song, params) }
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(Album, { includes: 'songs' }) }
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(Artist, { includes: 'albums,songs' }) }
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(Song) }
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(Song, { "includes" => "albums" }) }
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(Song, { "includes" => "albums" }) }
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(Song, { "includes" => "albums" }) }
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(Artist, { "includes" => "albums" }) }
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(Artist, { "includes" => "albums" }) }
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(Song, { "includes" => "wrong" }))
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(Artist, { "includes" => "payments" }))
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restpack_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce