restpack_serializer 0.4.11 → 0.4.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: 90d1438faf15f90e22381b152265374f8ad53543
4
- data.tar.gz: 0fcbd12fbc3df348107fe579e5ea9e5103901529
3
+ metadata.gz: 477bc16aca491e5318b8665a209f293ac5d4b933
4
+ data.tar.gz: 9f5e498a6996a42b0e6cada857128a23412e5126
5
5
  SHA512:
6
- metadata.gz: 0d4d76207155ad77ae9547c4f2439c9c6d5a7c8b9c5463aabf1d93e4d21d567c9e51169536a18f5619d407919b61c867bd754829e9095eea81dc2f67b166df04
7
- data.tar.gz: 2900f4a1a0bbfe320d5daf0e5194a72d65f917a015b159253a8a5004007a52b1470fed900e9ba9abc9ea23bfce6b1ad69a934c5633b764893f35df6720ee75be
6
+ metadata.gz: a9c20b94f9c75a1b18c38dd89a7e32686cc94e9c6101f29d52dbe937bb01d034047b8b8933886e89549a7433e77f417ac2a4a6e958f740797301944652ab1323
7
+ data.tar.gz: ed68635e42185820ff8d39fc4b5ad5e4f76c33f0f89e67fbe326228c2c2c8c0623309f28976ba625d543297dd30beaec86a586d8cd1956218bf5d811da1d3f5a
data/README.md CHANGED
@@ -311,3 +311,8 @@ end
311
311
  Side-loading is available when filtering:
312
312
 
313
313
  * http://restpack-serializer-sample.herokuapp.com/api/v1/albums.json?artist_ids=2,3&include=artists,songs
314
+
315
+ ## Running Tests
316
+
317
+ `bundle`
318
+ `rake spec`
@@ -1,8 +1,9 @@
1
1
  module RestPack::Serializer
2
2
  class Options
3
- attr_accessor :page, :page_size, :include, :filters, :serializer, :model_class, :scope, :include_links
3
+ attr_accessor :page, :page_size, :include, :filters, :serializer,
4
+ :model_class, :scope, :context, :include_links
4
5
 
5
- def initialize(serializer, params = {}, scope = nil)
6
+ def initialize(serializer, params = {}, scope = nil, context = {})
6
7
  params.symbolize_keys! if params.respond_to?(:symbolize_keys!)
7
8
 
8
9
  @page = 1
@@ -12,6 +13,7 @@ module RestPack::Serializer
12
13
  @serializer = serializer
13
14
  @model_class = serializer.model_class
14
15
  @scope = scope || model_class.send(:all)
16
+ @context = context
15
17
  @include_links = true
16
18
 
17
19
  @page = params[:page].to_i if params[:page]
@@ -2,8 +2,8 @@ module RestPack::Serializer::Paging
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  module ClassMethods
5
- def page(params = {}, scope = nil)
6
- page_with_options RestPack::Serializer::Options.new(self, params, scope)
5
+ def page(params = {}, scope = nil, context = {})
6
+ page_with_options RestPack::Serializer::Options.new(self, params, scope, context)
7
7
  end
8
8
 
9
9
  def page_with_options(options)
@@ -13,7 +13,7 @@ module RestPack::Serializer::Paging
13
13
  )
14
14
 
15
15
  result = RestPack::Serializer::Result.new
16
- result.resources[self.key] = serialize_page(page)
16
+ result.resources[self.key] = serialize_page(page, options)
17
17
  result.meta[self.key] = serialize_meta(page, options)
18
18
 
19
19
  if options.include_links
@@ -31,8 +31,8 @@ module RestPack::Serializer::Paging
31
31
 
32
32
  private
33
33
 
34
- def serialize_page(page)
35
- page.map { |model| self.as_json(model) }
34
+ def serialize_page(page, options)
35
+ page.map { |model| self.as_json(model, options.context) }
36
36
  end
37
37
 
38
38
  def serialize_meta(page, options)
@@ -2,8 +2,8 @@ module RestPack::Serializer::Resource
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  module ClassMethods
5
- def resource(params = {}, scope = nil)
6
- page_with_options RestPack::Serializer::Options.new(self, params, scope)
5
+ def resource(params = {}, scope = nil, context = {})
6
+ page_with_options RestPack::Serializer::Options.new(self, params, scope, context)
7
7
  end
8
8
  end
9
9
  end
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.4.11'
3
+ VERSION = '0.4.12'
4
4
  end
5
5
  end
@@ -4,6 +4,10 @@ module MyApp
4
4
  attributes :id, :title, :album_id
5
5
  can_include :albums, :artists
6
6
  can_filter_by :title
7
+
8
+ def title
9
+ @context[:reverse_title?] ? @model.title.reverse : @model.title
10
+ end
7
11
  end
8
12
 
9
13
  class AlbumSerializer
@@ -7,8 +7,10 @@ describe RestPack::Serializer::Paging do
7
7
  end
8
8
 
9
9
  context "#page" do
10
- let(:page) { MyApp::SongSerializer.page(params) }
10
+ let(:page) { MyApp::SongSerializer.page(params, scope, context) }
11
11
  let(:params) { { } }
12
+ let(:scope) { nil }
13
+ let(:context) { { } }
12
14
 
13
15
  context "with defaults" do
14
16
  it "page defaults to 1" do
@@ -61,6 +63,15 @@ describe RestPack::Serializer::Paging do
61
63
  end
62
64
  end
63
65
 
66
+ context "with context" do
67
+ let(:context) { { reverse_title?: true } }
68
+
69
+ it "returns reversed titles" do
70
+ first = MyApp::Song.first
71
+ page[:songs].first[:title].should == first.title.reverse
72
+ end
73
+ end
74
+
64
75
  it "serializes results" do
65
76
  first = MyApp::Song.first
66
77
  page[:songs].first.should == {
@@ -6,14 +6,24 @@ describe RestPack::Serializer::Resource do
6
6
  @song = @album.songs.first
7
7
  end
8
8
 
9
- let(:resource) { MyApp::SongSerializer.resource(params) }
9
+ let(:resource) { MyApp::SongSerializer.resource(params, scope, context) }
10
10
  let(:params) { { id: @song.id } }
11
+ let(:scope) { nil }
12
+ let(:context) { { } }
11
13
 
12
14
  it "returns a resource by id" do
13
15
  resource[:songs].count.should == 1
14
16
  resource[:songs][0][:id].should == @song.id.to_s
15
17
  end
16
18
 
19
+ context "with context" do
20
+ let(:context) { { reverse_title?: true } }
21
+
22
+ it "returns reversed titles" do
23
+ resource[:songs][0][:title].should == @song.title.reverse
24
+ end
25
+ end
26
+
17
27
  describe "side-loading" do
18
28
  let(:params) { { id: @song.id, include: 'albums' } }
19
29
 
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.11
4
+ version: 0.4.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-07 00:00:00.000000000 Z
11
+ date: 2014-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -269,7 +269,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
269
269
  version: '0'
270
270
  requirements: []
271
271
  rubyforge_project:
272
- rubygems_version: 2.0.5
272
+ rubygems_version: 2.0.14
273
273
  signing_key:
274
274
  specification_version: 4
275
275
  summary: Model serialization, paging, side-loading and filtering
@@ -290,3 +290,4 @@ test_files:
290
290
  - spec/serializable/side_loading/side_loading_spec.rb
291
291
  - spec/spec_helper.rb
292
292
  - spec/support/factory.rb
293
+ has_rdoc: