pagoid 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +6 -0
- data/lib/pagoid.rb +21 -0
- data/lib/pagoid/adapter_router.rb +58 -0
- data/lib/pagoid/controller_extensions.rb +33 -0
- data/lib/pagoid/engine.rb +10 -0
- data/lib/pagoid/kaminari.rb +22 -0
- data/lib/pagoid/pager.rb +123 -0
- data/lib/pagoid/paging_adapter.rb +27 -0
- data/lib/pagoid/version.rb +3 -0
- data/lib/pagoid/will_paginate.rb +50 -0
- data/pagoid.gemspec +29 -0
- data/spec/lib/pagoid/kaminari_spec.rb +24 -0
- data/spec/lib/pagoid/pager_spec.rb +92 -0
- data/spec/lib/pagoid/will_paginate_spec.rb +22 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/adapter_shared_example.rb +28 -0
- data/spec/support/person.rb +15 -0
- metadata +210 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pagoid
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-1.9.3-p327
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jon Phenow
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Pagoid
|
2
|
+
|
3
|
+
Pagoid extracts the difference between [WillPaginate](https://github.com/mislav/will_paginate) and
|
4
|
+
[Kaminari](https://github.com/amatsuda/kaminari) as well as provides a standard callback
|
5
|
+
for things like publishing page info to your clients.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'kaminari'
|
13
|
+
# OR
|
14
|
+
gem 'will_paginate'
|
15
|
+
|
16
|
+
gem 'pagoid'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
$ bundle
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class PeopleController < ApplicationController
|
29
|
+
paged do |pager|
|
30
|
+
metadata[:paging] = pager.headers if pager.display_headers?
|
31
|
+
end
|
32
|
+
|
33
|
+
def index
|
34
|
+
@people = paginated Person
|
35
|
+
render json: { metadata: metdata, result: @people }
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def metadata
|
40
|
+
@metdata ||= {}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
You can stack `paged` blocks through inheritance:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class ApplicationController < ActionController::Base
|
49
|
+
paged do |pager|
|
50
|
+
notice! pager.headers
|
51
|
+
end
|
52
|
+
|
53
|
+
def notice!(hash)
|
54
|
+
Rails.logger.debug hash
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class PeopleController < ApplicationController
|
59
|
+
#.. from above
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/pagoid.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "pagoid/version"
|
2
|
+
begin
|
3
|
+
require 'kaminari'
|
4
|
+
require 'kaminari/models/array_extension'
|
5
|
+
if defined?(ActiveRecord::Base)
|
6
|
+
require 'kaminari/models/active_record_extension'
|
7
|
+
::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
|
8
|
+
end
|
9
|
+
rescue LoadError
|
10
|
+
begin
|
11
|
+
require 'will_paginate'
|
12
|
+
require 'will_paginate/array'
|
13
|
+
rescue LoadError
|
14
|
+
raise LoadError, "Please install kaminari or will_paginate for Pagoid backends"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'pagoid/pager'
|
19
|
+
require 'pagoid/controller_extensions'
|
20
|
+
require 'pagoid/adapter_router'
|
21
|
+
require 'pagoid/engine' if defined?(Rails)
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'pagoid/paging_adapter'
|
2
|
+
module Pagoid
|
3
|
+
class AdapterRouter
|
4
|
+
private
|
5
|
+
attr_accessor :pageable
|
6
|
+
|
7
|
+
def initialize(pageable)
|
8
|
+
self.pageable = pageable
|
9
|
+
end
|
10
|
+
|
11
|
+
public
|
12
|
+
|
13
|
+
def route
|
14
|
+
chosen_adapter = configured || route_table.find { |routeable| useable? routeable }
|
15
|
+
raise RouterError, "Could not find a suitable Pagoid Adapter" unless chosen_adapter
|
16
|
+
load_dependencies chosen_adapter
|
17
|
+
constantize(pagoided(chosen_adapter)).new pageable
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def configured
|
23
|
+
end
|
24
|
+
|
25
|
+
def pagoided(adapter)
|
26
|
+
"::Pagoid::#{adapter}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def load_dependencies(adapter)
|
30
|
+
require "pagoid/#{underscore(adapter)}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def underscore(adapter_name)
|
34
|
+
adapter_name.gsub(/::/, '/')
|
35
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
36
|
+
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
|
37
|
+
.tr("-", "_")
|
38
|
+
.downcase
|
39
|
+
end
|
40
|
+
|
41
|
+
def useable?(adapter)
|
42
|
+
constantize "::#{adapter}"
|
43
|
+
rescue NameError
|
44
|
+
false
|
45
|
+
end
|
46
|
+
|
47
|
+
def constantize(adapter)
|
48
|
+
Object.module_eval(adapter)
|
49
|
+
end
|
50
|
+
|
51
|
+
def route_table
|
52
|
+
%w[
|
53
|
+
Kaminari
|
54
|
+
WillPaginate
|
55
|
+
]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Pagoid
|
2
|
+
module ControllerExtensions
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
def paginated(resource)
|
8
|
+
pager = Pager.new(resource, params)
|
9
|
+
self.class.apply_pager_blocks(self, pager)
|
10
|
+
pager.paginated
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def paged(&block)
|
15
|
+
@pagination_block = block
|
16
|
+
end
|
17
|
+
|
18
|
+
def apply_pager_blocks(context, pager_instance)
|
19
|
+
pager_blocks.each { |block| context.instance_exec(pager_instance, &block) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def pager_block
|
23
|
+
@pagination_block || ->(x) { x }
|
24
|
+
end
|
25
|
+
private :pager_block
|
26
|
+
|
27
|
+
def pager_blocks
|
28
|
+
[pager_block] + (superclass.respond_to?(:pager_blocks) ? superclass.pager_blocks : [])
|
29
|
+
end
|
30
|
+
protected :pager_blocks
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'pagoid/paging_adapter'
|
2
|
+
module Pagoid
|
3
|
+
class Kaminari < PagingAdapter
|
4
|
+
def coerce(coerceable = paginatable)
|
5
|
+
coerce?(coerceable) ? ::Kaminari.paginate_array(Array(coerceable)) : coerceable
|
6
|
+
end
|
7
|
+
|
8
|
+
def page(*args)
|
9
|
+
__getobj__.page(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def coerce?(coerceable = paginatable)
|
15
|
+
!(
|
16
|
+
coerceable.respond_to?(:order) &&
|
17
|
+
coerceable.respond_to?(:page) &&
|
18
|
+
coerceable.page.respond_to?(:per)
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/pagoid/pager.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
# Service Class for handling of adapting paging for both Arrays and ActiveRecord objects.
|
3
|
+
# An array of AR Objects should come out the same as if you were to pass in a relation (though
|
4
|
+
# likely much slower).
|
5
|
+
#
|
6
|
+
# Defaults:
|
7
|
+
# page: 1
|
8
|
+
# per_page: 100
|
9
|
+
# order_by: Object <=> Object
|
10
|
+
# direction: Descending
|
11
|
+
module Pagoid
|
12
|
+
class Pager
|
13
|
+
extend Forwardable
|
14
|
+
def_delegators :paginatable, :coerce
|
15
|
+
|
16
|
+
# Initialize
|
17
|
+
#
|
18
|
+
# @param [Object] paginatable Some object(like an Array or ActiveRecord Object) for pagination
|
19
|
+
# @param [Hash] options options hash for tweaking pagination
|
20
|
+
# @option options [Fixnum] :page The page number
|
21
|
+
# @option options [Fixnum] :per_page The page size
|
22
|
+
# @option options [Fixnum] :order_by Callable on each element (or column) to sort by
|
23
|
+
# @option options [Fixnum] :direction "desc" or "asc"
|
24
|
+
def initialize(paginatable, options = {})
|
25
|
+
self.paginatable = AdapterRouter.new(paginatable).route
|
26
|
+
self.options = options.dup
|
27
|
+
end
|
28
|
+
|
29
|
+
# Memoized method of Paged, Sized, and Ordered output
|
30
|
+
#
|
31
|
+
# @return ordered, sized, paged data
|
32
|
+
def paginated
|
33
|
+
@paginated ||= pered
|
34
|
+
end
|
35
|
+
|
36
|
+
# Hash of data for publishing on a payload for Paging reference
|
37
|
+
#
|
38
|
+
# @return [Hash] hash of paging data
|
39
|
+
def headers
|
40
|
+
{
|
41
|
+
total: paginated.total_count,
|
42
|
+
total_pages: paginated.total_pages,
|
43
|
+
first_page: paginated.first_page?,
|
44
|
+
last_page: paginated.last_page?,
|
45
|
+
current_page: paginated.current_page,
|
46
|
+
limit: paginated.limit_value,
|
47
|
+
offset: paginated.offset_value
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
# Describes whether or not to publish paging reference
|
52
|
+
#
|
53
|
+
# @return boolean describing whether to display pagination headers
|
54
|
+
def display_headers?
|
55
|
+
!paginated.nil?
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
attr_accessor :paginatable
|
61
|
+
attr_accessor :options
|
62
|
+
|
63
|
+
def pered
|
64
|
+
paged.per(per_page)
|
65
|
+
end
|
66
|
+
|
67
|
+
def ordered
|
68
|
+
coerce(order_callable.call(paginatable))
|
69
|
+
end
|
70
|
+
|
71
|
+
def paged
|
72
|
+
ordered.page(page)
|
73
|
+
end
|
74
|
+
|
75
|
+
def per_page
|
76
|
+
options[:per_page].to_i > 0 ? options[:per_page].to_i : 100
|
77
|
+
end
|
78
|
+
|
79
|
+
def order_callable
|
80
|
+
order_method_hash[order_method] || sort_proc || ->(o) { o }
|
81
|
+
end
|
82
|
+
|
83
|
+
def order_method
|
84
|
+
order_methods.find { |method| coerce.respond_to? method }
|
85
|
+
end
|
86
|
+
|
87
|
+
def order_method_hash
|
88
|
+
{
|
89
|
+
order: ->(orderable) { orderable.order "#{order_by} #{direction}" }
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def sort_proc
|
94
|
+
if coerce.respond_to?(:sort)
|
95
|
+
->(orderable) {
|
96
|
+
orderable.sort { |a,b|
|
97
|
+
direction == :asc ? order_by_value(a) <=> order_by_value(b) : order_by_value(b) <=> order_by_value(a)
|
98
|
+
}
|
99
|
+
}
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def order_by_value(orderable)
|
104
|
+
orderable.respond_to?(order_by) ? orderable.public_send(order_by) : orderable
|
105
|
+
end
|
106
|
+
|
107
|
+
def order_methods
|
108
|
+
order_method_hash.keys
|
109
|
+
end
|
110
|
+
|
111
|
+
def page
|
112
|
+
options[:page].to_i > 0 ? options[:page].to_i : 1
|
113
|
+
end
|
114
|
+
|
115
|
+
def order_by
|
116
|
+
(!options[:order_by].nil? ? options[:order_by] : :created_at).to_s
|
117
|
+
end
|
118
|
+
|
119
|
+
def direction
|
120
|
+
options[:direction].to_s == "asc" ? :asc : :desc
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
module Pagoid
|
3
|
+
class PagingAdapter < SimpleDelegator
|
4
|
+
attr_accessor :paginatable
|
5
|
+
attr_accessor :attributes
|
6
|
+
|
7
|
+
def initialize(paginatable, attributes = {})
|
8
|
+
self.paginatable = paginatable
|
9
|
+
self.attributes = attributes
|
10
|
+
super coerce
|
11
|
+
end
|
12
|
+
|
13
|
+
def coerce(coerceable = paginatable)
|
14
|
+
coerceable
|
15
|
+
end
|
16
|
+
|
17
|
+
%w[page per order].each do |meth|
|
18
|
+
define_method(meth) do |*args|
|
19
|
+
chain(super(*args))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def chain(object, state = {})
|
24
|
+
self.class.new object, state
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'pagoid/paging_adapter'
|
2
|
+
module Pagoid
|
3
|
+
class WillPaginate < PagingAdapter
|
4
|
+
def total_count
|
5
|
+
count
|
6
|
+
end
|
7
|
+
|
8
|
+
def limit_value
|
9
|
+
per_page
|
10
|
+
end
|
11
|
+
|
12
|
+
def offset_value
|
13
|
+
offset
|
14
|
+
end
|
15
|
+
|
16
|
+
def page(num)
|
17
|
+
if array?
|
18
|
+
chain __getobj__.paginate(page: num), original: __getobj__, page: num
|
19
|
+
else
|
20
|
+
chain super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def per(num)
|
25
|
+
if array?
|
26
|
+
chain per_object.paginate(page: attributes[:page], per_page: num)
|
27
|
+
else
|
28
|
+
chain __getobj__.per_page(num)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def first_page?
|
33
|
+
current_page == 1
|
34
|
+
end
|
35
|
+
|
36
|
+
def last_page?
|
37
|
+
current_page == total_pages
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def per_object
|
43
|
+
attributes[:original] || __getobj__
|
44
|
+
end
|
45
|
+
|
46
|
+
def array?
|
47
|
+
__getobj__.respond_to? :paginate
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/pagoid.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pagoid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pagoid"
|
8
|
+
spec.version = Pagoid::VERSION
|
9
|
+
spec.authors = ["Jon Phenow"]
|
10
|
+
spec.email = ["j.phenow@gmail.com"]
|
11
|
+
spec.description = %q{Standardize paging abstraction}
|
12
|
+
spec.summary = %q{Easy way to paginate with will_paginate or kaminari backends}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "kaminari"
|
25
|
+
spec.add_development_dependency "will_paginate"
|
26
|
+
spec.add_development_dependency "activerecord"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
28
|
+
spec.add_development_dependency "simplecov"
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pagoid/kaminari'
|
3
|
+
require 'kaminari'
|
4
|
+
require 'kaminari/models/array_extension'
|
5
|
+
module Pagoid
|
6
|
+
describe Kaminari do
|
7
|
+
subject { described_class.new decorated }
|
8
|
+
before do
|
9
|
+
AdapterRouter.any_instance.stub configured: "Kaminari"
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:decorated) { [] }
|
13
|
+
|
14
|
+
it_should_behave_like "a pager adapter"
|
15
|
+
|
16
|
+
its(:coerce) { should be_a ::Kaminari::PaginatableArray }
|
17
|
+
|
18
|
+
describe "with active record" do
|
19
|
+
let(:decorated) { Person }
|
20
|
+
|
21
|
+
its(:coerce) { should == Person }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Pagoid
|
3
|
+
describe Pager do
|
4
|
+
describe "array paging" do
|
5
|
+
let(:paginatable) { (1..10).to_a }
|
6
|
+
let(:options) { {} }
|
7
|
+
subject { described_class.new paginatable, options }
|
8
|
+
|
9
|
+
its(:paginated) { should == paginatable.reverse }
|
10
|
+
its(:display_headers?) { should be_true }
|
11
|
+
|
12
|
+
describe "with options" do
|
13
|
+
let(:options) { { per_page: per_page, page: page } }
|
14
|
+
let(:per_page) { 2 }
|
15
|
+
let(:page) { 1 }
|
16
|
+
|
17
|
+
its(:paginated) { should == [10,9] }
|
18
|
+
its(:display_headers?) { should be_true }
|
19
|
+
|
20
|
+
describe "sorting" do
|
21
|
+
let(:options) { { per_page: per_page, page: page, order_by: :to_s } }
|
22
|
+
|
23
|
+
its(:paginated) { should == [9,8] }
|
24
|
+
its(:display_headers?) { should be_true }
|
25
|
+
|
26
|
+
describe "with paging" do
|
27
|
+
let(:page) { 3 }
|
28
|
+
|
29
|
+
its(:paginated) { should == [5,4] }
|
30
|
+
its(:display_headers?) { should be_true }
|
31
|
+
|
32
|
+
describe "with direction" do
|
33
|
+
let(:page) { nil }
|
34
|
+
let(:options) { { per_page: per_page, page: page, order_by: :to_s, direction: "asc" } }
|
35
|
+
|
36
|
+
its(:paginated) { should == [1,10] }
|
37
|
+
its(:display_headers?) { should be_true }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "ActiveRecord paging" do
|
45
|
+
before do
|
46
|
+
%w[Adam Jon Mike Pat].each do |name|
|
47
|
+
Person.create(name: name)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
let(:paginatable) { Person }
|
52
|
+
let(:options) { {} }
|
53
|
+
subject { described_class.new paginatable, options }
|
54
|
+
|
55
|
+
its(:paginated) { should == paginatable.order("created_at desc").all }
|
56
|
+
its(:display_headers?) { should be_true }
|
57
|
+
|
58
|
+
describe "with options" do
|
59
|
+
let(:options) { { per_page: per_page, page: page } }
|
60
|
+
let(:per_page) { 2 }
|
61
|
+
let(:page) { 1 }
|
62
|
+
|
63
|
+
its(:paginated) { should == Person.order("created_at desc").page(page).per(per_page) }
|
64
|
+
its(:display_headers?) { should be_true }
|
65
|
+
|
66
|
+
describe "sorting" do
|
67
|
+
let(:options) { { per_page: per_page, page: page, order_by: :name } }
|
68
|
+
|
69
|
+
its(:paginated) { should == Person.order("#{options[:order_by]} desc").page(page).per(per_page) }
|
70
|
+
its(:display_headers?) { should be_true }
|
71
|
+
|
72
|
+
describe "with paging" do
|
73
|
+
let(:page) { 2 }
|
74
|
+
|
75
|
+
its(:paginated) {
|
76
|
+
should == Person.page(page).per(options[:per_page]).order("#{options[:order_by]} desc")
|
77
|
+
}
|
78
|
+
its(:display_headers?) { should be_true }
|
79
|
+
|
80
|
+
describe "with direction" do
|
81
|
+
let(:page) { nil }
|
82
|
+
let(:options) { { per_page: per_page, page: page, order_by: :name, direction: "asc" } }
|
83
|
+
|
84
|
+
its(:paginated) { should == Person.order("name ASC").first(2) }
|
85
|
+
its(:display_headers?) { should be_true }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pagoid/will_paginate'
|
3
|
+
require 'will_paginate'
|
4
|
+
require 'will_paginate/array'
|
5
|
+
module Pagoid
|
6
|
+
describe WillPaginate do
|
7
|
+
subject { described_class.new decorated }
|
8
|
+
before do
|
9
|
+
AdapterRouter.any_instance.stub configured: "WillPaginate"
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:decorated) { [] }
|
13
|
+
|
14
|
+
it_should_behave_like "a pager adapter"
|
15
|
+
|
16
|
+
describe "with active record" do
|
17
|
+
let(:decorated) { Person }
|
18
|
+
|
19
|
+
its(:coerce) { should == Person }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter "/spec/"
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'pagoid'
|
8
|
+
|
9
|
+
Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
|
+
config.run_all_when_everything_filtered = true
|
14
|
+
config.filter_run :focus
|
15
|
+
|
16
|
+
config.before { Person.delete_all }
|
17
|
+
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
shared_examples "a specific page adaptation" do
|
2
|
+
it { should respond_to :page }
|
3
|
+
it { should respond_to :coerce }
|
4
|
+
|
5
|
+
describe "post-page methods" do
|
6
|
+
subject { described_class.new(decorated).page(1).per(1) }
|
7
|
+
it { should respond_to :total_count }
|
8
|
+
it { should respond_to :total_pages }
|
9
|
+
it { should respond_to :first_page? }
|
10
|
+
it { should respond_to :last_page? }
|
11
|
+
it { should respond_to :current_page }
|
12
|
+
it { should respond_to :limit_value }
|
13
|
+
it { should respond_to :offset_value }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
shared_examples "a pager adapter" do
|
18
|
+
subject { described_class.new decorated }
|
19
|
+
let(:decorated) { [] }
|
20
|
+
|
21
|
+
it_should_behave_like "a specific page adaptation"
|
22
|
+
|
23
|
+
describe "with active record" do
|
24
|
+
let(:decorated) { Person }
|
25
|
+
|
26
|
+
it_should_behave_like "a specific page adaptation"
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
root = File.expand_path File.join(__FILE__, "../../../")
|
2
|
+
ActiveRecord::Base.establish_connection(
|
3
|
+
adapter: "sqlite3",
|
4
|
+
database: "#{root}/spec/support/pagoid.db"
|
5
|
+
)
|
6
|
+
|
7
|
+
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'people'")
|
8
|
+
|
9
|
+
ActiveRecord::Base.connection.create_table(:people) do |t|
|
10
|
+
t.string :name
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
|
14
|
+
class Person < ActiveRecord::Base
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pagoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jon Phenow
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: kaminari
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: will_paginate
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: activerecord
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: sqlite3
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: simplecov
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
description: Standardize paging abstraction
|
143
|
+
email:
|
144
|
+
- j.phenow@gmail.com
|
145
|
+
executables: []
|
146
|
+
extensions: []
|
147
|
+
extra_rdoc_files: []
|
148
|
+
files:
|
149
|
+
- .gitignore
|
150
|
+
- .rspec
|
151
|
+
- .ruby-gemset
|
152
|
+
- .ruby-version
|
153
|
+
- Gemfile
|
154
|
+
- LICENSE.txt
|
155
|
+
- README.md
|
156
|
+
- Rakefile
|
157
|
+
- lib/pagoid.rb
|
158
|
+
- lib/pagoid/adapter_router.rb
|
159
|
+
- lib/pagoid/controller_extensions.rb
|
160
|
+
- lib/pagoid/engine.rb
|
161
|
+
- lib/pagoid/kaminari.rb
|
162
|
+
- lib/pagoid/pager.rb
|
163
|
+
- lib/pagoid/paging_adapter.rb
|
164
|
+
- lib/pagoid/version.rb
|
165
|
+
- lib/pagoid/will_paginate.rb
|
166
|
+
- pagoid.gemspec
|
167
|
+
- spec/lib/pagoid/kaminari_spec.rb
|
168
|
+
- spec/lib/pagoid/pager_spec.rb
|
169
|
+
- spec/lib/pagoid/will_paginate_spec.rb
|
170
|
+
- spec/spec_helper.rb
|
171
|
+
- spec/support/adapter_shared_example.rb
|
172
|
+
- spec/support/person.rb
|
173
|
+
homepage: ''
|
174
|
+
licenses:
|
175
|
+
- MIT
|
176
|
+
post_install_message:
|
177
|
+
rdoc_options: []
|
178
|
+
require_paths:
|
179
|
+
- lib
|
180
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
181
|
+
none: false
|
182
|
+
requirements:
|
183
|
+
- - ! '>='
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: '0'
|
186
|
+
segments:
|
187
|
+
- 0
|
188
|
+
hash: -1133189516884409261
|
189
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
none: false
|
191
|
+
requirements:
|
192
|
+
- - ! '>='
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
segments:
|
196
|
+
- 0
|
197
|
+
hash: -1133189516884409261
|
198
|
+
requirements: []
|
199
|
+
rubyforge_project:
|
200
|
+
rubygems_version: 1.8.24
|
201
|
+
signing_key:
|
202
|
+
specification_version: 3
|
203
|
+
summary: Easy way to paginate with will_paginate or kaminari backends
|
204
|
+
test_files:
|
205
|
+
- spec/lib/pagoid/kaminari_spec.rb
|
206
|
+
- spec/lib/pagoid/pager_spec.rb
|
207
|
+
- spec/lib/pagoid/will_paginate_spec.rb
|
208
|
+
- spec/spec_helper.rb
|
209
|
+
- spec/support/adapter_shared_example.rb
|
210
|
+
- spec/support/person.rb
|