rails_pagination 2.0.5
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.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +62 -0
- data/Rakefile +38 -0
- data/lib/rails_pagination/action_view/base.rb +11 -0
- data/lib/rails_pagination/active_record/base.rb +25 -0
- data/lib/rails_pagination/active_record/relation.rb +82 -0
- data/lib/rails_pagination/navigation.rb +39 -0
- data/lib/rails_pagination/pager.rb +97 -0
- data/lib/rails_pagination/railtie.rb +15 -0
- data/lib/rails_pagination/version.rb +5 -0
- data/lib/rails_pagination.rb +10 -0
- data/test/collection_empty_test.rb +41 -0
- data/test/collection_first_test.rb +42 -0
- data/test/collection_last_test.rb +42 -0
- data/test/collection_middle_test.rb +42 -0
- data/test/collection_padding_test.rb +83 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/controllers/test_controller.rb +7 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/model.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/app/views/test/index.html.erb +30 -0
- data/test/dummy/config/application.rb +59 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +61 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20120908215532_create_models.rb +8 -0
- data/test/dummy/db/schema.rb +22 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +1998 -0
- data/test/dummy/log/test.log +149351 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/tmp/cache/assets/CBF/7A0/sprockets%2Fc9192594b6d1b927590d37ebad844808 +0 -0
- data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/dummy/tmp/cache/assets/D50/930/sprockets%2F277654ad498bbc928b88364ffcbad129 +0 -0
- data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/DB2/700/sprockets%2Fa4f4b12a5cbea015faede527e4a04307 +0 -0
- data/test/dummy/tmp/cache/assets/DD5/C50/sprockets%2Fcdcdad84697d0b186a1de99f1950e5ab +0 -0
- data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/helpers_empty_test.rb +46 -0
- data/test/helpers_first_test.rb +47 -0
- data/test/helpers_last_test.rb +47 -0
- data/test/helpers_middle_test.rb +47 -0
- data/test/rails_pagination_test.rb +9 -0
- data/test/test_helper.rb +21 -0
- metadata +204 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 Mattways
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
{<img src="https://codeclimate.com/github/mattways/rails_pagination.png" />}[https://codeclimate.com/github/mattways/rails_pagination] {<img src="https://secure.travis-ci.org/mattways/rails_pagination.png?branch=master" alt="Build Status" />}[https://travis-ci.org/mattways/rails_pagination] {<img src="https://gemnasium.com/mattways/rails_pagination.png" alt="Dependency Status" />}[https://gemnasium.com/mattways/rails_pagination]
|
2
|
+
|
3
|
+
= Rails Pagination
|
4
|
+
|
5
|
+
Inspired in kaminari and will_paginate but more minimalistic
|
6
|
+
|
7
|
+
= Install
|
8
|
+
|
9
|
+
Put this line in your Gemfile:
|
10
|
+
gem 'rails_pagination'
|
11
|
+
|
12
|
+
Then bundle:
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
= Usage
|
16
|
+
|
17
|
+
The same approach as kaminari:
|
18
|
+
Model.page(1).per(20)
|
19
|
+
|
20
|
+
But doesn't force you to use any markup, just use the methods of the pager object in your views:
|
21
|
+
- pager = paginate(@collection)
|
22
|
+
- if pager.has_multiple_pages?
|
23
|
+
#pager
|
24
|
+
- if pager.has_first_page?
|
25
|
+
%a.first{:href => pager.first_page_path}= t('pager.first')
|
26
|
+
- if pager.has_previous_page?
|
27
|
+
%a.previous{:href => pager.previous_page_path}= t('pager.previous')
|
28
|
+
- if pager.navigation.has_less_pages?
|
29
|
+
%span.ellipsis ...
|
30
|
+
- for page in pager.navigation.pages
|
31
|
+
- if pager.is_current_page? page
|
32
|
+
%span.page.current= page
|
33
|
+
- else
|
34
|
+
%a.page{:href => pager.page_path(page)}= page
|
35
|
+
- if pager.navigation.has_more_pages?
|
36
|
+
%span.ellipsis ...
|
37
|
+
- if pager.has_next_page?
|
38
|
+
%a.next{:href => pager.next_page_path}= t('pager.next')
|
39
|
+
- if pager.has_last_page?
|
40
|
+
%a.last{:href => pager.last_page_path}= t('pager.last')
|
41
|
+
|
42
|
+
= Configuration
|
43
|
+
|
44
|
+
== Default param name
|
45
|
+
|
46
|
+
If you want to change the default parameter name or the default navigation length call paginate method this way:
|
47
|
+
paginate(@collection, :navigation => 3, :parameter => :p)
|
48
|
+
Or to set it globally put this in your application.rb:
|
49
|
+
config.pagination.default_parameter = :p
|
50
|
+
config.pagination.default_navigation = 5
|
51
|
+
|
52
|
+
== Default per page
|
53
|
+
|
54
|
+
If you want to change the default per_page put this in your model:
|
55
|
+
default_per_page 5
|
56
|
+
Or to set it globally put this in your application.rb:
|
57
|
+
config.pagination.default_per_page = 5
|
58
|
+
|
59
|
+
== Padding
|
60
|
+
|
61
|
+
You can use positive and negative padding:
|
62
|
+
Model.page(1).per(10).pad(-4)
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'RailsPagination'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module RailsPagination
|
2
|
+
module ActiveRecord
|
3
|
+
module Base
|
4
|
+
|
5
|
+
def inherited(subclass)
|
6
|
+
if subclass.superclass == ::ActiveRecord::Base
|
7
|
+
subclass.scope :page, Proc.new {|number|
|
8
|
+
subclass.limit(subclass.default_per_page).offset(subclass.default_per_page * ([number.to_i, 1].max - 1))
|
9
|
+
} do
|
10
|
+
include RailsPagination::ActiveRecord::Relation
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def default_per_page(value=nil)
|
16
|
+
unless value.nil?
|
17
|
+
value = [value.to_i, 1].max
|
18
|
+
@default_per_page = value
|
19
|
+
end
|
20
|
+
defined?(@default_per_page).nil? ? Rails.application.config.pagination.default_per_page : @default_per_page
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module RailsPagination
|
2
|
+
module ActiveRecord
|
3
|
+
module Relation
|
4
|
+
|
5
|
+
def per(value)
|
6
|
+
value = [value.to_i, 1].max
|
7
|
+
limit(value).offset(value * (offset_value / limit_value))
|
8
|
+
end
|
9
|
+
|
10
|
+
def pad(value)
|
11
|
+
@padding = value
|
12
|
+
r = (offset_value + value) < 0 ? self : offset(offset_value + value)
|
13
|
+
decrease_limit? ? r.limit(limit_value + value) : r
|
14
|
+
end
|
15
|
+
|
16
|
+
def total_count
|
17
|
+
@total_count ||= begin
|
18
|
+
c = except(:offset, :limit, :order)
|
19
|
+
c = c.except(:includes) unless references_eager_loaded_tables?
|
20
|
+
uses_distinct_sql_statement = c.to_sql =~ /DISTINCT/i
|
21
|
+
if uses_distinct_sql_statement
|
22
|
+
c.length
|
23
|
+
else
|
24
|
+
c = c.count
|
25
|
+
c.respond_to?(:count) ? c.count : c
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def total_pages
|
31
|
+
@total_pages ||= [(fixed_total_count.to_f / fixed_limit_value).ceil, 1].max
|
32
|
+
end
|
33
|
+
|
34
|
+
def current_page
|
35
|
+
@current_page ||= (offset_value.to_f / fixed_limit_value).ceil + 1
|
36
|
+
end
|
37
|
+
|
38
|
+
def previous_page
|
39
|
+
@previous_page ||= current_page > 1 ? (current_page - 1) : nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def next_page
|
43
|
+
@next_page ||= current_page < total_pages ? (current_page + 1) : nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def first_page
|
47
|
+
1
|
48
|
+
end
|
49
|
+
|
50
|
+
def last_page
|
51
|
+
total_pages
|
52
|
+
end
|
53
|
+
|
54
|
+
def out_of_bounds?
|
55
|
+
@out_of_bounds ||= current_page > total_pages or current_page < first_page
|
56
|
+
end
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def has_padding?
|
61
|
+
not defined?(@padding).nil?
|
62
|
+
end
|
63
|
+
|
64
|
+
def padding_negative?
|
65
|
+
has_padding? and @padding < 0
|
66
|
+
end
|
67
|
+
|
68
|
+
def decrease_limit?
|
69
|
+
padding_negative? and offset_value == 0
|
70
|
+
end
|
71
|
+
|
72
|
+
def fixed_total_count
|
73
|
+
total_count - (padding_negative? ? @padding : 0)
|
74
|
+
end
|
75
|
+
|
76
|
+
def fixed_limit_value
|
77
|
+
limit_value - (decrease_limit? ? @padding : 0)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module RailsPagination
|
2
|
+
class Navigation
|
3
|
+
|
4
|
+
def initialize(collection, length)
|
5
|
+
if collection.total_pages >= length
|
6
|
+
middle = (length / 2).ceil
|
7
|
+
if collection.current_page > collection.total_pages - middle
|
8
|
+
starts_at = collection.total_pages - length + 1
|
9
|
+
ends_at = collection.total_pages
|
10
|
+
elsif collection.current_page <= middle
|
11
|
+
starts_at = 1
|
12
|
+
ends_at = length
|
13
|
+
else
|
14
|
+
starts_at = collection.current_page - middle
|
15
|
+
ends_at = collection.current_page + middle
|
16
|
+
end
|
17
|
+
else
|
18
|
+
starts_at = 1
|
19
|
+
ends_at = collection.total_pages
|
20
|
+
end
|
21
|
+
@has_less_pages = starts_at > 1
|
22
|
+
@has_more_pages = ends_at < collection.total_pages
|
23
|
+
@pages = Range.new(starts_at, ends_at)
|
24
|
+
end
|
25
|
+
|
26
|
+
def has_less_pages?
|
27
|
+
@has_less_pages
|
28
|
+
end
|
29
|
+
|
30
|
+
def has_more_pages?
|
31
|
+
@has_more_pages
|
32
|
+
end
|
33
|
+
|
34
|
+
def pages
|
35
|
+
@pages
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module RailsPagination
|
2
|
+
class Pager
|
3
|
+
|
4
|
+
def initialize(*args)
|
5
|
+
options = args.extract_options!
|
6
|
+
@template = args[0]
|
7
|
+
@collection = args[1]
|
8
|
+
@parameter = options[:parameter] || Rails.application.config.pagination.default_parameter
|
9
|
+
@navigation = Navigation.new(args[1], options[:navigation] || Rails.application.config.pagination.default_navigation)
|
10
|
+
end
|
11
|
+
|
12
|
+
def has_multiple_pages?
|
13
|
+
@has_multiple_pages ||= @collection.total_pages > 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_current_page?(page)
|
17
|
+
@collection.current_page == page
|
18
|
+
end
|
19
|
+
|
20
|
+
def has_previous_page?
|
21
|
+
@has_previous_page ||= @collection.previous_page != nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def has_next_page?
|
25
|
+
@has_next_page ||= @collection.next_page != nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def has_first_page?
|
29
|
+
@collection.current_page > 1 and @collection.total_pages > 1
|
30
|
+
end
|
31
|
+
|
32
|
+
def is_first_page?
|
33
|
+
@is_first_page ||= @collection.current_page == 1
|
34
|
+
end
|
35
|
+
|
36
|
+
def has_last_page?
|
37
|
+
@collection.current_page != @collection.last_page and @collection.total_pages > 1
|
38
|
+
end
|
39
|
+
|
40
|
+
def is_last_page?
|
41
|
+
@is_last_page ||= @collection.current_page == total_pages
|
42
|
+
end
|
43
|
+
|
44
|
+
def navigation
|
45
|
+
@navigation
|
46
|
+
end
|
47
|
+
|
48
|
+
def page_path(page)
|
49
|
+
@template.url_for(@template.request.query_parameters.merge(@parameter => page))
|
50
|
+
end
|
51
|
+
|
52
|
+
def page_url(page)
|
53
|
+
@template.url_for(@template.request.query_parameters.merge(@parameter => page, :only_path => false))
|
54
|
+
end
|
55
|
+
|
56
|
+
def current_page_path
|
57
|
+
page_path @collection.current_page
|
58
|
+
end
|
59
|
+
|
60
|
+
def current_page_url
|
61
|
+
page_url @collection.current_page
|
62
|
+
end
|
63
|
+
|
64
|
+
def first_page_path
|
65
|
+
page_path @collection.first_page
|
66
|
+
end
|
67
|
+
|
68
|
+
def first_page_url
|
69
|
+
page_url @collection.first_page
|
70
|
+
end
|
71
|
+
|
72
|
+
def previous_page_path
|
73
|
+
page_path @collection.previous_page
|
74
|
+
end
|
75
|
+
|
76
|
+
def previous_page_url
|
77
|
+
page_url @collection.previous_page
|
78
|
+
end
|
79
|
+
|
80
|
+
def next_page_path
|
81
|
+
page_path @collection.next_page
|
82
|
+
end
|
83
|
+
|
84
|
+
def next_page_url
|
85
|
+
page_url @collection.next_page
|
86
|
+
end
|
87
|
+
|
88
|
+
def last_page_path
|
89
|
+
page_path @collection.last_page
|
90
|
+
end
|
91
|
+
|
92
|
+
def last_page_url
|
93
|
+
page_url @collection.last_page
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module RailsPagination
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
config.pagination = ::ActiveSupport::OrderedOptions.new
|
5
|
+
config.pagination.default_per_page = 30
|
6
|
+
config.pagination.default_parameter = :p
|
7
|
+
config.pagination.default_navigation = 5
|
8
|
+
|
9
|
+
initializer 'rails_pagination' do
|
10
|
+
::ActiveRecord::Base.send :extend, RailsPagination::ActiveRecord::Base
|
11
|
+
::ActionView::Base.send :include, RailsPagination::ActionView::Base
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rails_pagination/action_view/base'
|
2
|
+
require 'rails_pagination/active_record/base'
|
3
|
+
require 'rails_pagination/active_record/relation'
|
4
|
+
require 'rails_pagination/navigation'
|
5
|
+
require 'rails_pagination/pager'
|
6
|
+
require 'rails_pagination/railtie'
|
7
|
+
require 'rails_pagination/version'
|
8
|
+
|
9
|
+
module RailsPagination
|
10
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CollectionEmptyTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup :create_records
|
6
|
+
|
7
|
+
test "should have 1 page" do
|
8
|
+
assert_equal @collection.total_pages, 1
|
9
|
+
end
|
10
|
+
|
11
|
+
test "current page should be 1" do
|
12
|
+
assert_equal @collection.current_page, 1
|
13
|
+
end
|
14
|
+
|
15
|
+
test "first page should be 1" do
|
16
|
+
assert_equal @collection.first_page, 1
|
17
|
+
end
|
18
|
+
|
19
|
+
test "should have no previous page" do
|
20
|
+
assert_nil @collection.previous_page
|
21
|
+
end
|
22
|
+
|
23
|
+
test "should have no next page" do
|
24
|
+
assert_nil @collection.next_page
|
25
|
+
end
|
26
|
+
|
27
|
+
test "last page should be 1" do
|
28
|
+
assert_equal @collection.last_page, 1
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should not be out of bounds" do
|
32
|
+
assert !@collection.out_of_bounds?
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def create_records
|
38
|
+
@collection = Model.page(1).per(2)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CollectionFirstTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup :create_records
|
6
|
+
|
7
|
+
test "should have 5 pages" do
|
8
|
+
assert_equal @collection.total_pages, 5
|
9
|
+
end
|
10
|
+
|
11
|
+
test "current page should be 1" do
|
12
|
+
assert_equal @collection.current_page, 1
|
13
|
+
end
|
14
|
+
|
15
|
+
test "first page should be 1" do
|
16
|
+
assert_equal @collection.first_page, 1
|
17
|
+
end
|
18
|
+
|
19
|
+
test "should have no previous page" do
|
20
|
+
assert_nil @collection.previous_page
|
21
|
+
end
|
22
|
+
|
23
|
+
test "next page should be 2" do
|
24
|
+
assert_equal @collection.next_page, 2
|
25
|
+
end
|
26
|
+
|
27
|
+
test "last page shuold be 5" do
|
28
|
+
assert_equal @collection.last_page, 5
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should not be out of bounds" do
|
32
|
+
assert !@collection.out_of_bounds?
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def create_records
|
38
|
+
10.times.each { |id| Model.create :title => "Record #{id}" }
|
39
|
+
@collection = Model.page(1).per(2)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CollectionLastTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup :create_records
|
6
|
+
|
7
|
+
test "should have 5 pages" do
|
8
|
+
assert_equal @collection.total_pages, 5
|
9
|
+
end
|
10
|
+
|
11
|
+
test "current page should be 5" do
|
12
|
+
assert_equal @collection.current_page, 5
|
13
|
+
end
|
14
|
+
|
15
|
+
test "first page should be 1" do
|
16
|
+
assert_equal @collection.first_page, 1
|
17
|
+
end
|
18
|
+
|
19
|
+
test "should have previous page 4" do
|
20
|
+
assert_equal @collection.previous_page, 4
|
21
|
+
end
|
22
|
+
|
23
|
+
test "should not have next page" do
|
24
|
+
assert_nil @collection.next_page
|
25
|
+
end
|
26
|
+
|
27
|
+
test "last page shuold be 5" do
|
28
|
+
assert_equal @collection.last_page, 5
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should not be out of bounds" do
|
32
|
+
assert !@collection.out_of_bounds?
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def create_records
|
38
|
+
10.times.each { |id| Model.create :title => "Record #{id}" }
|
39
|
+
@collection = Model.page(5).per(2)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CollectionMiddleTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup :create_records
|
6
|
+
|
7
|
+
test "should have 5 pages" do
|
8
|
+
assert_equal @collection.total_pages, 5
|
9
|
+
end
|
10
|
+
|
11
|
+
test "current page should be 3" do
|
12
|
+
assert_equal @collection.current_page, 3
|
13
|
+
end
|
14
|
+
|
15
|
+
test "first page should be 1" do
|
16
|
+
assert_equal @collection.first_page, 1
|
17
|
+
end
|
18
|
+
|
19
|
+
test "should have previous page 2" do
|
20
|
+
assert_equal @collection.previous_page, 2
|
21
|
+
end
|
22
|
+
|
23
|
+
test "should have next page 4" do
|
24
|
+
assert_equal @collection.next_page, 4
|
25
|
+
end
|
26
|
+
|
27
|
+
test "last page shuold be 5" do
|
28
|
+
assert_equal @collection.last_page, 5
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should not be out of bounds" do
|
32
|
+
assert !@collection.out_of_bounds?
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def create_records
|
38
|
+
10.times.each { |id| Model.create :title => "Record #{id}" }
|
39
|
+
@collection = Model.page(3).per(2)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CollectionPaddingTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
test "negative padding should have 1 page" do
|
6
|
+
11.times.each { |id| Model.create :title => "Record #{id}" }
|
7
|
+
|
8
|
+
@collection = Model.page(1).per(15).pad(-4)
|
9
|
+
assert_equal @collection.total_pages, 1
|
10
|
+
|
11
|
+
@collection = Model.page(2).per(15).pad(-4)
|
12
|
+
assert_equal @collection.total_pages, 1
|
13
|
+
|
14
|
+
@collection = Model.page(3).per(15).pad(-4)
|
15
|
+
assert_equal @collection.total_pages, 1
|
16
|
+
end
|
17
|
+
|
18
|
+
test "negative padding should have 2 pages" do
|
19
|
+
12.times.each { |id| Model.create :title => "Record #{id}" }
|
20
|
+
|
21
|
+
@collection = Model.page(1).per(15).pad(-4)
|
22
|
+
assert_equal @collection.total_pages, 2
|
23
|
+
|
24
|
+
@collection = Model.page(2).per(15).pad(-4)
|
25
|
+
assert_equal @collection.total_pages, 2
|
26
|
+
|
27
|
+
@collection = Model.page(3).per(15).pad(-4)
|
28
|
+
assert_equal @collection.total_pages, 2
|
29
|
+
end
|
30
|
+
|
31
|
+
test "negative padding should have 3 pages" do
|
32
|
+
27.times.each { |id| Model.create :title => "Record #{id}" }
|
33
|
+
|
34
|
+
@collection = Model.page(1).per(15).pad(-4)
|
35
|
+
assert_equal @collection.total_pages, 3
|
36
|
+
|
37
|
+
@collection = Model.page(2).per(15).pad(-4)
|
38
|
+
assert_equal @collection.total_pages, 3
|
39
|
+
|
40
|
+
@collection = Model.page(3).per(15).pad(-4)
|
41
|
+
assert_equal @collection.total_pages, 3
|
42
|
+
end
|
43
|
+
|
44
|
+
test "positive padding should have 1 page" do
|
45
|
+
15.times.each { |id| Model.create :title => "Record #{id}" }
|
46
|
+
|
47
|
+
@collection = Model.page(1).per(15).pad(4)
|
48
|
+
assert_equal @collection.total_pages, 1
|
49
|
+
|
50
|
+
@collection = Model.page(2).per(15).pad(4)
|
51
|
+
assert_equal @collection.total_pages, 1
|
52
|
+
|
53
|
+
@collection = Model.page(3).per(15).pad(4)
|
54
|
+
assert_equal @collection.total_pages, 1
|
55
|
+
end
|
56
|
+
|
57
|
+
test "positive padding should have 2 pages" do
|
58
|
+
16.times.each { |id| Model.create :title => "Record #{id}" }
|
59
|
+
|
60
|
+
@collection = Model.page(1).per(15).pad(4)
|
61
|
+
assert_equal @collection.total_pages, 2
|
62
|
+
|
63
|
+
@collection = Model.page(2).per(15).pad(4)
|
64
|
+
assert_equal @collection.total_pages, 2
|
65
|
+
|
66
|
+
@collection = Model.page(3).per(15).pad(4)
|
67
|
+
assert_equal @collection.total_pages, 2
|
68
|
+
end
|
69
|
+
|
70
|
+
test "positive padding should have 3 pages" do
|
71
|
+
31.times.each { |id| Model.create :title => "Record #{id}" }
|
72
|
+
|
73
|
+
@collection = Model.page(1).per(15).pad(4)
|
74
|
+
assert_equal @collection.total_pages, 3
|
75
|
+
|
76
|
+
@collection = Model.page(2).per(15).pad(4)
|
77
|
+
assert_equal @collection.total_pages, 3
|
78
|
+
|
79
|
+
@collection = Model.page(3).per(15).pad(4)
|
80
|
+
assert_equal @collection.total_pages, 3
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|