restpack_serializer 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWM1YTg1ZWIyMWJmNjFlNDgyMTY0YWRkN2JkMzdiMzIyMTNkOTY3MQ==
4
+ MGQyN2VjOTdjYjFkMjI4NDIwNDc1ZTMxZTEyMjQ2YzZmNzk5MWFkYQ==
5
5
  data.tar.gz: !binary |-
6
- ODQ4NTZjYzljZDBjNjllNjAyOGFlYzA1YjM5ODgwMzM3YjczODlhYQ==
6
+ Y2IwNjM4Y2E3NTAzYmI2ZWUzNTNmMWJkMmZhMjY2NjNmMGQ2N2U2Nw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MzRlMGQzOWFkMmRjYzI0Y2VmZDk0NTcyMzc4NTllOTM2ODk5OGU5MGJlZGFi
10
- OTQ1MGQxNDIwMDdkZDQ1MzEyNWMwMWQyYjE2NmMwMDRhOGE3MWE5N2JhOTVh
11
- ZGI1M2VjOTc4NmQ1ODJhMjU0YmY3YzlmMzY1N2E3OWIwZGRjNTY=
9
+ NWE2OGIxNTA0YjhkNjYzMTc2ZGQ2MzNlOTZmOTJkMjAyZjQ1Y2JjYTNmNTZi
10
+ Y2Q0MTU5ZWNhMzI4MDVmNzU4ZmNkNzZjZjVkNTAyZTJkMDVjZTUwMjczNTc1
11
+ ZDMxMDJmMDFjYjY2YzJiYjE4NDQ0OGQ5NzZiNTY3MDk0YTlmZTU=
12
12
  data.tar.gz: !binary |-
13
- ODBlYzFkNDJiYjIxODk1OThhMTM5YWVlOTgwZGViZDBkMDIzMjJhOWVjNTk0
14
- ODNmZWU1NWJmYWU3MGY3MmUyMDQzMjkwN2RmNDk0NWJiMDZlZjUyMGM4MzQ5
15
- ZDE3ZGNhNGQ5Mjk3ZWU0YWY4ZjdhNzY2NjkwMDFiMTM3MDhjNWI=
13
+ OWU0MjUwNmQ2MTNhMDAxODFhNGI2MGQ5NjkxYjdhZjZlNjVjMjE1YzYxZGRj
14
+ OWUxZDVkYzJkNmE0N2MzMmRjMDhiMjJlNzU3OTEyZjFjODZhNzhlMGY0ZGM4
15
+ YWU1Y2Y5NDI1NDM2YTRjOTU3NjUzY2E5ODczZDJkMzkyN2U5NWE=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- restpack_serializer (0.2.6)
4
+ restpack_serializer (0.2.7)
5
5
  activerecord (>= 3.0)
6
6
  activesupport (>= 3.0)
7
7
  will_paginate (~> 3.0)
@@ -2,11 +2,13 @@ module RestPack::Serializer
2
2
  class Options
3
3
  attr_accessor :page, :page_size, :includes, :filters, :model_class, :scope, :include_links
4
4
 
5
+ DEFAULT_PAGE_SIZE = 10
6
+
5
7
  def initialize(model_class, params = {}, scope = nil)
6
8
  params.symbolize_keys! if params.respond_to?(:symbolize_keys!)
7
9
 
8
10
  @page = 1
9
- @page_size = 10
11
+ @page_size = DEFAULT_PAGE_SIZE
10
12
  @includes = []
11
13
  @filters = filters_from_params(params, model_class)
12
14
  @model_class = model_class
@@ -31,6 +33,14 @@ module RestPack::Serializer
31
33
  @scope.where(scope_filter)
32
34
  end
33
35
 
36
+ def default_page_size?
37
+ @page_size == DEFAULT_PAGE_SIZE
38
+ end
39
+
40
+ def filters_as_url_params
41
+ @filters.sort.map {|k,v| "#{k}=#{v.join(',')}" }.join('&')
42
+ end
43
+
34
44
  private
35
45
 
36
46
  def filters_from_params(params, model_class)
@@ -1,10 +1,8 @@
1
1
  module RestPack::Serializer::Attributes
2
2
  extend ActiveSupport::Concern
3
3
 
4
- module InstanceMethods
5
- def default_href
6
- "#{RestPack::Serializer.href_prefix}/#{@model.class.table_name}/#{@model.id}.json"
7
- end
4
+ def default_href
5
+ "#{RestPack::Serializer.href_prefix}/#{@model.class.table_name}/#{@model.id}.json"
8
6
  end
9
7
 
10
8
  module ClassMethods
@@ -48,7 +48,25 @@ module RestPack::Serializer::Paging
48
48
  meta[:page_count] = ((page.total_entries - 1) / options.page_size) + 1
49
49
  meta[:previous_page] = meta[:page] > 1 ? meta[:page] - 1 : nil
50
50
  meta[:next_page] = meta[:page] < meta[:page_count] ? meta[:page] + 1 : nil
51
+
52
+ meta[:previous_href] = page_href(meta[:previous_page], options)
53
+ meta[:next_href] = page_href(meta[:next_page], options)
51
54
  meta
52
55
  end
56
+
57
+ def page_href(page, options)
58
+ return nil unless page
59
+
60
+ url = "#{RestPack::Serializer.href_prefix}/#{self.key}.json"
61
+
62
+ params = []
63
+ params << "page=#{page}" unless page == 1
64
+ params << "page_size=#{options.page_size}" unless options.default_page_size?
65
+ params << "includes=#{options.includes.join(',')}" if options.includes.any?
66
+ params << options.filters_as_url_params if options.filters.any?
67
+
68
+ url += '?' + params.join('&') if params.any?
69
+ url
70
+ end
53
71
  end
54
72
  end
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.2.6'
3
+ VERSION = '0.2.7'
4
4
  end
5
5
  end
@@ -12,6 +12,8 @@ describe RestPack::Serializer::Options do
12
12
  it { subject.page_size.should == 10 }
13
13
  it { subject.filters.should == {} }
14
14
  it { subject.scope.should == Song.scoped }
15
+ it { subject.default_page_size?.should == true }
16
+ it { subject.filters_as_url_params.should == '' }
15
17
  end
16
18
 
17
19
  describe 'with paging params' do
@@ -33,22 +35,27 @@ describe RestPack::Serializer::Options do
33
35
  describe 'with a primary key with a single value' do
34
36
  let(:params) { { 'id' => '142857' } }
35
37
  it { subject.filters.should == { id: ['142857'] } }
38
+ it { subject.filters_as_url_params.should == 'id=142857' }
36
39
  end
37
40
  describe 'with a primary key with multiple values' do
38
41
  let(:params) { { 'ids' => '42,142857' } }
39
42
  it { subject.filters.should == { id: ['42', '142857'] } }
43
+ it { subject.filters_as_url_params.should == 'id=42,142857' }
40
44
  end
41
45
  describe 'with a foreign key with a single value' do
42
46
  let(:params) { { 'album_id' => '789' } }
43
47
  it { subject.filters.should == { album_id: ['789'] } }
48
+ it { subject.filters_as_url_params.should == 'album_id=789' }
44
49
  end
45
50
  describe 'with a foreign key with multiple values' do
46
51
  let(:params) { { 'album_id' => '789,678,567' } }
47
52
  it { subject.filters.should == { album_id: ['789', '678', '567'] } }
53
+ it { subject.filters_as_url_params.should == 'album_id=789,678,567' }
48
54
  end
49
55
  describe 'with multiple foreign keys' do
50
56
  let(:params) { { 'album_id' => '111,222', 'artist_id' => '888,999' } }
51
57
  it { subject.filters.should == { album_id: ['111', '222'], artist_id: ['888', '999'] } }
58
+ it { subject.filters_as_url_params.should == 'album_id=111,222&artist_id=888,999' }
52
59
  end
53
60
  end
54
61
 
@@ -21,7 +21,9 @@ describe RestPack::Serializer::Paging do
21
21
  page[:meta][:songs][:count].should == 18
22
22
  page[:meta][:songs][:page_count].should == 2
23
23
  page[:meta][:songs][:previous_page].should == nil
24
+ page[:meta][:songs][:previous_href].should == nil
24
25
  page[:meta][:songs][:next_page].should == 2
26
+ page[:meta][:songs][:next_href].should == '/songs.json?page=2'
25
27
  end
26
28
  it "includes links" do
27
29
  page[:links].should == {
@@ -37,6 +39,10 @@ describe RestPack::Serializer::Paging do
37
39
  page[:meta][:songs][:page_size].should == 3
38
40
  page[:meta][:songs][:page_count].should == 6
39
41
  end
42
+ it "includes the custom page size in the page hrefs" do
43
+ page[:meta][:songs][:next_page].should == 2
44
+ page[:meta][:songs][:next_href].should == '/songs.json?page=2&page_size=3'
45
+ end
40
46
  end
41
47
 
42
48
  it "serializes results" do
@@ -71,6 +77,7 @@ describe RestPack::Serializer::Paging do
71
77
  page[:meta][:songs][:page].should == 2
72
78
  page[:meta][:songs][:previous_page].should == 1
73
79
  page[:meta][:songs][:next_page].should == nil
80
+ page[:meta][:songs][:previous_href].should == '/songs.json'
74
81
  end
75
82
  end
76
83
 
@@ -85,6 +92,10 @@ describe RestPack::Serializer::Paging do
85
92
  page[:meta][:songs][:includes].should == [:albums]
86
93
  end
87
94
 
95
+ it "includes the side-loads in page hrefs" do
96
+ page[:meta][:songs][:next_href].should == '/songs.json?page=2&includes=albums'
97
+ end
98
+
88
99
  context "with includes as comma delimited string" do
89
100
  let(:params) { { includes: "albums,artists" } }
90
101
  it "includes side-loaded models" do
@@ -92,6 +103,10 @@ describe RestPack::Serializer::Paging do
92
103
  page[:artists].should_not == nil
93
104
  end
94
105
 
106
+ it "includes the side-loads in page hrefs" do
107
+ page[:meta][:songs][:next_href].should == '/songs.json?page=2&includes=albums,artists'
108
+ end
109
+
95
110
  it "includes links" do
96
111
  page[:links]['songs.album'].should_not == nil
97
112
  page[:links]['songs.artist'].should_not == nil
@@ -118,6 +133,10 @@ describe RestPack::Serializer::Paging do
118
133
  it "returns a page with songs from album1" do
119
134
  page[:meta][:songs][:count].should == @album1.songs.length
120
135
  end
136
+
137
+ it "includes the filter in page hrefs" do
138
+ page[:meta][:songs][:next_href].should == "/songs.json?page=2&album_id=#{@album1.id}"
139
+ end
121
140
  end
122
141
  end
123
142
 
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.2.6
4
+ version: 0.2.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-05-20 00:00:00.000000000 Z
11
+ date: 2013-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord