will_paginate 2.3.16 → 3.0.pre
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/CHANGELOG.rdoc +24 -80
- data/LICENSE +1 -1
- data/README.rdoc +125 -0
- data/Rakefile +26 -22
- data/lib/will_paginate/array.rb +25 -8
- data/lib/will_paginate/collection.rb +15 -28
- data/lib/will_paginate/core_ext.rb +26 -0
- data/lib/will_paginate/deprecation.rb +50 -0
- data/lib/will_paginate/finders/active_record.rb +158 -0
- data/lib/will_paginate/finders/active_resource.rb +51 -0
- data/lib/will_paginate/finders/base.rb +112 -0
- data/lib/will_paginate/finders/data_mapper.rb +30 -0
- data/lib/will_paginate/finders/sequel.rb +23 -0
- data/lib/will_paginate/finders.rb +9 -0
- data/lib/will_paginate/railtie.rb +26 -0
- data/lib/will_paginate/version.rb +5 -5
- data/lib/will_paginate/view_helpers/action_view.rb +142 -0
- data/lib/will_paginate/view_helpers/base.rb +126 -0
- data/lib/will_paginate/view_helpers/link_renderer.rb +130 -0
- data/lib/will_paginate/view_helpers/link_renderer_base.rb +83 -0
- data/lib/will_paginate/view_helpers/merb.rb +13 -0
- data/lib/will_paginate/view_helpers.rb +25 -436
- data/lib/will_paginate.rb +10 -84
- data/spec/collection_spec.rb +147 -0
- data/spec/console +8 -0
- data/spec/console_fixtures.rb +8 -0
- data/spec/database.yml +22 -0
- data/spec/finders/active_record_spec.rb +377 -0
- data/spec/finders/active_resource_spec.rb +52 -0
- data/spec/finders/activerecord_test_connector.rb +114 -0
- data/spec/finders/data_mapper_spec.rb +62 -0
- data/spec/finders/data_mapper_test_connector.rb +20 -0
- data/spec/finders/sequel_spec.rb +53 -0
- data/spec/finders/sequel_test_connector.rb +9 -0
- data/spec/finders_spec.rb +76 -0
- data/{test → spec}/fixtures/developer.rb +2 -3
- data/{test → spec}/fixtures/project.rb +2 -6
- data/{test → spec}/fixtures/projects.yml +1 -1
- data/{test → spec}/fixtures/reply.rb +1 -1
- data/spec/fixtures/topic.rb +7 -0
- data/spec/rcov.opts +2 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +74 -0
- data/spec/tasks.rake +60 -0
- data/spec/view_helpers/action_view_spec.rb +345 -0
- data/spec/view_helpers/base_spec.rb +64 -0
- data/spec/view_helpers/link_renderer_base_spec.rb +84 -0
- data/spec/view_helpers/view_example_group.rb +103 -0
- metadata +60 -52
- data/README.md +0 -53
- data/lib/will_paginate/finder.rb +0 -264
- data/lib/will_paginate/i18n.rb +0 -29
- data/lib/will_paginate/locale/en.yml +0 -33
- data/lib/will_paginate/named_scope.rb +0 -170
- data/lib/will_paginate/named_scope_patch.rb +0 -37
- data/lib/will_paginate/per_page.rb +0 -27
- data/test/ci.rb +0 -56
- data/test/collection_test.rb +0 -160
- data/test/console +0 -8
- data/test/database.yml +0 -16
- data/test/finder_test.rb +0 -520
- data/test/fixtures/topic.rb +0 -12
- data/test/gemfiles/Gemfile.1.2 +0 -13
- data/test/gemfiles/Gemfile.1.2.lock +0 -39
- data/test/gemfiles/Gemfile.2.0 +0 -16
- data/test/gemfiles/Gemfile.2.0.lock +0 -28
- data/test/gemfiles/Gemfile.2.1 +0 -16
- data/test/gemfiles/Gemfile.2.1.lock +0 -28
- data/test/gemfiles/Gemfile.2.2 +0 -16
- data/test/gemfiles/Gemfile.2.2.lock +0 -28
- data/test/helper.rb +0 -34
- data/test/lib/activerecord_test_case.rb +0 -38
- data/test/lib/activerecord_test_connector.rb +0 -86
- data/test/lib/load_fixtures.rb +0 -12
- data/test/lib/view_test_process.rb +0 -186
- data/test/view_test.rb +0 -373
- /data/{test → spec}/fixtures/admin.rb +0 -0
- /data/{test → spec}/fixtures/developers_projects.yml +0 -0
- /data/{test → spec}/fixtures/replies.yml +0 -0
- /data/{test → spec}/fixtures/schema.rb +0 -0
- /data/{test → spec}/fixtures/topics.yml +0 -0
- /data/{test → spec}/fixtures/user.rb +0 -0
- /data/{test → spec}/fixtures/users.yml +0 -0
data/CHANGELOG.rdoc
CHANGED
|
@@ -1,81 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* added
|
|
4
|
-
* added
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*
|
|
22
|
-
|
|
23
|
-
= 2.3.12, released 2009-12-01
|
|
24
|
-
|
|
25
|
-
* make view helpers "HTML safe" for Rails 2.3.5 with rails_xss plugin
|
|
26
|
-
|
|
27
|
-
= 2.3.11, released 2009-06-02
|
|
28
|
-
|
|
29
|
-
* fix `enable_actionpack`
|
|
30
|
-
|
|
31
|
-
= 2.3.10, released 2009-05-21
|
|
32
|
-
|
|
33
|
-
* count_by_sql: don't use table alias with any adapters starting with "oracle"
|
|
34
|
-
* Add back "AS count_table" alias to `paginate_by_sql` counter SQL
|
|
35
|
-
|
|
36
|
-
= 2.3.9, released 2009-05-29
|
|
37
|
-
|
|
38
|
-
* remove "AS count_table" alias from `paginate_by_sql` counter SQL
|
|
39
|
-
* Rails 2.3.2 compat: monkeypatch Rails issue #2189 (count breaks has_many :through)
|
|
40
|
-
* fix generation of page URLs that contain the "@" character
|
|
41
|
-
* check for method existance in a ruby 1.8- and 1.9-compatible way
|
|
42
|
-
* load will_paginate view helpers even if ActiveRecord is not loaded
|
|
43
|
-
|
|
44
|
-
== 2.3.8, released 2009-03-09
|
|
45
|
-
|
|
46
|
-
* Rails 2.3 compat: query parameter parsing with Rack
|
|
47
|
-
|
|
48
|
-
== 2.3.7, released 2009-02-09
|
|
49
|
-
|
|
50
|
-
* Removed all unnecessary &block variables since they cause serious memory damage and lots of subsequent gc runs.
|
|
51
|
-
|
|
52
|
-
== 2.3.6, released 2008-10-26
|
|
53
|
-
|
|
54
|
-
* Rails 2.2 fix: stop using `extract_attribute_names_from_match` inernal AR method, it no longer exists
|
|
55
|
-
|
|
56
|
-
== 2.3.5, released 2008-10-07
|
|
57
|
-
|
|
58
|
-
* update the backported named_scope implementation for Rails versions older than 2.1
|
|
59
|
-
* break out of scope of paginated_each() yielded block when used on named scopes
|
|
60
|
-
* fix paginate(:from)
|
|
61
|
-
|
|
62
|
-
== 2.3.4, released 2008-09-16
|
|
63
|
-
|
|
64
|
-
* Removed gem dependency to Active Support (causes trouble with vendored rails).
|
|
65
|
-
* Rails 2.1: fix a failing test and a deprecation warning.
|
|
66
|
-
* Cope with scoped :select when counting.
|
|
67
|
-
|
|
68
|
-
== 2.3.3, released 2008-08-29
|
|
69
|
-
|
|
70
|
-
* Ensure that paginate_by_sql doesn't change the original SQL query.
|
|
71
|
-
* RDoc love (now live at http://gitrdoc.com/mislav/will_paginate/tree/master)
|
|
72
|
-
* Rename :prev_label to :previous_label for consistency. old name still functions but is deprecated
|
|
73
|
-
* ActiveRecord 2.1: Remove :include option from count_all query when it's possible.
|
|
74
|
-
|
|
75
|
-
== 2.3.2, released 2008-05-16
|
|
76
|
-
|
|
77
|
-
* Fixed LinkRenderer#stringified_merge by removing "return" from iterator block
|
|
78
|
-
* Ensure that 'href' values in pagination links are escaped URLs
|
|
1
|
+
== "agnostic" branch
|
|
2
|
+
|
|
3
|
+
* added Sequel support
|
|
4
|
+
* added an initialization hook for Merb
|
|
5
|
+
* refactored URL generation
|
|
6
|
+
* BACKWARDS INCOMPATIBLE: refactored LinkRenderer; also markup changes
|
|
7
|
+
<span class="current">1</span> is now <em>1</em>
|
|
8
|
+
a.prev_page -> a.previous_page (for consistency)
|
|
9
|
+
* "prev_label" -> "previous_label"
|
|
10
|
+
* ported view tests to specs
|
|
11
|
+
* setup Autotest
|
|
12
|
+
* added per_page=(limit) attribute writer to set default per_page
|
|
13
|
+
* Remove :include option from count_all query when possible (Rails 2.1)
|
|
14
|
+
* added WP::ViewHelpers::ActionView and LinkRenderer
|
|
15
|
+
* specs for ViewHelpers::Base and LinkRendererBase
|
|
16
|
+
* created LinkRendererBase that implements windowed visible page numbers logic
|
|
17
|
+
* created WP::ViewHelpers::Base abstract module that implements generic view helpers
|
|
18
|
+
* ported finder tests to specs
|
|
19
|
+
* added WP::Finders::DataMapper
|
|
20
|
+
* added WP::Finders::ActiveRecord mixin for ActiveRecord::Base
|
|
21
|
+
* created WP::Finders::Base abstract module that implements generic pagination logic
|
|
22
|
+
* removed dependency to ActiveSupport
|
|
79
23
|
|
|
80
24
|
== 2.3.1, released 2008-05-04
|
|
81
25
|
|
|
@@ -106,14 +50,14 @@
|
|
|
106
50
|
gem install mislav-will_paginate
|
|
107
51
|
|
|
108
52
|
* extract reusable pagination testing stuff into WillPaginate::View
|
|
109
|
-
* rethink the page URL construction
|
|
53
|
+
* rethink the page URL construction mechanism to be more bulletproof when
|
|
110
54
|
combined with custom routing for page parameter
|
|
111
55
|
* test that anchor parameter can be used in pagination links
|
|
112
56
|
|
|
113
57
|
== 2.2.2, released 2008-04-21
|
|
114
58
|
|
|
115
59
|
* Add support for page parameter in custom routes like "/foo/page/2"
|
|
116
|
-
* Change output of "page_entries_info" on single-page collection and
|
|
60
|
+
* Change output of "page_entries_info" on single-page collection and erroneous
|
|
117
61
|
output with empty collection as reported by Tim Chater
|
|
118
62
|
|
|
119
63
|
== 2.2.1, released 2008-04-08
|
data/LICENSE
CHANGED
data/README.rdoc
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
= The will_paginate Ruby library
|
|
2
|
+
|
|
3
|
+
Pagination is just limiting the number of records loaded and displayed. Why should you let it get in
|
|
4
|
+
your way while developing?
|
|
5
|
+
|
|
6
|
+
This is how you paginate on an ActiveRecord model:
|
|
7
|
+
|
|
8
|
+
Post.paginate :page => 1, :order => 'created_at DESC'
|
|
9
|
+
|
|
10
|
+
Most of the time it's as simple as replacing "find" with "paginate" and specifying the page you want.
|
|
11
|
+
|
|
12
|
+
Some resources to get you started:
|
|
13
|
+
|
|
14
|
+
* The {will_paginate project page}[http://mislav.github.com/will_paginate/];
|
|
15
|
+
* Your mind reels with questions? Join our {Google group}[http://groups.google.com/group/will_paginate];
|
|
16
|
+
* {How to report bugs}[http://github.com/mislav/will_paginate/wikis/report-bugs];
|
|
17
|
+
* {Watch the will_paginate screencast}[http://railscasts.com/episodes/51] by Ryan Bates.
|
|
18
|
+
|
|
19
|
+
== I'm not using Rails; can I still use will_paginate?
|
|
20
|
+
|
|
21
|
+
Absolutely -- although will_paginate started off as a Rails plugin, now it is a <em>completely
|
|
22
|
+
framework-agnostic</em> library with support for Rails and Merb built-in. The core library doesn't
|
|
23
|
+
have any dependences and you can safely use it in any Ruby code.
|
|
24
|
+
|
|
25
|
+
When will_paginate is loaded in an environment where ActiveRecord and ActionView are present, it
|
|
26
|
+
automatically hooks into these frameworks to provide easy pagination on your models and in your
|
|
27
|
+
views. The same mechanism works for Merb applications, too. But, if no known framework is present
|
|
28
|
+
then you have absolute control over what parts of will_paginate do you want to load and where you want
|
|
29
|
+
them mixed in.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
== Installation
|
|
33
|
+
|
|
34
|
+
The recommended way is that you get the gem hosted on {gems.github.com}[http://gems.github.com/]:
|
|
35
|
+
|
|
36
|
+
gem install mislav-will_paginate
|
|
37
|
+
|
|
38
|
+
In <b>Rails 2.1</b>, add a gem dependency:
|
|
39
|
+
|
|
40
|
+
# for Rails 2.1 and newer
|
|
41
|
+
config.gem 'mislav-will_paginate', :lib => 'will_paginate', :version => '~> 3.0'
|
|
42
|
+
|
|
43
|
+
If you're using Rails 2.0 or older, or any other Ruby framework, just add a simple require to a file
|
|
44
|
+
that initializes your application. For example, in Rails you would put this at the end of
|
|
45
|
+
"config/environment.rb".
|
|
46
|
+
|
|
47
|
+
gem 'mislav-will_paginate', '~> 3.0'
|
|
48
|
+
require 'will_paginate'
|
|
49
|
+
|
|
50
|
+
That's it. Remember to install the gem on <strong>all</strong> machines that you are deploying to.
|
|
51
|
+
|
|
52
|
+
<i>There are extensive {installation
|
|
53
|
+
instructions}[http://github.com/mislav/will_paginate/wikis/installation] on {the
|
|
54
|
+
wiki}[http://github.com/mislav/will_paginate/wikis].</i>
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
== Example usage
|
|
58
|
+
|
|
59
|
+
Typical usage involves a paginating find in the controller:
|
|
60
|
+
|
|
61
|
+
@posts = Post.paginate :page => params[:page], :order => 'updated_at DESC'
|
|
62
|
+
|
|
63
|
+
It's true: +paginate+ works just like +find+ -- it just doesn't fetch all the records. Don't forget
|
|
64
|
+
to tell it which page you want, or it will complain! Read more in WillPaginate::Finders.
|
|
65
|
+
|
|
66
|
+
Render the posts in your view like you would normally do, and when you need to render pagination,
|
|
67
|
+
just stick this in:
|
|
68
|
+
|
|
69
|
+
<%= will_paginate @posts %>
|
|
70
|
+
|
|
71
|
+
You're done. Read more in WillPaginate::ViewHelpers::Base.
|
|
72
|
+
|
|
73
|
+
How does it know how much items to fetch per page? It asks your model by calling its
|
|
74
|
+
+per_page+ class method. You can define it like this:
|
|
75
|
+
|
|
76
|
+
class Post < ActiveRecord::Base
|
|
77
|
+
self.per_page = 50
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
... or don't worry about it at all. WillPaginate defines it to be <strong>30</strong> by default. You can
|
|
81
|
+
always specify the count explicitly when calling +paginate+:
|
|
82
|
+
|
|
83
|
+
Post.paginate :page => params[:page], :per_page => 50
|
|
84
|
+
|
|
85
|
+
The +paginate+ finder wraps the original finder and returns your result set that now has some new
|
|
86
|
+
properties. You can use the collection as you would use any other array. WillPaginate view helpers
|
|
87
|
+
also need that collection object to be able to render pagination:
|
|
88
|
+
|
|
89
|
+
<ol>
|
|
90
|
+
<% for post in @posts -%>
|
|
91
|
+
<li>Render `post` in some nice way.</li>
|
|
92
|
+
<% end -%>
|
|
93
|
+
</ol>
|
|
94
|
+
|
|
95
|
+
<p>Now let's render us some pagination!</p>
|
|
96
|
+
<%= will_paginate @posts %>
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
== Authors and credits
|
|
100
|
+
|
|
101
|
+
The original author of will_paginate was PJ Hyett, who later handed over development to Mislav
|
|
102
|
+
Marohnić. (The library was completely rewritten since then.)
|
|
103
|
+
|
|
104
|
+
All these people helped making will_paginate what it is now with their code contributions or just
|
|
105
|
+
simply awesome ideas:
|
|
106
|
+
|
|
107
|
+
Chris Wanstrath, Dr. Nic Williams, K. Adam Christensen, Mike Garey, Bence Golda, Matt Aimonetti,
|
|
108
|
+
Charles Brian Quinn, Desi McAdam, James Coglan, Matijs van Zuijlen, Maria, Brendan Ribera, Todd
|
|
109
|
+
Willey, Bryan Helmkamp, Jan Berkel, Lourens Naudé, Rick Olson, Russell Norris, Piotr Usewicz, Chris
|
|
110
|
+
Eppstein, Brandon Arbini, Denis Barushev, Paul Barry, Ben Pickles, Ken Collins, Lida Tang and Pieter
|
|
111
|
+
Noordhuis.
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
== Usable pagination in the UI
|
|
115
|
+
|
|
116
|
+
There are example CSS styles to get you started on the will_paginate project page.
|
|
117
|
+
|
|
118
|
+
More reading about pagination as design pattern:
|
|
119
|
+
|
|
120
|
+
* {Pagination 101}[http://kurafire.net/log/archive/2007/06/22/pagination-101];
|
|
121
|
+
* {Pagination gallery}[http://www.smashingmagazine.com/2007/11/16/pagination-gallery-examples-and-good-practices/] featured on Smashing Magazine;
|
|
122
|
+
* {Pagination design pattern}[http://developer.yahoo.com/ypatterns/parent.php?pattern=pagination] on Yahoo Design Pattern Library.
|
|
123
|
+
|
|
124
|
+
Want to discuss, request features, ask questions? Join the {Google
|
|
125
|
+
group}[http://groups.google.com/group/will_paginate].
|
data/Rakefile
CHANGED
|
@@ -1,28 +1,32 @@
|
|
|
1
|
-
require 'rake/
|
|
1
|
+
require 'rake/rdoctask'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
task :default => :test
|
|
3
|
+
load 'spec/tasks.rake'
|
|
5
4
|
|
|
6
|
-
desc '
|
|
7
|
-
|
|
8
|
-
if ENV['DB'] and ENV['DB'] != 'sqlite3'
|
|
9
|
-
t.pattern = %w[test/finder_test.rb]
|
|
10
|
-
else
|
|
11
|
-
t.pattern = 'test/**/*_test.rb'
|
|
12
|
-
end
|
|
13
|
-
t.libs << 'test'
|
|
14
|
-
end
|
|
5
|
+
desc 'Default: run specs.'
|
|
6
|
+
task :default => :spec
|
|
15
7
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
8
|
+
desc 'Generate RDoc documentation for the will_paginate plugin.'
|
|
9
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
10
|
+
rdoc.rdoc_files.include('README.rdoc', 'LICENSE', 'CHANGELOG.rdoc').
|
|
11
|
+
include('lib/**/*.rb').
|
|
12
|
+
exclude('lib/will_paginate/finders/active_record/named_scope*').
|
|
13
|
+
exclude('lib/will_paginate/finders/sequel.rb').
|
|
14
|
+
exclude('lib/will_paginate/view_helpers/merb.rb').
|
|
15
|
+
exclude('lib/will_paginate/deprecation.rb').
|
|
16
|
+
exclude('lib/will_paginate/core_ext.rb').
|
|
17
|
+
exclude('lib/will_paginate/version.rb')
|
|
18
|
+
|
|
19
|
+
rdoc.main = "README.rdoc" # page to start on
|
|
20
|
+
rdoc.title = "will_paginate documentation"
|
|
21
|
+
|
|
22
|
+
rdoc.rdoc_dir = 'doc' # rdoc output folder
|
|
23
|
+
rdoc.options << '--inline-source' << '--charset=UTF-8'
|
|
24
|
+
rdoc.options << '--webcvs=http://github.com/mislav/will_paginate/tree/master/'
|
|
25
|
+
end
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
task :website do
|
|
28
|
+
Dir.chdir('website') do
|
|
29
|
+
%x(haml index.haml index.html)
|
|
30
|
+
%x(sass pagination.sass pagination.css)
|
|
27
31
|
end
|
|
28
32
|
end
|
data/lib/will_paginate/array.rb
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
require 'will_paginate/collection'
|
|
2
|
-
require 'will_paginate/per_page'
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
class Array
|
|
4
|
+
# Paginates a static array (extracting a subset of it). The result is a
|
|
5
|
+
# WillPaginate::Collection instance, which is an array with a few more
|
|
6
|
+
# properties about its paginated state.
|
|
7
|
+
#
|
|
8
|
+
# Parameters:
|
|
9
|
+
# * <tt>:page</tt> - current page, defaults to 1
|
|
10
|
+
# * <tt>:per_page</tt> - limit of items per page, defaults to 30
|
|
11
|
+
# * <tt>:total_entries</tt> - total number of items in the array, defaults to
|
|
12
|
+
# <tt>array.length</tt> (obviously)
|
|
13
|
+
#
|
|
14
|
+
# Example:
|
|
15
|
+
# arr = ['a', 'b', 'c', 'd', 'e']
|
|
16
|
+
# paged = arr.paginate(:per_page => 2) #-> ['a', 'b']
|
|
17
|
+
# paged.total_entries #-> 5
|
|
18
|
+
# arr.paginate(:page => 2, :per_page => 2) #-> ['c', 'd']
|
|
19
|
+
# arr.paginate(:page => 3, :per_page => 2) #-> ['e']
|
|
20
|
+
#
|
|
21
|
+
# This method was originally {suggested by Desi
|
|
22
|
+
# McAdam}[http://www.desimcadam.com/archives/8] and later proved to be the
|
|
23
|
+
# most useful method of will_paginate library.
|
|
6
24
|
def paginate(options = {})
|
|
7
25
|
raise ArgumentError, "parameter hash expected (got #{options.inspect})" unless Hash === options
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
WillPaginate::Collection.create(page, per_page, total) do |pager|
|
|
26
|
+
|
|
27
|
+
WillPaginate::Collection.create options[:page] || 1,
|
|
28
|
+
options[:per_page] || 30,
|
|
29
|
+
options[:total_entries] || self.length do |pager|
|
|
13
30
|
pager.replace self[pager.offset, pager.per_page].to_a
|
|
14
31
|
end
|
|
15
32
|
end
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require 'will_paginate/per_page'
|
|
2
|
-
|
|
3
1
|
module WillPaginate
|
|
4
2
|
# = Invalid page number error
|
|
5
3
|
# This is an ArgumentError raised in case a page was requested that is either
|
|
@@ -19,24 +17,8 @@ module WillPaginate
|
|
|
19
17
|
# requested. Use <tt>WillPaginate::Collection#out_of_bounds?</tt> method to
|
|
20
18
|
# check for those cases and manually deal with them as you see fit.
|
|
21
19
|
class InvalidPage < ArgumentError
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def self.validate(page_value, per_page_value)
|
|
26
|
-
page = page_value.to_i
|
|
27
|
-
raise self.new(page_value, page) if page < 1
|
|
28
|
-
per_page = per_page_value.to_i
|
|
29
|
-
offset = (page - 1) * per_page
|
|
30
|
-
raise self, "invalid offset: #{offset.inspect}" if offset < 0 or offset > BIGINT
|
|
31
|
-
[page, per_page]
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def initialize(value, page_num = nil)
|
|
35
|
-
if page_num
|
|
36
|
-
super "#{value.inspect} given as value, which translates to '#{page_num}' as page number"
|
|
37
|
-
else
|
|
38
|
-
super value
|
|
39
|
-
end
|
|
20
|
+
def initialize(page, page_num) #:nodoc:
|
|
21
|
+
super "#{page.inspect} given as value, which translates to '#{page_num}' as page number"
|
|
40
22
|
end
|
|
41
23
|
end
|
|
42
24
|
|
|
@@ -51,10 +33,12 @@ module WillPaginate
|
|
|
51
33
|
#
|
|
52
34
|
# If you are writing a library that provides a collection which you would like
|
|
53
35
|
# to conform to this API, you don't have to copy these methods over; simply
|
|
54
|
-
# make your plugin/gem dependant on
|
|
36
|
+
# make your plugin/gem dependant on the "will_paginate" gem:
|
|
55
37
|
#
|
|
38
|
+
# gem 'will_paginate'
|
|
56
39
|
# require 'will_paginate/collection'
|
|
57
|
-
#
|
|
40
|
+
#
|
|
41
|
+
# # now use WillPaginate::Collection directly or subclass it
|
|
58
42
|
class Collection < Array
|
|
59
43
|
attr_reader :current_page, :per_page, :total_entries, :total_pages
|
|
60
44
|
|
|
@@ -62,8 +46,12 @@ module WillPaginate
|
|
|
62
46
|
# and the total number of entries. The last argument is optional because it
|
|
63
47
|
# is best to do lazy counting; in other words, count *conditionally* after
|
|
64
48
|
# populating the collection using the +replace+ method.
|
|
65
|
-
def initialize(page, per_page
|
|
66
|
-
@current_page
|
|
49
|
+
def initialize(page, per_page, total = nil)
|
|
50
|
+
@current_page = page.to_i
|
|
51
|
+
raise InvalidPage.new(page, @current_page) if @current_page < 1
|
|
52
|
+
@per_page = per_page.to_i
|
|
53
|
+
raise ArgumentError, "`per_page` setting cannot be less than 1 (#{@per_page} given)" if @per_page < 1
|
|
54
|
+
|
|
67
55
|
self.total_entries = total if total
|
|
68
56
|
end
|
|
69
57
|
|
|
@@ -94,7 +82,7 @@ module WillPaginate
|
|
|
94
82
|
#
|
|
95
83
|
# The Array#paginate API has since then changed, but this still serves as a
|
|
96
84
|
# fine example of WillPaginate::Collection usage.
|
|
97
|
-
def self.create(page, per_page, total = nil)
|
|
85
|
+
def self.create(page, per_page, total = nil, &block)
|
|
98
86
|
pager = new(page, per_page, total)
|
|
99
87
|
yield pager
|
|
100
88
|
pager
|
|
@@ -110,7 +98,7 @@ module WillPaginate
|
|
|
110
98
|
# Current offset of the paginated collection. If we're on the first page,
|
|
111
99
|
# it is always 0. If we're on the 2nd page and there are 30 entries per page,
|
|
112
100
|
# the offset is 30. This property is useful if you want to render ordinals
|
|
113
|
-
#
|
|
101
|
+
# besides your records: simply start with offset + 1.
|
|
114
102
|
def offset
|
|
115
103
|
(current_page - 1) * per_page
|
|
116
104
|
end
|
|
@@ -124,8 +112,7 @@ module WillPaginate
|
|
|
124
112
|
def next_page
|
|
125
113
|
current_page < total_pages ? (current_page + 1) : nil
|
|
126
114
|
end
|
|
127
|
-
|
|
128
|
-
# sets the <tt>total_entries</tt> property and calculates <tt>total_pages</tt>
|
|
115
|
+
|
|
129
116
|
def total_entries=(number)
|
|
130
117
|
@total_entries = number.to_i
|
|
131
118
|
@total_pages = (@total_entries / per_page.to_f).ceil
|
|
@@ -12,6 +12,8 @@ class Array
|
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
+
## everything below copied from ActiveSupport so we don't depend on it ##
|
|
16
|
+
|
|
15
17
|
unless Hash.instance_methods.include_method? :except
|
|
16
18
|
Hash.class_eval do
|
|
17
19
|
# Returns a new hash without the given keys.
|
|
@@ -41,3 +43,27 @@ unless Hash.instance_methods.include_method? :slice
|
|
|
41
43
|
end
|
|
42
44
|
end
|
|
43
45
|
end
|
|
46
|
+
|
|
47
|
+
unless String.instance_methods.include_method? :constantize
|
|
48
|
+
String.class_eval do
|
|
49
|
+
def constantize
|
|
50
|
+
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
|
|
51
|
+
raise NameError, "#{self.inspect} is not a valid constant name!"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
Object.module_eval("::#{$1}", __FILE__, __LINE__)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
unless String.instance_methods.include_method? :underscore
|
|
60
|
+
String.class_eval do
|
|
61
|
+
def underscore
|
|
62
|
+
self.to_s.gsub(/::/, '/').
|
|
63
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
|
64
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
|
65
|
+
tr("-", "_").
|
|
66
|
+
downcase
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# borrowed from ActiveSupport::Deprecation
|
|
2
|
+
module WillPaginate
|
|
3
|
+
module Deprecation
|
|
4
|
+
def self.debug() @debug; end
|
|
5
|
+
def self.debug=(value) @debug = value; end
|
|
6
|
+
self.debug = false
|
|
7
|
+
|
|
8
|
+
# Choose the default warn behavior according to Rails.env.
|
|
9
|
+
# Ignore deprecation warnings in production.
|
|
10
|
+
BEHAVIORS = {
|
|
11
|
+
'test' => Proc.new { |message, callstack|
|
|
12
|
+
$stderr.puts(message)
|
|
13
|
+
$stderr.puts callstack.join("\n ") if debug
|
|
14
|
+
},
|
|
15
|
+
'development' => Proc.new { |message, callstack|
|
|
16
|
+
logger = defined?(::RAILS_DEFAULT_LOGGER) ? ::RAILS_DEFAULT_LOGGER : Logger.new($stderr)
|
|
17
|
+
logger.warn message
|
|
18
|
+
logger.debug callstack.join("\n ") if debug
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
def self.warn(message, callstack = caller)
|
|
23
|
+
if behavior
|
|
24
|
+
message = 'WillPaginate: ' + message.strip.gsub(/\s+/, ' ')
|
|
25
|
+
behavior.call(message, callstack)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.default_behavior
|
|
30
|
+
if defined?(::Rails)
|
|
31
|
+
BEHAVIORS[::Rails.env.to_s]
|
|
32
|
+
else
|
|
33
|
+
BEHAVIORS['test']
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Behavior is a block that takes a message argument.
|
|
38
|
+
def self.behavior() @behavior; end
|
|
39
|
+
def self.behavior=(value) @behavior = value; end
|
|
40
|
+
self.behavior = default_behavior
|
|
41
|
+
|
|
42
|
+
def self.silence
|
|
43
|
+
old_behavior = self.behavior
|
|
44
|
+
self.behavior = nil
|
|
45
|
+
yield
|
|
46
|
+
ensure
|
|
47
|
+
self.behavior = old_behavior
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|