paginate-responder 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paginate-responder.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'minitest'
8
+ gem 'rake'
9
+ gem 'rack-link_headers', :path => '../rack-link-headers'
10
+ end
11
+
12
+ group :test do
13
+ gem 'will_paginate'
14
+ gem 'actionpack'
15
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jan Graichen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ # Paginate::Responder
2
+
3
+ A Rails pagination responder with link header support.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'paginate-responder'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install paginate-responder
18
+
19
+ You will also need a pagination gem like
20
+ [will_paginate](mislav/will_paginate).
21
+
22
+ ## Usage
23
+
24
+ Add `Responders::PaginateResponder` to your responder chain:
25
+
26
+ ```ruby
27
+ class AppResponder < Responder
28
+ include Responders::PaginateResponder
29
+ end
30
+
31
+ class MyController < ApplicationController
32
+ self.responder = AppResponder
33
+ end
34
+ ```
35
+
36
+ Or use it with [plataformatec/responders](https://github.com/plataformatec/responders):
37
+
38
+ ```ruby
39
+ class MyController < ApplicationController
40
+ responders Responders::PaginateResponder
41
+ end
42
+ ```
43
+
44
+ `PaginateResponder` will add the following link headers to
45
+ non HTML responses:
46
+
47
+ * *first* First page's URL.
48
+ * *last* Last page's URL.
49
+ * *next* Next page's URL.
50
+ * *prev* Previous page's URL.
51
+
52
+ Next and previous page links will not be added if current
53
+ page is first or last page.
54
+
55
+ Also a `X-Total-Pages` header will be added with the total
56
+ number of pages if available. This allows applications
57
+ to display a progress bar or similar while fetching pages.
58
+
59
+ `PaginateResponder` should work with any pagination gem that
60
+ adds a `paginate` method to collections. Tests run with
61
+ [will_paginate](mislav/will_paginate).
62
+
63
+ The `total_pages` method on the collection will be used as
64
+ total page count. If not total page method is present or
65
+ nil is returned some link header may be missing.
66
+
67
+ ## TODOs
68
+
69
+ * Documentation
70
+ ** Controller methods
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Add tests for your feature.
77
+ 4. Add your feature.
78
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 6. Push to the branch (`git push origin my-new-feature`)
80
+ 7. Create new Pull Request
81
+
82
+ ## License
83
+
84
+ [MIT License](http://www.opensource.org/licenses/mit-license.php)
85
+
86
+ Copyright (c) 2013, Jan Graichen
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "test"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,9 @@
1
+ require 'rack-link_headers'
2
+
3
+ module Responders
4
+ autoload :PaginateResponder, 'responders/paginate_responder'
5
+ end
6
+
7
+ module PaginateResponder
8
+ autoload :VERSION, 'paginate-responder/version'
9
+ end
@@ -0,0 +1,12 @@
1
+ module PaginateResponder
2
+ module VERSION
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 0
6
+ STAGE = nil
7
+
8
+ def self.to_s
9
+ [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join '.'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+
2
+ module Responders
3
+ module PaginateResponder
4
+ def to_format
5
+ if get? && resource.respond_to?(:paginate)
6
+ @resource = resource.paginate :page => self.page, :per_page => self.per_page
7
+
8
+ controller.response.link("first", controller.url_for(request.params.merge(:page => 1)))
9
+ controller.response.link("prev", controller.url_for(request.params.merge(:page => page - 1))) if page > 1
10
+ controller.response.link("next", controller.url_for(request.params.merge(:page => page + 1))) if total_pages && (page+1) < total_pages
11
+ controller.response.link("last", controller.url_for(request.params.merge(:page => total_pages))) if total_pages
12
+ controller.response.headers["X-Total-Pages"] = total_pages if total_pages
13
+ end
14
+ super
15
+ end
16
+
17
+ def page
18
+ @page ||= controller.page if controller.respond_to? :page
19
+ @page ||= controller.params[:page].try(:to_i)
20
+ @page ||= 1
21
+ @page
22
+ rescue
23
+ 1
24
+ end
25
+
26
+ def total_pages
27
+ @total_pages ||= resource.total_pages if resource.respond_to? :total_pages
28
+ @total_pages
29
+ end
30
+
31
+ def per_page
32
+ @per_page ||= controller.per_page if controller.respond_to? :per_page
33
+ @per_page ||= controller.params[:per_page].try(:to_i)
34
+ @per_page = [[1, @per_page].max, max_per_page].min
35
+ @per_page
36
+ rescue
37
+ max_per_page
38
+ end
39
+
40
+ def max_per_page
41
+ @max_per_page ||= controller.max_per_page if controller.respond_to? :max_per_page
42
+ @max_per_page ||= 50
43
+ @max_per_page
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'paginate-responder/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "paginate-responder"
8
+ gem.version = PaginateResponder::VERSION
9
+ gem.authors = ["Jan Graichen"]
10
+ gem.email = ["jg@altimos.de"]
11
+ gem.description = %q{A Rails pagination responder with link header support.}
12
+ gem.summary = %q{A Rails pagination responder with link header support.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rack-link_headers'
21
+ end
@@ -0,0 +1,104 @@
1
+ require 'test_helper.rb'
2
+
3
+ class PaginateResponderTest < ActionController::TestCase
4
+ tests PaginateController
5
+
6
+ def json; JSON[@response.body] end
7
+
8
+ def test_pagination
9
+ get :index, :format => :json
10
+
11
+ assert_equal 50, json.size
12
+ assert_equal ('AA'..'zz').to_a[0..49], json
13
+ end
14
+
15
+ def test_pagination_page_2
16
+ get :index, :format => :json, :page => 2
17
+
18
+ assert_equal 50, json.size
19
+ assert_equal ('AA'..'zz').to_a[50..99], json
20
+ end
21
+
22
+ def test_pagination_per_page
23
+ get :index, :format => :json, :page => 1, :per_page => 10
24
+
25
+ assert_equal 10, json.size
26
+ assert_equal ('AA'..'zz').to_a[0..9], json
27
+ end
28
+
29
+ def test_pagination_per_page_page_2
30
+ get :index, :format => :json, :page => 2, :per_page => 10
31
+
32
+ assert_equal 10, json.size
33
+ assert_equal ('AA'..'zz').to_a[10..19], json
34
+ end
35
+
36
+ def test_headers
37
+ get :index, :format => :json
38
+
39
+ assert_equal 3, response.links.size
40
+
41
+ assert_equal "first", response.links[0][:rel]
42
+ assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
43
+
44
+ assert_equal "next", response.links[1][:rel]
45
+ assert_equal "http://test.host/index.json?page=2", response.links[1][:url]
46
+
47
+ assert_equal "last", response.links[2][:rel]
48
+ assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
49
+ end
50
+
51
+ def test_headers_page_5
52
+ get :index, :format => :json, :page => 5
53
+
54
+ assert_equal 4, response.links.size
55
+
56
+ assert_equal "first", response.links[0][:rel]
57
+ assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
58
+
59
+ assert_equal "prev", response.links[1][:rel]
60
+ assert_equal "http://test.host/index.json?page=4", response.links[1][:url]
61
+
62
+ assert_equal "next", response.links[2][:rel]
63
+ assert_equal "http://test.host/index.json?page=6", response.links[2][:url]
64
+
65
+ assert_equal "last", response.links[3][:rel]
66
+ assert_equal "http://test.host/index.json?page=14", response.links[3][:url]
67
+ end
68
+
69
+ def test_headers_last_page
70
+ get :index, :format => :json, :page => 14
71
+
72
+ assert_equal 3, response.links.size
73
+
74
+ assert_equal "first", response.links[0][:rel]
75
+ assert_equal "http://test.host/index.json?page=1", response.links[0][:url]
76
+
77
+ assert_equal "prev", response.links[1][:rel]
78
+ assert_equal "http://test.host/index.json?page=13", response.links[1][:url]
79
+
80
+ assert_equal "last", response.links[2][:rel]
81
+ assert_equal "http://test.host/index.json?page=14", response.links[2][:url]
82
+ end
83
+
84
+ def test_headers_per_page
85
+ get :index, :format => :json, :page => 1, :per_page => 10
86
+
87
+ assert_equal 3, response.links.size
88
+
89
+ assert_equal "first", response.links[0][:rel]
90
+ assert_equal "http://test.host/index.json?page=1&per_page=10", response.links[0][:url]
91
+
92
+ assert_equal "next", response.links[1][:rel]
93
+ assert_equal "http://test.host/index.json?page=2&per_page=10", response.links[1][:url]
94
+
95
+ assert_equal "last", response.links[2][:rel]
96
+ assert_equal "http://test.host/index.json?page=68&per_page=10", response.links[2][:url]
97
+ end
98
+
99
+ def test_headers_total_pages
100
+ get :index, :format => :json
101
+
102
+ assert_equal "14", response.headers["X-Total-Pages"].to_s
103
+ end
104
+ end
@@ -0,0 +1,39 @@
1
+ require 'minitest/autorun'
2
+ require 'bundler'
3
+
4
+ Bundler.setup
5
+
6
+ # Configure Rails
7
+ ENV["RAILS_ENV"] = "test"
8
+
9
+ require 'active_support'
10
+ require 'action_controller'
11
+ require 'will_paginate/array'
12
+
13
+ require 'paginate-responder'
14
+
15
+ Responders::Routes = ActionDispatch::Routing::RouteSet.new
16
+ Responders::Routes.draw do
17
+ match '/index' => 'paginate#index'
18
+ end
19
+
20
+ class ActiveSupport::TestCase
21
+ setup do
22
+ @routes = Responders::Routes
23
+ end
24
+ end
25
+
26
+ class PaginationResponder < ActionController::Responder
27
+ include Responders::PaginateResponder
28
+ end
29
+
30
+ class PaginateController < ActionController::Base
31
+ include Responders::Routes.url_helpers
32
+
33
+ self.responder = PaginationResponder
34
+ respond_to :json
35
+
36
+ def index
37
+ respond_with ('AA'..'zz').to_a
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paginate-responder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Graichen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack-link_headers
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A Rails pagination responder with link header support.
31
+ email:
32
+ - jg@altimos.de
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .travis.yml
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/paginate-responder.rb
44
+ - lib/paginate-responder/version.rb
45
+ - lib/responders/paginate_responder.rb
46
+ - paginate-responder.gemspec
47
+ - test/paginate_responder_test.rb
48
+ - test/test_helper.rb
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
62
+ - 0
63
+ hash: -1984734961715854545
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ segments:
71
+ - 0
72
+ hash: -1984734961715854545
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A Rails pagination responder with link header support.
79
+ test_files:
80
+ - test/paginate_responder_test.rb
81
+ - test/test_helper.rb