mislav-will_paginate 2.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +62 -0
- data/LICENSE +18 -0
- data/README.rdoc +135 -0
- data/Rakefile +110 -0
- data/examples/apple-circle.gif +0 -0
- data/examples/index.haml +69 -0
- data/examples/index.html +92 -0
- data/examples/pagination.css +90 -0
- data/examples/pagination.sass +91 -0
- data/init.rb +1 -0
- data/lib/will_paginate.rb +86 -0
- data/lib/will_paginate/array.rb +16 -0
- data/lib/will_paginate/collection.rb +145 -0
- data/lib/will_paginate/core_ext.rb +32 -0
- data/lib/will_paginate/finder.rb +239 -0
- data/lib/will_paginate/named_scope.rb +132 -0
- data/lib/will_paginate/named_scope_patch.rb +39 -0
- data/lib/will_paginate/version.rb +9 -0
- data/lib/will_paginate/view_helpers.rb +341 -0
- data/test/boot.rb +21 -0
- data/test/collection_test.rb +140 -0
- data/test/console +8 -0
- data/test/database.yml +22 -0
- data/test/finder_test.rb +416 -0
- data/test/fixtures/admin.rb +3 -0
- data/test/fixtures/developer.rb +13 -0
- data/test/fixtures/developers_projects.yml +13 -0
- data/test/fixtures/project.rb +15 -0
- data/test/fixtures/projects.yml +6 -0
- data/test/fixtures/replies.yml +29 -0
- data/test/fixtures/reply.rb +7 -0
- data/test/fixtures/schema.rb +38 -0
- data/test/fixtures/topic.rb +6 -0
- data/test/fixtures/topics.yml +30 -0
- data/test/fixtures/user.rb +2 -0
- data/test/fixtures/users.yml +35 -0
- data/test/helper.rb +37 -0
- data/test/lib/activerecord_test_case.rb +36 -0
- data/test/lib/activerecord_test_connector.rb +69 -0
- data/test/lib/load_fixtures.rb +11 -0
- data/test/lib/view_test_process.rb +157 -0
- data/test/view_test.rb +278 -0
- metadata +130 -0
@@ -0,0 +1,239 @@
|
|
1
|
+
require 'will_paginate/core_ext'
|
2
|
+
|
3
|
+
module WillPaginate
|
4
|
+
# A mixin for ActiveRecord::Base. Provides +per_page+ class method
|
5
|
+
# and hooks things up to provide paginating finders.
|
6
|
+
#
|
7
|
+
# Find out more in WillPaginate::Finder::ClassMethods
|
8
|
+
#
|
9
|
+
module Finder
|
10
|
+
def self.included(base)
|
11
|
+
base.extend ClassMethods
|
12
|
+
class << base
|
13
|
+
alias_method_chain :method_missing, :paginate
|
14
|
+
# alias_method_chain :find_every, :paginate
|
15
|
+
define_method(:per_page) { 30 } unless respond_to?(:per_page)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# = Paginating finders for ActiveRecord models
|
20
|
+
#
|
21
|
+
# WillPaginate adds +paginate+, +per_page+ and other methods to
|
22
|
+
# ActiveRecord::Base class methods and associations. It also hooks into
|
23
|
+
# +method_missing+ to intercept pagination calls to dynamic finders such as
|
24
|
+
# +paginate_by_user_id+ and translate them to ordinary finders
|
25
|
+
# (+find_all_by_user_id+ in this case).
|
26
|
+
#
|
27
|
+
# In short, paginating finders are equivalent to ActiveRecord finders; the
|
28
|
+
# only difference is that we start with "paginate" instead of "find" and
|
29
|
+
# that <tt>:page</tt> is required parameter:
|
30
|
+
#
|
31
|
+
# @posts = Post.paginate :all, :page => params[:page], :order => 'created_at DESC'
|
32
|
+
#
|
33
|
+
# In paginating finders, "all" is implicit. There is no sense in paginating
|
34
|
+
# a single record, right? So, you can drop the <tt>:all</tt> argument:
|
35
|
+
#
|
36
|
+
# Post.paginate(...) => Post.find :all
|
37
|
+
# Post.paginate_all_by_something => Post.find_all_by_something
|
38
|
+
# Post.paginate_by_something => Post.find_all_by_something
|
39
|
+
#
|
40
|
+
# == The importance of the <tt>:order</tt> parameter
|
41
|
+
#
|
42
|
+
# In ActiveRecord finders, <tt>:order</tt> parameter specifies columns for
|
43
|
+
# the <tt>ORDER BY</tt> clause in SQL. It is important to have it, since
|
44
|
+
# pagination only makes sense with ordered sets. Without the <tt>ORDER
|
45
|
+
# BY</tt> clause, databases aren't required to do consistent ordering when
|
46
|
+
# performing <tt>SELECT</tt> queries; this is especially true for
|
47
|
+
# PostgreSQL.
|
48
|
+
#
|
49
|
+
# Therefore, make sure you are doing ordering on a column that makes the
|
50
|
+
# most sense in the current context. Make that obvious to the user, also.
|
51
|
+
# For perfomance reasons you will also want to add an index to that column.
|
52
|
+
module ClassMethods
|
53
|
+
# This is the main paginating finder.
|
54
|
+
#
|
55
|
+
# == Special parameters for paginating finders
|
56
|
+
# * <tt>:page</tt> -- REQUIRED, but defaults to 1 if false or nil
|
57
|
+
# * <tt>:per_page</tt> -- defaults to <tt>CurrentModel.per_page</tt> (which is 30 if not overridden)
|
58
|
+
# * <tt>:total_entries</tt> -- use only if you manually count total entries
|
59
|
+
# * <tt>:count</tt> -- additional options that are passed on to +count+
|
60
|
+
# * <tt>:finder</tt> -- name of the ActiveRecord finder used (default: "find")
|
61
|
+
#
|
62
|
+
# All other options (+conditions+, +order+, ...) are forwarded to +find+
|
63
|
+
# and +count+ calls.
|
64
|
+
def paginate(*args, &block)
|
65
|
+
options = args.pop
|
66
|
+
page, per_page, total_entries = wp_parse_options(options)
|
67
|
+
finder = (options[:finder] || 'find').to_s
|
68
|
+
|
69
|
+
if finder == 'find'
|
70
|
+
# an array of IDs may have been given:
|
71
|
+
total_entries ||= (Array === args.first and args.first.size)
|
72
|
+
# :all is implicit
|
73
|
+
args.unshift(:all) if args.empty?
|
74
|
+
end
|
75
|
+
|
76
|
+
WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
|
77
|
+
count_options = options.except :page, :per_page, :total_entries, :finder
|
78
|
+
find_options = count_options.except(:count).update(:offset => pager.offset, :limit => pager.per_page)
|
79
|
+
|
80
|
+
args << find_options
|
81
|
+
# @options_from_last_find = nil
|
82
|
+
pager.replace send(finder, *args, &block)
|
83
|
+
|
84
|
+
# magic counting for user convenience:
|
85
|
+
pager.total_entries = wp_count(count_options, args, finder) unless pager.total_entries
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Iterates through all records by loading one page at a time. This is useful
|
90
|
+
# for migrations or any other use case where you don't want to load all the
|
91
|
+
# records in memory at once.
|
92
|
+
#
|
93
|
+
# It uses +paginate+ internally; therefore it accepts all of its options.
|
94
|
+
# You can specify a starting page with <tt>:page</tt> (default is 1). Default
|
95
|
+
# <tt>:order</tt> is <tt>"id"</tt>, override if necessary.
|
96
|
+
#
|
97
|
+
# See http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord where
|
98
|
+
# Jamis Buck describes this and also uses a more efficient way for MySQL.
|
99
|
+
def paginated_each(options = {}, &block)
|
100
|
+
options = { :order => 'id', :page => 1 }.merge options
|
101
|
+
options[:page] = options[:page].to_i
|
102
|
+
options[:total_entries] = 0 # skip the individual count queries
|
103
|
+
total = 0
|
104
|
+
|
105
|
+
begin
|
106
|
+
collection = paginate(options)
|
107
|
+
total += collection.each(&block).size
|
108
|
+
options[:page] += 1
|
109
|
+
end until collection.size < collection.per_page
|
110
|
+
|
111
|
+
total
|
112
|
+
end
|
113
|
+
|
114
|
+
# Wraps +find_by_sql+ by simply adding LIMIT and OFFSET to your SQL string
|
115
|
+
# based on the params otherwise used by paginating finds: +page+ and
|
116
|
+
# +per_page+.
|
117
|
+
#
|
118
|
+
# Example:
|
119
|
+
#
|
120
|
+
# @developers = Developer.paginate_by_sql ['select * from developers where salary > ?', 80000],
|
121
|
+
# :page => params[:page], :per_page => 3
|
122
|
+
#
|
123
|
+
# A query for counting rows will automatically be generated if you don't
|
124
|
+
# supply <tt>:total_entries</tt>. If you experience problems with this
|
125
|
+
# generated SQL, you might want to perform the count manually in your
|
126
|
+
# application.
|
127
|
+
#
|
128
|
+
def paginate_by_sql(sql, options)
|
129
|
+
WillPaginate::Collection.create(*wp_parse_options(options)) do |pager|
|
130
|
+
query = sanitize_sql(sql)
|
131
|
+
original_query = query.dup
|
132
|
+
# add limit, offset
|
133
|
+
add_limit! query, :offset => pager.offset, :limit => pager.per_page
|
134
|
+
# perfom the find
|
135
|
+
pager.replace find_by_sql(query)
|
136
|
+
|
137
|
+
unless pager.total_entries
|
138
|
+
count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s]+$/mi, ''
|
139
|
+
count_query = "SELECT COUNT(*) FROM (#{count_query})"
|
140
|
+
|
141
|
+
unless ['oracle', 'oci'].include?(self.connection.adapter_name.downcase)
|
142
|
+
count_query << ' AS count_table'
|
143
|
+
end
|
144
|
+
# perform the count query
|
145
|
+
pager.total_entries = count_by_sql(count_query)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def respond_to?(method, include_priv = false) #:nodoc:
|
151
|
+
case method.to_sym
|
152
|
+
when :paginate, :paginate_by_sql
|
153
|
+
true
|
154
|
+
else
|
155
|
+
super(method.to_s.sub(/^paginate/, 'find'), include_priv)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
protected
|
160
|
+
|
161
|
+
def method_missing_with_paginate(method, *args, &block) #:nodoc:
|
162
|
+
# did somebody tried to paginate? if not, let them be
|
163
|
+
unless method.to_s.index('paginate') == 0
|
164
|
+
return method_missing_without_paginate(method, *args, &block)
|
165
|
+
end
|
166
|
+
|
167
|
+
# paginate finders are really just find_* with limit and offset
|
168
|
+
finder = method.to_s.sub('paginate', 'find')
|
169
|
+
finder.sub!('find', 'find_all') if finder.index('find_by_') == 0
|
170
|
+
|
171
|
+
options = args.pop
|
172
|
+
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
|
173
|
+
options = options.dup
|
174
|
+
options[:finder] = finder
|
175
|
+
args << options
|
176
|
+
|
177
|
+
paginate(*args, &block)
|
178
|
+
end
|
179
|
+
|
180
|
+
# Does the not-so-trivial job of finding out the total number of entries
|
181
|
+
# in the database. It relies on the ActiveRecord +count+ method.
|
182
|
+
def wp_count(options, args, finder)
|
183
|
+
excludees = [:count, :order, :limit, :offset, :readonly]
|
184
|
+
unless options[:select] and options[:select] =~ /^\s*DISTINCT\b/i
|
185
|
+
excludees << :select # only exclude the select param if it doesn't begin with DISTINCT
|
186
|
+
end
|
187
|
+
# count expects (almost) the same options as find
|
188
|
+
count_options = options.except *excludees
|
189
|
+
|
190
|
+
# merge the hash found in :count
|
191
|
+
# this allows you to specify :select, :order, or anything else just for the count query
|
192
|
+
count_options.update options[:count] if options[:count]
|
193
|
+
|
194
|
+
# we may have to scope ...
|
195
|
+
counter = Proc.new { count(count_options) }
|
196
|
+
|
197
|
+
# we may be in a model or an association proxy!
|
198
|
+
klass = (@owner and @reflection) ? @reflection.klass : self
|
199
|
+
|
200
|
+
count = if finder.index('find_') == 0 and klass.respond_to?(scoper = finder.sub('find', 'with'))
|
201
|
+
# scope_out adds a 'with_finder' method which acts like with_scope, if it's present
|
202
|
+
# then execute the count with the scoping provided by the with_finder
|
203
|
+
send(scoper, &counter)
|
204
|
+
elsif match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(finder)
|
205
|
+
# extract conditions from calls like "paginate_by_foo_and_bar"
|
206
|
+
attribute_names = extract_attribute_names_from_match(match)
|
207
|
+
conditions = construct_attributes_from_arguments(attribute_names, args)
|
208
|
+
with_scope(:find => { :conditions => conditions }, &counter)
|
209
|
+
else
|
210
|
+
counter.call
|
211
|
+
end
|
212
|
+
|
213
|
+
count.respond_to?(:length) ? count.length : count
|
214
|
+
end
|
215
|
+
|
216
|
+
def wp_parse_options(options) #:nodoc:
|
217
|
+
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
|
218
|
+
options = options.symbolize_keys
|
219
|
+
raise ArgumentError, ':page parameter required' unless options.key? :page
|
220
|
+
|
221
|
+
if options[:count] and options[:total_entries]
|
222
|
+
raise ArgumentError, ':count and :total_entries are mutually exclusive'
|
223
|
+
end
|
224
|
+
|
225
|
+
page = options[:page] || 1
|
226
|
+
per_page = options[:per_page] || self.per_page
|
227
|
+
total = options[:total_entries]
|
228
|
+
[page, per_page, total]
|
229
|
+
end
|
230
|
+
|
231
|
+
private
|
232
|
+
|
233
|
+
# def find_every_with_paginate(options)
|
234
|
+
# @options_from_last_find = options
|
235
|
+
# find_every_without_paginate(options)
|
236
|
+
# end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
## stolen from: http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/named_scope.rb?rev=9084
|
2
|
+
|
3
|
+
module WillPaginate
|
4
|
+
# This is a feature backported from Rails 2.1 because of its usefullness not only with will_paginate,
|
5
|
+
# but in other aspects when managing complex conditions that you want to be reusable.
|
6
|
+
module NamedScope
|
7
|
+
# All subclasses of ActiveRecord::Base have two named_scopes:
|
8
|
+
# * <tt>all</tt>, which is similar to a <tt>find(:all)</tt> query, and
|
9
|
+
# * <tt>scoped</tt>, which allows for the creation of anonymous scopes, on the fly:
|
10
|
+
#
|
11
|
+
# Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)
|
12
|
+
#
|
13
|
+
# These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
|
14
|
+
# intermediate values (scopes) around as first-class objects is convenient.
|
15
|
+
def self.included(base)
|
16
|
+
base.class_eval do
|
17
|
+
extend ClassMethods
|
18
|
+
named_scope :all
|
19
|
+
named_scope :scoped, lambda { |scope| scope }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def scopes #:nodoc:
|
25
|
+
read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
|
26
|
+
end
|
27
|
+
|
28
|
+
# Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
|
29
|
+
# such as <tt>:conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions</tt>.
|
30
|
+
#
|
31
|
+
# class Shirt < ActiveRecord::Base
|
32
|
+
# named_scope :red, :conditions => {:color => 'red'}
|
33
|
+
# named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# The above calls to <tt>named_scope</tt> define class methods <tt>Shirt.red</tt> and <tt>Shirt.dry_clean_only</tt>. <tt>Shirt.red</tt>,
|
37
|
+
# in effect, represents the query <tt>Shirt.find(:all, :conditions => {:color => 'red'})</tt>.
|
38
|
+
#
|
39
|
+
# Unlike Shirt.find(...), however, the object returned by <tt>Shirt.red</tt> is not an Array; it resembles the association object
|
40
|
+
# constructed by a <tt>has_many</tt> declaration. For instance, you can invoke <tt>Shirt.red.find(:first)</tt>, <tt>Shirt.red.count</tt>,
|
41
|
+
# <tt>Shirt.red.find(:all, :conditions => {:size => 'small'})</tt>. Also, just
|
42
|
+
# as with the association objects, name scopes acts like an Array, implementing Enumerable; <tt>Shirt.red.each(&block)</tt>,
|
43
|
+
# <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt> all behave as if Shirt.red really were an Array.
|
44
|
+
#
|
45
|
+
# These named scopes are composable. For instance, <tt>Shirt.red.dry_clean_only</tt> will produce all shirts that are both red and dry clean only.
|
46
|
+
# Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
|
47
|
+
# for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
|
48
|
+
#
|
49
|
+
# All scopes are available as class methods on the ActiveRecord descendent upon which the scopes were defined. But they are also available to
|
50
|
+
# <tt>has_many</tt> associations. If,
|
51
|
+
#
|
52
|
+
# class Person < ActiveRecord::Base
|
53
|
+
# has_many :shirts
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
|
57
|
+
# only shirts.
|
58
|
+
#
|
59
|
+
# Named scopes can also be procedural.
|
60
|
+
#
|
61
|
+
# class Shirt < ActiveRecord::Base
|
62
|
+
# named_scope :colored, lambda { |color|
|
63
|
+
# { :conditions => { :color => color } }
|
64
|
+
# }
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
|
68
|
+
#
|
69
|
+
# Named scopes can also have extensions, just as with <tt>has_many</tt> declarations:
|
70
|
+
#
|
71
|
+
# class Shirt < ActiveRecord::Base
|
72
|
+
# named_scope :red, :conditions => {:color => 'red'} do
|
73
|
+
# def dom_id
|
74
|
+
# 'red_shirts'
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
def named_scope(name, options = {}, &block)
|
80
|
+
scopes[name] = lambda do |parent_scope, *args|
|
81
|
+
Scope.new(parent_scope, case options
|
82
|
+
when Hash
|
83
|
+
options
|
84
|
+
when Proc
|
85
|
+
options.call(*args)
|
86
|
+
end, &block)
|
87
|
+
end
|
88
|
+
(class << self; self end).instance_eval do
|
89
|
+
define_method name do |*args|
|
90
|
+
scopes[name].call(self, *args)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class Scope #:nodoc:
|
97
|
+
attr_reader :proxy_scope, :proxy_options
|
98
|
+
[].methods.each { |m| delegate m, :to => :proxy_found unless m =~ /(^__|^nil\?|^send|class|extend|find|count|sum|average|maximum|minimum|paginate)/ }
|
99
|
+
delegate :scopes, :with_scope, :to => :proxy_scope
|
100
|
+
|
101
|
+
def initialize(proxy_scope, options, &block)
|
102
|
+
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
|
103
|
+
extend Module.new(&block) if block_given?
|
104
|
+
@proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
|
105
|
+
end
|
106
|
+
|
107
|
+
def reload
|
108
|
+
load_found; self
|
109
|
+
end
|
110
|
+
|
111
|
+
protected
|
112
|
+
def proxy_found
|
113
|
+
@found || load_found
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
def method_missing(method, *args, &block)
|
118
|
+
if scopes.include?(method)
|
119
|
+
scopes[method].call(self, *args)
|
120
|
+
else
|
121
|
+
with_scope :find => proxy_options do
|
122
|
+
proxy_scope.send(method, *args, &block)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def load_found
|
128
|
+
@found = find(:all)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
## based on http://dev.rubyonrails.org/changeset/9084
|
2
|
+
|
3
|
+
ActiveRecord::Associations::AssociationProxy.class_eval do
|
4
|
+
protected
|
5
|
+
def with_scope(*args, &block)
|
6
|
+
@reflection.klass.send :with_scope, *args, &block
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
[ ActiveRecord::Associations::AssociationCollection,
|
11
|
+
ActiveRecord::Associations::HasManyThroughAssociation ].each do |klass|
|
12
|
+
klass.class_eval do
|
13
|
+
protected
|
14
|
+
alias :method_missing_without_scopes :method_missing_without_paginate
|
15
|
+
def method_missing_without_paginate(method, *args, &block)
|
16
|
+
if @reflection.klass.scopes.include?(method)
|
17
|
+
@reflection.klass.scopes[method].call(self, *args, &block)
|
18
|
+
else
|
19
|
+
method_missing_without_scopes(method, *args, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Rails 1.2.6
|
26
|
+
ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
|
27
|
+
protected
|
28
|
+
def method_missing(method, *args, &block)
|
29
|
+
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
30
|
+
super
|
31
|
+
elsif @reflection.klass.scopes.include?(method)
|
32
|
+
@reflection.klass.scopes[method].call(self, *args)
|
33
|
+
else
|
34
|
+
@reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
|
35
|
+
@reflection.klass.send(method, *args, &block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end if ActiveRecord::Base.respond_to? :find_first
|
@@ -0,0 +1,341 @@
|
|
1
|
+
require 'will_paginate/core_ext'
|
2
|
+
|
3
|
+
module WillPaginate
|
4
|
+
# = Will Paginate view helpers
|
5
|
+
#
|
6
|
+
# Currently there is only one view helper: +will_paginate+. It renders the
|
7
|
+
# pagination links for the given collection. The helper itself is lightweight
|
8
|
+
# and serves only as a wrapper around link renderer instantiation; the
|
9
|
+
# renderer then does all the hard work of generating the HTML.
|
10
|
+
#
|
11
|
+
# == Global options for helpers
|
12
|
+
#
|
13
|
+
# Options for pagination helpers are optional and get their default values from the
|
14
|
+
# WillPaginate::ViewHelpers.pagination_options hash. You can write to this hash to
|
15
|
+
# override default options on the global level:
|
16
|
+
#
|
17
|
+
# WillPaginate::ViewHelpers.pagination_options[:prev_label] = 'Previous page'
|
18
|
+
#
|
19
|
+
# By putting this into your environment.rb you can easily translate link texts to previous
|
20
|
+
# and next pages, as well as override some other defaults to your liking.
|
21
|
+
module ViewHelpers
|
22
|
+
# default options that can be overridden on the global level
|
23
|
+
@@pagination_options = {
|
24
|
+
:class => 'pagination',
|
25
|
+
:prev_label => '« Previous',
|
26
|
+
:next_label => 'Next »',
|
27
|
+
:inner_window => 4, # links around the current page
|
28
|
+
:outer_window => 1, # links around beginning and end
|
29
|
+
:separator => ' ', # single space is friendly to spiders and non-graphic browsers
|
30
|
+
:param_name => :page,
|
31
|
+
:params => nil,
|
32
|
+
:renderer => 'WillPaginate::LinkRenderer',
|
33
|
+
:page_links => true,
|
34
|
+
:container => true
|
35
|
+
}
|
36
|
+
mattr_reader :pagination_options
|
37
|
+
|
38
|
+
# Renders Digg/Flickr-style pagination for a WillPaginate::Collection
|
39
|
+
# object. Nil is returned if there is only one page in total; no point in
|
40
|
+
# rendering the pagination in that case...
|
41
|
+
#
|
42
|
+
# ==== Options
|
43
|
+
# * <tt>:class</tt> -- CSS class name for the generated DIV (default: "pagination")
|
44
|
+
# * <tt>:prev_label</tt> -- default: "« Previous"
|
45
|
+
# * <tt>:next_label</tt> -- default: "Next »"
|
46
|
+
# * <tt>:inner_window</tt> -- how many links are shown around the current page (default: 4)
|
47
|
+
# * <tt>:outer_window</tt> -- how many links are around the first and the last page (default: 1)
|
48
|
+
# * <tt>:separator</tt> -- string separator for page HTML elements (default: single space)
|
49
|
+
# * <tt>:param_name</tt> -- parameter name for page number in URLs (default: <tt>:page</tt>)
|
50
|
+
# * <tt>:params</tt> -- additional parameters when generating pagination links
|
51
|
+
# (eg. <tt>:controller => "foo", :action => nil</tt>)
|
52
|
+
# * <tt>:renderer</tt> -- class name of the link renderer (default: WillPaginate::LinkRenderer)
|
53
|
+
# * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
|
54
|
+
# * <tt>:container</tt> -- toggles rendering of the DIV container for pagination links, set to
|
55
|
+
# false only when you are rendering your own pagination markup (default: true)
|
56
|
+
# * <tt>:id</tt> -- HTML ID for the container (default: nil). Pass +true+ to have the ID automatically
|
57
|
+
# generated from the class name of objects in collection: for example, paginating
|
58
|
+
# ArticleComment models would yield an ID of "article_comments_pagination".
|
59
|
+
#
|
60
|
+
# All options beside listed ones are passed as HTML attributes to the container
|
61
|
+
# element for pagination links (the DIV). For example:
|
62
|
+
#
|
63
|
+
# <%= will_paginate @posts, :id => 'wp_posts' %>
|
64
|
+
#
|
65
|
+
# ... will result in:
|
66
|
+
#
|
67
|
+
# <div class="pagination" id="wp_posts"> ... </div>
|
68
|
+
#
|
69
|
+
# ==== Using the helper without arguments
|
70
|
+
# If the helper is called without passing in the collection object, it will
|
71
|
+
# try to read from the instance variable inferred by the controller name.
|
72
|
+
# For example, calling +will_paginate+ while the current controller is
|
73
|
+
# PostsController will result in trying to read from the <tt>@posts</tt>
|
74
|
+
# variable. Example:
|
75
|
+
#
|
76
|
+
# <%= will_paginate :id => true %>
|
77
|
+
#
|
78
|
+
# ... will result in <tt>@post</tt> collection getting paginated:
|
79
|
+
#
|
80
|
+
# <div class="pagination" id="posts_pagination"> ... </div>
|
81
|
+
#
|
82
|
+
def will_paginate(collection = nil, options = {})
|
83
|
+
options, collection = collection, nil if collection.is_a? Hash
|
84
|
+
unless collection or !controller
|
85
|
+
collection_name = "@#{controller.controller_name}"
|
86
|
+
collection = instance_variable_get(collection_name)
|
87
|
+
raise ArgumentError, "The #{collection_name} variable appears to be empty. Did you " +
|
88
|
+
"forget to pass the collection object for will_paginate?" unless collection
|
89
|
+
end
|
90
|
+
# early exit if there is nothing to render
|
91
|
+
return nil unless WillPaginate::ViewHelpers.total_pages_for_collection(collection) > 1
|
92
|
+
|
93
|
+
options = options.symbolize_keys.reverse_merge WillPaginate::ViewHelpers.pagination_options
|
94
|
+
# create the renderer instance
|
95
|
+
renderer_class = options[:renderer].to_s.constantize
|
96
|
+
renderer = renderer_class.new collection, options, self
|
97
|
+
# render HTML for pagination
|
98
|
+
renderer.to_html
|
99
|
+
end
|
100
|
+
|
101
|
+
# Wrapper for rendering pagination links at both top and bottom of a block
|
102
|
+
# of content.
|
103
|
+
#
|
104
|
+
# <% paginated_section @posts do %>
|
105
|
+
# <ol id="posts">
|
106
|
+
# <% for post in @posts %>
|
107
|
+
# <li> ... </li>
|
108
|
+
# <% end %>
|
109
|
+
# </ol>
|
110
|
+
# <% end %>
|
111
|
+
#
|
112
|
+
# will result in:
|
113
|
+
#
|
114
|
+
# <div class="pagination"> ... </div>
|
115
|
+
# <ol id="posts">
|
116
|
+
# ...
|
117
|
+
# </ol>
|
118
|
+
# <div class="pagination"> ... </div>
|
119
|
+
#
|
120
|
+
# Arguments are passed to a <tt>will_paginate</tt> call, so the same options
|
121
|
+
# apply. Don't use the <tt>:id</tt> option; otherwise you'll finish with two
|
122
|
+
# blocks of pagination links sharing the same ID (which is invalid HTML).
|
123
|
+
def paginated_section(*args, &block)
|
124
|
+
pagination = will_paginate(*args).to_s
|
125
|
+
content = pagination + capture(&block) + pagination
|
126
|
+
concat content, block.binding
|
127
|
+
end
|
128
|
+
|
129
|
+
# Renders a helpful message with numbers of displayed vs. total entries.
|
130
|
+
# You can use this as a blueprint for your own, similar helpers.
|
131
|
+
#
|
132
|
+
# <%= page_entries_info @posts %>
|
133
|
+
# #-> Displaying entries 6 - 10 of 26 in total
|
134
|
+
def page_entries_info(collection)
|
135
|
+
if collection.total_pages < 2
|
136
|
+
case collection.size
|
137
|
+
when 0; 'No entries found'
|
138
|
+
when 1; 'Displaying <b>1</b> entry'
|
139
|
+
else; "Displaying <b>all #{collection.size}</b> entries"
|
140
|
+
end
|
141
|
+
else
|
142
|
+
%{Displaying entries <b>%d - %d</b> of <b>%d</b> in total} % [
|
143
|
+
collection.offset + 1,
|
144
|
+
collection.offset + collection.length,
|
145
|
+
collection.total_entries
|
146
|
+
]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def self.total_pages_for_collection(collection) #:nodoc:
|
151
|
+
if collection.respond_to?('page_count') and !collection.respond_to?('total_pages')
|
152
|
+
WillPaginate::Deprecation.warn <<-MSG
|
153
|
+
You are using a paginated collection of class #{collection.class.name}
|
154
|
+
which conforms to the old API of WillPaginate::Collection by using
|
155
|
+
`page_count`, while the current method name is `total_pages`. Please
|
156
|
+
upgrade yours or 3rd-party code that provides the paginated collection.
|
157
|
+
MSG
|
158
|
+
class << collection
|
159
|
+
def total_pages; page_count; end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
collection.total_pages
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
# This class does the heavy lifting of actually building the pagination
|
167
|
+
# links. It is used by +will_paginate+ helper internally.
|
168
|
+
class LinkRenderer
|
169
|
+
# * +collection+ is a WillPaginate::Collection instance or any other object
|
170
|
+
# that conforms to that API
|
171
|
+
# * +options+ are forwarded from +will_paginate+ view helper
|
172
|
+
# * +template+ is the reference to the template being rendered
|
173
|
+
def initialize(collection, options, template)
|
174
|
+
@collection = collection
|
175
|
+
@options = options
|
176
|
+
@template = template
|
177
|
+
end
|
178
|
+
|
179
|
+
# Process it! This method returns the complete HTML string which contains
|
180
|
+
# pagination links. Feel free to subclass LinkRenderer and change this
|
181
|
+
# method as you see fit.
|
182
|
+
def to_html
|
183
|
+
links = @options[:page_links] ? windowed_links : []
|
184
|
+
# previous/next buttons
|
185
|
+
links.unshift page_link_or_span(@collection.previous_page, %w(disabled prev_page), @options[:prev_label])
|
186
|
+
links.push page_link_or_span(@collection.next_page, %w(disabled next_page), @options[:next_label])
|
187
|
+
|
188
|
+
html = links.join(@options[:separator])
|
189
|
+
@options[:container] ? @template.content_tag(:div, html, html_attributes) : html
|
190
|
+
end
|
191
|
+
|
192
|
+
# Returns the subset of +options+ this instance was initialized with that
|
193
|
+
# represent HTML attributes for the container element of pagination links.
|
194
|
+
def html_attributes
|
195
|
+
return @html_attributes if @html_attributes
|
196
|
+
@html_attributes = @options.except *(WillPaginate::ViewHelpers.pagination_options.keys - [:class])
|
197
|
+
# pagination of Post models will have the ID of "posts_pagination"
|
198
|
+
if @options[:container] and @options[:id] === true
|
199
|
+
@html_attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination'
|
200
|
+
end
|
201
|
+
@html_attributes
|
202
|
+
end
|
203
|
+
|
204
|
+
protected
|
205
|
+
|
206
|
+
# The gap in page links is represented by:
|
207
|
+
#
|
208
|
+
# <span class="gap">…</span>
|
209
|
+
def gap_marker
|
210
|
+
'<span class="gap">…</span>'
|
211
|
+
end
|
212
|
+
|
213
|
+
# Collects link items for visible page numbers.
|
214
|
+
def windowed_links
|
215
|
+
prev = nil
|
216
|
+
|
217
|
+
visible_page_numbers.inject [] do |links, n|
|
218
|
+
# detect gaps:
|
219
|
+
links << gap_marker if prev and n > prev + 1
|
220
|
+
links << page_link_or_span(n, 'current')
|
221
|
+
prev = n
|
222
|
+
links
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
# Calculates visible page numbers using the <tt>:inner_window</tt> and
|
227
|
+
# <tt>:outer_window</tt> options.
|
228
|
+
def visible_page_numbers
|
229
|
+
inner_window, outer_window = @options[:inner_window].to_i, @options[:outer_window].to_i
|
230
|
+
window_from = current_page - inner_window
|
231
|
+
window_to = current_page + inner_window
|
232
|
+
|
233
|
+
# adjust lower or upper limit if other is out of bounds
|
234
|
+
if window_to > total_pages
|
235
|
+
window_from -= window_to - total_pages
|
236
|
+
window_to = total_pages
|
237
|
+
end
|
238
|
+
if window_from < 1
|
239
|
+
window_to += 1 - window_from
|
240
|
+
window_from = 1
|
241
|
+
window_to = total_pages if window_to > total_pages
|
242
|
+
end
|
243
|
+
|
244
|
+
visible = (1..total_pages).to_a
|
245
|
+
left_gap = (2 + outer_window)...window_from
|
246
|
+
right_gap = (window_to + 1)...(total_pages - outer_window)
|
247
|
+
visible -= left_gap.to_a if left_gap.last - left_gap.first > 1
|
248
|
+
visible -= right_gap.to_a if right_gap.last - right_gap.first > 1
|
249
|
+
|
250
|
+
visible
|
251
|
+
end
|
252
|
+
|
253
|
+
def page_link_or_span(page, span_class, text = nil)
|
254
|
+
text ||= page.to_s
|
255
|
+
classnames = Array[*span_class]
|
256
|
+
|
257
|
+
if page and page != current_page
|
258
|
+
@template.link_to text, url_for(page), :rel => rel_value(page), :class => classnames[1]
|
259
|
+
else
|
260
|
+
@template.content_tag :span, text, :class => classnames.join(' ')
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
# Returns URL params for +page_link_or_span+, taking the current GET params
|
265
|
+
# and <tt>:params</tt> option into account.
|
266
|
+
def url_for(page)
|
267
|
+
unless @url_string
|
268
|
+
@url_params = { :escape => false }
|
269
|
+
# page links should preserve GET parameters
|
270
|
+
stringified_merge @url_params, @template.params if @template.request.get?
|
271
|
+
stringified_merge @url_params, @options[:params] if @options[:params]
|
272
|
+
|
273
|
+
if complex = param_name.index(/[^\w-]/)
|
274
|
+
page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest).
|
275
|
+
parse_query_parameters("#{param_name}=#{page}")
|
276
|
+
|
277
|
+
stringified_merge @url_params, page_param
|
278
|
+
else
|
279
|
+
@url_params[param_name] = 1
|
280
|
+
end
|
281
|
+
|
282
|
+
url = @template.url_for(@url_params)
|
283
|
+
|
284
|
+
if complex
|
285
|
+
@url_string = url.sub(%r!([?&]#{CGI.escape param_name}=)#{page}!, '\1@')
|
286
|
+
return url
|
287
|
+
else
|
288
|
+
@url_string = url
|
289
|
+
@url_params[param_name] = 2
|
290
|
+
@template.url_for(@url_params).split(//).each_with_index do |char, i|
|
291
|
+
if char == '2' and url[i, 1] == '1'
|
292
|
+
@url_string[i] = '@'
|
293
|
+
break
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
# finally!
|
299
|
+
@url_string.sub '@', page.to_s
|
300
|
+
end
|
301
|
+
|
302
|
+
private
|
303
|
+
|
304
|
+
def rel_value(page)
|
305
|
+
case page
|
306
|
+
when @collection.previous_page; 'prev' + (page == 1 ? ' start' : '')
|
307
|
+
when @collection.next_page; 'next'
|
308
|
+
when 1; 'start'
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
def current_page
|
313
|
+
@collection.current_page
|
314
|
+
end
|
315
|
+
|
316
|
+
def total_pages
|
317
|
+
@total_pages ||= WillPaginate::ViewHelpers.total_pages_for_collection(@collection)
|
318
|
+
end
|
319
|
+
|
320
|
+
def param_name
|
321
|
+
@param_name ||= @options[:param_name].to_s
|
322
|
+
end
|
323
|
+
|
324
|
+
def stringified_merge(target, other)
|
325
|
+
other.each do |key, value|
|
326
|
+
key = key.to_s
|
327
|
+
existing = target[key]
|
328
|
+
|
329
|
+
if value.is_a?(Hash)
|
330
|
+
target[key] = existing = {} if existing.nil?
|
331
|
+
if existing.is_a?(Hash)
|
332
|
+
stringified_merge(existing, value)
|
333
|
+
return
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
target[key] = value
|
338
|
+
end
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|