will_paginate 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of will_paginate might be problematic. Click here for more details.
- data/CHANGELOG +3 -0
- data/LICENSE +18 -0
- data/Manifest.txt +34 -0
- data/README +152 -0
- data/Rakefile +65 -0
- data/config/release.rb +82 -0
- data/lib/will_paginate/collection.rb +132 -0
- data/lib/will_paginate/core_ext.rb +80 -0
- data/lib/will_paginate/finder.rb +214 -0
- data/lib/will_paginate/version.rb +9 -0
- data/lib/will_paginate/view_helpers.rb +218 -0
- data/lib/will_paginate.rb +63 -0
- data/setup.rb +1585 -0
- data/test/array_pagination_test.rb +131 -0
- data/test/boot.rb +23 -0
- data/test/console +9 -0
- data/test/finder_test.rb +322 -0
- data/test/fixtures/admin.rb +3 -0
- data/test/fixtures/developer.rb +11 -0
- data/test/fixtures/developers_projects.yml +13 -0
- data/test/fixtures/project.rb +15 -0
- data/test/fixtures/projects.yml +7 -0
- data/test/fixtures/replies.yml +29 -0
- data/test/fixtures/reply.rb +5 -0
- data/test/fixtures/schema.rb +38 -0
- data/test/fixtures/topic.rb +4 -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 +25 -0
- data/test/lib/activerecord_test_case.rb +23 -0
- data/test/lib/activerecord_test_connector.rb +60 -0
- data/test/lib/load_fixtures.rb +13 -0
- data/test/pagination_test.rb +257 -0
- metadata +99 -0
@@ -0,0 +1,214 @@
|
|
1
|
+
require 'will_paginate/core_ext'
|
2
|
+
|
3
|
+
module WillPaginate
|
4
|
+
# A mixin for ActiveRecord::Base. Provides +per_page+ class method
|
5
|
+
# and makes +paginate+ finders possible with some method_missing magic.
|
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+ and +per_page+ methods to ActiveRecord::Base
|
22
|
+
# class methods and associations. It also hooks into +method_missing+ to
|
23
|
+
# 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
|
+
# Wraps +find_by_sql+ by simply adding LIMIT and OFFSET to your SQL string
|
90
|
+
# based on the params otherwise used by paginating finds: +page+ and
|
91
|
+
# +per_page+.
|
92
|
+
#
|
93
|
+
# Example:
|
94
|
+
#
|
95
|
+
# @developers = Developer.paginate_by_sql ['select * from developers where salary > ?', 80000],
|
96
|
+
# :page => params[:page], :per_page => 3
|
97
|
+
#
|
98
|
+
# A query for counting rows will automatically be generated if you don't
|
99
|
+
# supply <tt>:total_entries</tt>. If you experience problems with this
|
100
|
+
# generated SQL, you might want to perform the count manually in your
|
101
|
+
# application.
|
102
|
+
#
|
103
|
+
def paginate_by_sql(sql, options)
|
104
|
+
WillPaginate::Collection.create(*wp_parse_options(options)) do |pager|
|
105
|
+
query = sanitize_sql(sql)
|
106
|
+
original_query = query.dup
|
107
|
+
# add limit, offset
|
108
|
+
add_limit! query, :offset => pager.offset, :limit => pager.per_page
|
109
|
+
# perfom the find
|
110
|
+
pager.replace find_by_sql(query)
|
111
|
+
|
112
|
+
unless pager.total_entries
|
113
|
+
count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s]+$/mi, ''
|
114
|
+
count_query = "SELECT COUNT(*) FROM (#{count_query})"
|
115
|
+
|
116
|
+
unless ['oracle', 'oci'].include?(self.connection.adapter_name.downcase)
|
117
|
+
count_query << ' AS count_table'
|
118
|
+
end
|
119
|
+
# perform the count query
|
120
|
+
pager.total_entries = count_by_sql(count_query)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def respond_to?(method, include_priv = false) #:nodoc:
|
126
|
+
case method.to_sym
|
127
|
+
when :paginate, :paginate_by_sql
|
128
|
+
true
|
129
|
+
else
|
130
|
+
super(method.to_s.sub(/^paginate/, 'find'), include_priv)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
protected
|
135
|
+
|
136
|
+
def method_missing_with_paginate(method, *args, &block) #:nodoc:
|
137
|
+
# did somebody tried to paginate? if not, let them be
|
138
|
+
unless method.to_s.index('paginate') == 0
|
139
|
+
return method_missing_without_paginate(method, *args, &block)
|
140
|
+
end
|
141
|
+
|
142
|
+
# paginate finders are really just find_* with limit and offset
|
143
|
+
finder = method.to_s.sub('paginate', 'find')
|
144
|
+
finder.sub!('find', 'find_all') if finder.index('find_by_') == 0
|
145
|
+
|
146
|
+
options = args.pop
|
147
|
+
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
|
148
|
+
options = options.dup
|
149
|
+
options[:finder] = finder
|
150
|
+
args << options
|
151
|
+
|
152
|
+
paginate(*args, &block)
|
153
|
+
end
|
154
|
+
|
155
|
+
# Does the not-so-trivial job of finding out the total number of entries
|
156
|
+
# in the database. It relies on the ActiveRecord +count+ method.
|
157
|
+
def wp_count(options, args, finder)
|
158
|
+
excludees = [:count, :order, :limit, :offset, :readonly]
|
159
|
+
unless options[:select] and options[:select] =~ /^\s*DISTINCT\b/i
|
160
|
+
excludees << :select # only exclude the select param if it doesn't begin with DISTINCT
|
161
|
+
end
|
162
|
+
# count expects (almost) the same options as find
|
163
|
+
count_options = options.except *excludees
|
164
|
+
|
165
|
+
# merge the hash found in :count
|
166
|
+
# this allows you to specify :select, :order, or anything else just for the count query
|
167
|
+
count_options.update options[:count] if options[:count]
|
168
|
+
|
169
|
+
# we may have to scope ...
|
170
|
+
counter = Proc.new { count(count_options) }
|
171
|
+
|
172
|
+
# we may be in a model or an association proxy!
|
173
|
+
klass = (@owner and @reflection) ? @reflection.klass : self
|
174
|
+
|
175
|
+
count = if finder.index('find_') == 0 and klass.respond_to?(scoper = finder.sub('find', 'with'))
|
176
|
+
# scope_out adds a 'with_finder' method which acts like with_scope, if it's present
|
177
|
+
# then execute the count with the scoping provided by the with_finder
|
178
|
+
send(scoper, &counter)
|
179
|
+
elsif match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(finder)
|
180
|
+
# extract conditions from calls like "paginate_by_foo_and_bar"
|
181
|
+
attribute_names = extract_attribute_names_from_match(match)
|
182
|
+
conditions = construct_attributes_from_arguments(attribute_names, args)
|
183
|
+
with_scope(:find => { :conditions => conditions }, &counter)
|
184
|
+
else
|
185
|
+
counter.call
|
186
|
+
end
|
187
|
+
|
188
|
+
count.respond_to?(:length) ? count.length : count
|
189
|
+
end
|
190
|
+
|
191
|
+
def wp_parse_options(options) #:nodoc:
|
192
|
+
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
|
193
|
+
options = options.symbolize_keys
|
194
|
+
raise ArgumentError, ':page parameter required' unless options.key? :page
|
195
|
+
|
196
|
+
if options[:count] and options[:total_entries]
|
197
|
+
raise ArgumentError, ':count and :total_entries are mutually exclusive'
|
198
|
+
end
|
199
|
+
|
200
|
+
page = options[:page] || 1
|
201
|
+
per_page = options[:per_page] || self.per_page
|
202
|
+
total = options[:total_entries]
|
203
|
+
[page, per_page, total]
|
204
|
+
end
|
205
|
+
|
206
|
+
private
|
207
|
+
|
208
|
+
# def find_every_with_paginate(options)
|
209
|
+
# @options_from_last_find = options
|
210
|
+
# find_every_without_paginate(options)
|
211
|
+
# end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
@@ -0,0 +1,218 @@
|
|
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 specify the collection object for will_paginate?" unless collection
|
89
|
+
end
|
90
|
+
# early exit if there is nothing to render
|
91
|
+
return nil unless collection.page_count > 1
|
92
|
+
options = options.symbolize_keys.reverse_merge WillPaginate::ViewHelpers.pagination_options
|
93
|
+
# create the renderer instance
|
94
|
+
renderer_class = options[:renderer].to_s.constantize
|
95
|
+
renderer = renderer_class.new collection, options, self
|
96
|
+
# render HTML for pagination
|
97
|
+
renderer.to_html
|
98
|
+
end
|
99
|
+
|
100
|
+
# Renders a helpful message with numbers of displayed vs. total entries.
|
101
|
+
# You can use this as a blueprint for your own, similar helpers.
|
102
|
+
#
|
103
|
+
# <%= page_entries_info @posts %>
|
104
|
+
# #-> Displaying entries 6 - 10 of 26 in total
|
105
|
+
def page_entries_info(collection)
|
106
|
+
%{Displaying entries <b>%d - %d</b> of <b>%d</b> in total} % [
|
107
|
+
collection.offset + 1,
|
108
|
+
collection.offset + collection.length,
|
109
|
+
collection.total_entries
|
110
|
+
]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# This class does the heavy lifting of actually building the pagination
|
115
|
+
# links. It is used by +will_paginate+ helper internally.
|
116
|
+
class LinkRenderer
|
117
|
+
|
118
|
+
def initialize(collection, options, template)
|
119
|
+
@collection = collection
|
120
|
+
@options = options
|
121
|
+
@template = template
|
122
|
+
end
|
123
|
+
|
124
|
+
def to_html
|
125
|
+
links = @options[:page_links] ? windowed_links : []
|
126
|
+
# previous/next buttons
|
127
|
+
links.unshift page_link_or_span(@collection.previous_page, 'disabled', @options[:prev_label])
|
128
|
+
links.push page_link_or_span(@collection.next_page, 'disabled', @options[:next_label])
|
129
|
+
|
130
|
+
html = links.join(@options[:separator])
|
131
|
+
@options[:container] ? @template.content_tag(:div, html, html_attributes) : html
|
132
|
+
end
|
133
|
+
|
134
|
+
def html_attributes
|
135
|
+
return @html_attributes if @html_attributes
|
136
|
+
@html_attributes = @options.except *(WillPaginate::ViewHelpers.pagination_options.keys - [:class])
|
137
|
+
# pagination of Post models will have the ID of "posts_pagination"
|
138
|
+
if @options[:container] and @options[:id] === true
|
139
|
+
@html_attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination'
|
140
|
+
end
|
141
|
+
@html_attributes
|
142
|
+
end
|
143
|
+
|
144
|
+
protected
|
145
|
+
|
146
|
+
def gap_marker; '...'; end
|
147
|
+
|
148
|
+
def windowed_links
|
149
|
+
prev = nil
|
150
|
+
|
151
|
+
visible_page_numbers.inject [] do |links, n|
|
152
|
+
# detect gaps:
|
153
|
+
links << gap_marker if prev and n > prev + 1
|
154
|
+
links << page_link_or_span(n)
|
155
|
+
prev = n
|
156
|
+
links
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def visible_page_numbers
|
161
|
+
inner_window, outer_window = @options[:inner_window].to_i, @options[:outer_window].to_i
|
162
|
+
window_from = current_page - inner_window
|
163
|
+
window_to = current_page + inner_window
|
164
|
+
|
165
|
+
# adjust lower or upper limit if other is out of bounds
|
166
|
+
if window_to > total_pages
|
167
|
+
window_from -= window_to - total_pages
|
168
|
+
window_to = total_pages
|
169
|
+
elsif window_from < 1
|
170
|
+
window_to += 1 - window_from
|
171
|
+
window_from = 1
|
172
|
+
end
|
173
|
+
|
174
|
+
visible = (1..total_pages).to_a
|
175
|
+
left_gap = (2 + outer_window)...window_from
|
176
|
+
right_gap = (window_to + 1)...(total_pages - outer_window)
|
177
|
+
visible -= left_gap.to_a if left_gap.last - left_gap.first > 1
|
178
|
+
visible -= right_gap.to_a if right_gap.last - right_gap.first > 1
|
179
|
+
|
180
|
+
visible
|
181
|
+
end
|
182
|
+
|
183
|
+
def page_link_or_span(page, span_class = 'current', text = nil)
|
184
|
+
text ||= page.to_s
|
185
|
+
if page and page != current_page
|
186
|
+
@template.link_to text, url_options(page)
|
187
|
+
else
|
188
|
+
@template.content_tag :span, text, :class => span_class
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def url_options(page)
|
193
|
+
options = { param_name => page }
|
194
|
+
# page links should preserve GET parameters
|
195
|
+
options = params.merge(options) if @template.request.get?
|
196
|
+
options.rec_merge!(@options[:params]) if @options[:params]
|
197
|
+
return options
|
198
|
+
end
|
199
|
+
|
200
|
+
private
|
201
|
+
|
202
|
+
def current_page
|
203
|
+
@collection.current_page
|
204
|
+
end
|
205
|
+
|
206
|
+
def total_pages
|
207
|
+
@collection.page_count
|
208
|
+
end
|
209
|
+
|
210
|
+
def param_name
|
211
|
+
@param_name ||= @options[:param_name].to_sym
|
212
|
+
end
|
213
|
+
|
214
|
+
def params
|
215
|
+
@params ||= @template.params.to_hash.symbolize_keys
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
# = You *will* paginate!
|
4
|
+
#
|
5
|
+
# First read about WillPaginate::Finder::ClassMethods, then see
|
6
|
+
# WillPaginate::ViewHelpers. The magical array you're handling in-between is
|
7
|
+
# WillPaginate::Collection.
|
8
|
+
#
|
9
|
+
# Happy paginating!
|
10
|
+
module WillPaginate
|
11
|
+
class << self
|
12
|
+
# shortcut for <tt>enable_actionpack; enable_activerecord</tt>
|
13
|
+
def enable
|
14
|
+
enable_actionpack
|
15
|
+
enable_activerecord
|
16
|
+
end
|
17
|
+
|
18
|
+
# mixes in WillPaginate::ViewHelpers in ActionView::Base
|
19
|
+
def enable_actionpack
|
20
|
+
return if ActionView::Base.instance_methods.include? 'will_paginate'
|
21
|
+
require 'will_paginate/view_helpers'
|
22
|
+
ActionView::Base.class_eval { include ViewHelpers }
|
23
|
+
end
|
24
|
+
|
25
|
+
# mixes in WillPaginate::Finder in ActiveRecord::Base and classes that deal
|
26
|
+
# with associations
|
27
|
+
def enable_activerecord
|
28
|
+
return if ActiveRecord::Base.respond_to? :paginate
|
29
|
+
require 'will_paginate/finder'
|
30
|
+
ActiveRecord::Base.class_eval { include Finder }
|
31
|
+
|
32
|
+
associations = ActiveRecord::Associations
|
33
|
+
collection = associations::AssociationCollection
|
34
|
+
|
35
|
+
# to support paginating finders on associations, we have to mix in the
|
36
|
+
# method_missing magic from WillPaginate::Finder::ClassMethods to AssociationProxy
|
37
|
+
# subclasses, but in a different way for Rails 1.2.x and 2.0
|
38
|
+
(collection.instance_methods.include?(:create!) ?
|
39
|
+
collection : collection.subclasses.map(&:constantize)
|
40
|
+
).push(associations::HasManyThroughAssociation).each do |klass|
|
41
|
+
klass.class_eval do
|
42
|
+
include Finder::ClassMethods
|
43
|
+
alias_method_chain :method_missing, :paginate
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module Deprecation #:nodoc:
|
50
|
+
extend ActiveSupport::Deprecation
|
51
|
+
|
52
|
+
def self.warn(message, callstack = caller)
|
53
|
+
message = 'WillPaginate: ' + message.strip.gsub(/ {3,}/, ' ')
|
54
|
+
behavior.call(message, callstack) if behavior && !silenced?
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.silenced?
|
58
|
+
ActiveSupport::Deprecation.silenced?
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
WillPaginate.enable if defined? ActiveRecord and defined? ActionView
|