jkaneacumen-will_paginate 2.3.12

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.
@@ -0,0 +1,37 @@
1
+ ActiveRecord::Associations::AssociationProxy.class_eval do
2
+ protected
3
+ def with_scope(*args)
4
+ @reflection.klass.send(:with_scope, *args) { |*a| yield(*a) if block_given? }
5
+ end
6
+ end
7
+
8
+ [ ActiveRecord::Associations::AssociationCollection,
9
+ ActiveRecord::Associations::HasManyThroughAssociation ].each do |klass|
10
+ klass.class_eval do
11
+ protected
12
+ alias :method_missing_without_scopes :method_missing_without_paginate
13
+ def method_missing_without_paginate(method, *args)
14
+ if @reflection.klass.scopes.include?(method)
15
+ @reflection.klass.scopes[method].call(self, *args) { |*a| yield(*a) if block_given? }
16
+ else
17
+ method_missing_without_scopes(method, *args) { |*a| yield(*a) if block_given? }
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ # Rails 1.2.6
24
+ ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
25
+ protected
26
+ def method_missing(method, *args)
27
+ if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
28
+ super
29
+ elsif @reflection.klass.scopes.include?(method)
30
+ @reflection.klass.scopes[method].call(self, *args)
31
+ else
32
+ @reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
33
+ @reflection.klass.send(method, *args) { |*a| yield(*a) if block_given? }
34
+ end
35
+ end
36
+ end
37
+ end if ActiveRecord::Base.respond_to? :find_first
@@ -0,0 +1,9 @@
1
+ module WillPaginate
2
+ module VERSION
3
+ MAJOR = 2
4
+ MINOR = 3
5
+ TINY = 12
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,410 @@
1
+ require 'will_paginate/core_ext'
2
+
3
+ module WillPaginate
4
+ # = Will Paginate view helpers
5
+ #
6
+ # The main view helper, #will_paginate, renders
7
+ # pagination links for the given collection. The helper itself is lightweight
8
+ # and serves only as a wrapper around LinkRenderer 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
+ # <tt>WillPaginate::ViewHelpers.pagination_options</tt> hash. You can write to this hash to
15
+ # override default options on the global level:
16
+ #
17
+ # WillPaginate::ViewHelpers.pagination_options[:previous_label] = 'Previous page'
18
+ #
19
+ # By putting this into "config/initializers/will_paginate.rb" (or simply environment.rb in
20
+ # older versions of Rails) you can easily translate link texts to previous
21
+ # and next pages, as well as override some other defaults to your liking.
22
+ module ViewHelpers
23
+ # default options that can be overridden on the global level
24
+ @@pagination_options = {
25
+ :class => 'pagination',
26
+ :previous_label => '&laquo; Previous',
27
+ :next_label => 'Next &raquo;',
28
+ :inner_window => 4, # links around the current page
29
+ :outer_window => 1, # links around beginning and end
30
+ :separator => ' ', # single space is friendly to spiders and non-graphic browsers
31
+ :param_name => :page,
32
+ :params => nil,
33
+ :renderer => 'WillPaginate::LinkRenderer',
34
+ :page_links => true,
35
+ :container => true
36
+ }
37
+ mattr_reader :pagination_options
38
+
39
+ # Renders Digg/Flickr-style pagination for a WillPaginate::Collection
40
+ # object. Nil is returned if there is only one page in total; no point in
41
+ # rendering the pagination in that case...
42
+ #
43
+ # ==== Options
44
+ # Display options:
45
+ # * <tt>:previous_label</tt> -- default: "« Previous" (this parameter is called <tt>:prev_label</tt> in versions <b>2.3.2</b> and older!)
46
+ # * <tt>:next_label</tt> -- default: "Next »"
47
+ # * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
48
+ # * <tt>:inner_window</tt> -- how many links are shown around the current page (default: 4)
49
+ # * <tt>:outer_window</tt> -- how many links are around the first and the last page (default: 1)
50
+ # * <tt>:separator</tt> -- string separator for page HTML elements (default: single space)
51
+ #
52
+ # HTML options:
53
+ # * <tt>:class</tt> -- CSS class name for the generated DIV (default: "pagination")
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
57
+ # automatically 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
+ # Advanced options:
61
+ # * <tt>:param_name</tt> -- parameter name for page number in URLs (default: <tt>:page</tt>)
62
+ # * <tt>:params</tt> -- additional parameters when generating pagination links
63
+ # (eg. <tt>:controller => "foo", :action => nil</tt>)
64
+ # * <tt>:omit_page_one</tt> -- omits the page=1 parameter from the URL on page one (default: <tt>false</tt>)
65
+ # * <tt>:renderer</tt> -- class name, class or instance of a link renderer (default:
66
+ # <tt>WillPaginate::LinkRenderer</tt>)
67
+ #
68
+ # All options not recognized by will_paginate will become HTML attributes on the container
69
+ # element for pagination links (the DIV). For example:
70
+ #
71
+ # <%= will_paginate @posts, :style => 'font-size: small' %>
72
+ #
73
+ # ... will result in:
74
+ #
75
+ # <div class="pagination" style="font-size: small"> ... </div>
76
+ #
77
+ # ==== Using the helper without arguments
78
+ # If the helper is called without passing in the collection object, it will
79
+ # try to read from the instance variable inferred by the controller name.
80
+ # For example, calling +will_paginate+ while the current controller is
81
+ # PostsController will result in trying to read from the <tt>@posts</tt>
82
+ # variable. Example:
83
+ #
84
+ # <%= will_paginate :id => true %>
85
+ #
86
+ # ... will result in <tt>@post</tt> collection getting paginated:
87
+ #
88
+ # <div class="pagination" id="posts_pagination"> ... </div>
89
+ #
90
+ def will_paginate(collection = nil, options = {})
91
+ options, collection = collection, nil if collection.is_a? Hash
92
+ unless collection or !controller
93
+ collection_name = "@#{controller.controller_name}"
94
+ collection = instance_variable_get(collection_name)
95
+ raise ArgumentError, "The #{collection_name} variable appears to be empty. Did you " +
96
+ "forget to pass the collection object for will_paginate?" unless collection
97
+ end
98
+ # early exit if there is nothing to render
99
+ return nil unless WillPaginate::ViewHelpers.total_pages_for_collection(collection) > 1
100
+
101
+ options = options.symbolize_keys.reverse_merge WillPaginate::ViewHelpers.pagination_options
102
+ if options[:prev_label]
103
+ WillPaginate::Deprecation::warn(":prev_label view parameter is now :previous_label; the old name has been deprecated", caller)
104
+ options[:previous_label] = options.delete(:prev_label)
105
+ end
106
+
107
+ # get the renderer instance
108
+ renderer = case options[:renderer]
109
+ when String
110
+ options[:renderer].to_s.constantize.new
111
+ when Class
112
+ options[:renderer].new
113
+ else
114
+ options[:renderer]
115
+ end
116
+ # render HTML for pagination
117
+ renderer.prepare collection, options, self
118
+ renderer.to_html
119
+ end
120
+
121
+ # Wrapper for rendering pagination links at both top and bottom of a block
122
+ # of content.
123
+ #
124
+ # <% paginated_section @posts do %>
125
+ # <ol id="posts">
126
+ # <% for post in @posts %>
127
+ # <li> ... </li>
128
+ # <% end %>
129
+ # </ol>
130
+ # <% end %>
131
+ #
132
+ # will result in:
133
+ #
134
+ # <div class="pagination"> ... </div>
135
+ # <ol id="posts">
136
+ # ...
137
+ # </ol>
138
+ # <div class="pagination"> ... </div>
139
+ #
140
+ # Arguments are passed to a <tt>will_paginate</tt> call, so the same options
141
+ # apply. Don't use the <tt>:id</tt> option; otherwise you'll finish with two
142
+ # blocks of pagination links sharing the same ID (which is invalid HTML).
143
+ def paginated_section(*args, &block)
144
+ pagination = will_paginate(*args).to_s
145
+
146
+ unless ActionView::Base.respond_to? :erb_variable
147
+ concat pagination
148
+ yield
149
+ concat pagination
150
+ else
151
+ content = pagination + capture(&block) + pagination
152
+ concat(content, block.binding)
153
+ end
154
+ end
155
+
156
+ # Renders a helpful message with numbers of displayed vs. total entries.
157
+ # You can use this as a blueprint for your own, similar helpers.
158
+ #
159
+ # <%= page_entries_info @posts %>
160
+ # #-> Displaying posts 6 - 10 of 26 in total
161
+ #
162
+ # By default, the message will use the humanized class name of objects
163
+ # in collection: for instance, "project types" for ProjectType models.
164
+ # Override this with the <tt>:entry_name</tt> parameter:
165
+ #
166
+ # <%= page_entries_info @posts, :entry_name => 'item' %>
167
+ # #-> Displaying items 6 - 10 of 26 in total
168
+ def page_entries_info(collection, options = {})
169
+ entry_name = options[:entry_name] ||
170
+ (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
171
+
172
+ if collection.total_pages < 2
173
+ case collection.size
174
+ when 0; "No #{entry_name.pluralize} found"
175
+ when 1; "Displaying <b>1</b> #{entry_name}"
176
+ else; "Displaying <b>all #{collection.size}</b> #{entry_name.pluralize}"
177
+ end
178
+ else
179
+ %{Displaying #{entry_name.pluralize} <b>%d&nbsp;-&nbsp;%d</b> of <b>%d</b> in total} % [
180
+ collection.offset + 1,
181
+ collection.offset + collection.length,
182
+ collection.total_entries
183
+ ]
184
+ end
185
+ end
186
+
187
+ if respond_to? :safe_helper
188
+ safe_helper :will_paginate, :paginated_section, :page_entries_info
189
+ end
190
+
191
+ def self.total_pages_for_collection(collection) #:nodoc:
192
+ if collection.respond_to?('page_count') and !collection.respond_to?('total_pages')
193
+ WillPaginate::Deprecation.warn %{
194
+ You are using a paginated collection of class #{collection.class.name}
195
+ which conforms to the old API of WillPaginate::Collection by using
196
+ `page_count`, while the current method name is `total_pages`. Please
197
+ upgrade yours or 3rd-party code that provides the paginated collection}, caller
198
+ class << collection
199
+ def total_pages; page_count; end
200
+ end
201
+ end
202
+ collection.total_pages
203
+ end
204
+ end
205
+
206
+ # This class does the heavy lifting of actually building the pagination
207
+ # links. It is used by the <tt>will_paginate</tt> helper internally.
208
+ class LinkRenderer
209
+
210
+ # The gap in page links is represented by:
211
+ #
212
+ # <span class="gap">&hellip;</span>
213
+ attr_accessor :gap_marker
214
+
215
+ def initialize
216
+ @gap_marker = '<span class="gap">&hellip;</span>'
217
+ end
218
+
219
+ # * +collection+ is a WillPaginate::Collection instance or any other object
220
+ # that conforms to that API
221
+ # * +options+ are forwarded from +will_paginate+ view helper
222
+ # * +template+ is the reference to the template being rendered
223
+ def prepare(collection, options, template)
224
+ @collection = collection
225
+ @options = options
226
+ @template = template
227
+
228
+ # reset values in case we're re-using this instance
229
+ @total_pages = @param_name = @url_string = nil
230
+ end
231
+
232
+ # Process it! This method returns the complete HTML string which contains
233
+ # pagination links. Feel free to subclass LinkRenderer and change this
234
+ # method as you see fit.
235
+ def to_html
236
+ links = @options[:page_links] ? windowed_links : []
237
+ # previous/next buttons
238
+ links.unshift page_link_or_span(@collection.previous_page, 'disabled prev_page', @options[:previous_label])
239
+ links.push page_link_or_span(@collection.next_page, 'disabled next_page', @options[:next_label])
240
+
241
+ html = links.join(@options[:separator])
242
+ @options[:container] ? @template.content_tag(:div, html, html_attributes) : html
243
+ end
244
+
245
+ # Returns the subset of +options+ this instance was initialized with that
246
+ # represent HTML attributes for the container element of pagination links.
247
+ def html_attributes
248
+ return @html_attributes if @html_attributes
249
+ @html_attributes = @options.except *(WillPaginate::ViewHelpers.pagination_options.keys - [:class])
250
+ # pagination of Post models will have the ID of "posts_pagination"
251
+ if @options[:container] and @options[:id] === true
252
+ @html_attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination'
253
+ end
254
+ @html_attributes
255
+ end
256
+
257
+ protected
258
+
259
+ # Collects link items for visible page numbers.
260
+ def windowed_links
261
+ prev = nil
262
+
263
+ visible_page_numbers.inject [] do |links, n|
264
+ # detect gaps:
265
+ links << gap_marker if prev and n > prev + 1
266
+ links << page_link_or_span(n, 'current')
267
+ prev = n
268
+ links
269
+ end
270
+ end
271
+
272
+ # Calculates visible page numbers using the <tt>:inner_window</tt> and
273
+ # <tt>:outer_window</tt> options.
274
+ def visible_page_numbers
275
+ inner_window, outer_window = @options[:inner_window].to_i, @options[:outer_window].to_i
276
+ window_from = current_page - inner_window
277
+ window_to = current_page + inner_window
278
+
279
+ # adjust lower or upper limit if other is out of bounds
280
+ if window_to > total_pages
281
+ window_from -= window_to - total_pages
282
+ window_to = total_pages
283
+ end
284
+ if window_from < 1
285
+ window_to += 1 - window_from
286
+ window_from = 1
287
+ window_to = total_pages if window_to > total_pages
288
+ end
289
+
290
+ visible = (1..total_pages).to_a
291
+ left_gap = (2 + outer_window)...window_from
292
+ right_gap = (window_to + 1)...(total_pages - outer_window)
293
+ visible -= left_gap.to_a if left_gap.last - left_gap.first > 1
294
+ visible -= right_gap.to_a if right_gap.last - right_gap.first > 1
295
+
296
+ visible
297
+ end
298
+
299
+ def page_link_or_span(page, span_class, text = nil)
300
+ text ||= page.to_s
301
+
302
+ if page and page != current_page
303
+ classnames = span_class && span_class.index(' ') && span_class.split(' ', 2).last
304
+ page_link page, text, :rel => rel_value(page), :class => classnames
305
+ else
306
+ page_span page, text, :class => span_class
307
+ end
308
+ end
309
+
310
+ def page_link(page, text, attributes = {})
311
+ @template.link_to text, url_for(page), attributes
312
+ end
313
+
314
+ def page_span(page, text, attributes = {})
315
+ @template.content_tag :span, text, attributes
316
+ end
317
+
318
+ # Returns URL params for +page_link_or_span+, taking the current GET params
319
+ # and <tt>:params</tt> option into account.
320
+ def url_for(page)
321
+ page_one = page == 1
322
+ unless @url_string and !page_one
323
+ @url_params = {}
324
+ # page links should preserve GET parameters
325
+ stringified_merge @url_params, @template.params if @template.request.get?
326
+ stringified_merge @url_params, @options[:params] if @options[:params]
327
+
328
+ if complex = param_name.index(/[^\w-]/)
329
+ page_param = parse_query_parameters("#{param_name}=#{page}")
330
+
331
+ stringified_merge @url_params, page_param
332
+ else
333
+ @url_params[param_name] = page_one ? 1 : 2
334
+ @url_params[param_name] = nil if @options[:omit_page_one] and page_one
335
+ end
336
+
337
+ url = @template.url_for(@url_params)
338
+ return url if page_one
339
+
340
+ if complex
341
+ @url_string = url.sub(%r!((?:\?|&amp;)#{CGI.escape param_name}=)#{page}!, "\\1\0")
342
+ return url
343
+ else
344
+ @url_string = url
345
+ @url_params[param_name] = 3
346
+ @template.url_for(@url_params).split(//).each_with_index do |char, i|
347
+ if char == '3' and url[i, 1] == '2'
348
+ @url_string[i] = "\0"
349
+ break
350
+ end
351
+ end
352
+ end
353
+ end
354
+ # finally!
355
+ @url_string.sub "\0", page.to_s
356
+ end
357
+
358
+ private
359
+
360
+ def rel_value(page)
361
+ case page
362
+ when @collection.previous_page; 'prev' + (page == 1 ? ' start' : '')
363
+ when @collection.next_page; 'next'
364
+ when 1; 'start'
365
+ end
366
+ end
367
+
368
+ def current_page
369
+ @collection.current_page
370
+ end
371
+
372
+ def total_pages
373
+ @total_pages ||= WillPaginate::ViewHelpers.total_pages_for_collection(@collection)
374
+ end
375
+
376
+ def param_name
377
+ @param_name ||= @options[:param_name].to_s
378
+ end
379
+
380
+ # Recursively merge into target hash by using stringified keys from the other one
381
+ def stringified_merge(target, other)
382
+ other.each do |key, value|
383
+ key = key.to_s # this line is what it's all about!
384
+ existing = target[key]
385
+
386
+ if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?)
387
+ stringified_merge(existing || (target[key] = {}), value)
388
+ else
389
+ target[key] = value
390
+ end
391
+ end
392
+ end
393
+
394
+ def parse_query_parameters(params)
395
+ if defined? Rack::Utils
396
+ # For Rails > 2.3
397
+ Rack::Utils.parse_nested_query(params)
398
+ elsif defined?(ActionController::AbstractRequest)
399
+ ActionController::AbstractRequest.parse_query_parameters(params)
400
+ elsif defined?(ActionController::UrlEncodedPairParser)
401
+ # For Rails > 2.2
402
+ ActionController::UrlEncodedPairParser.parse_query_parameters(params)
403
+ elsif defined?(CGIMethods)
404
+ CGIMethods.parse_query_parameters(params)
405
+ else
406
+ raise "unsupported ActionPack version"
407
+ end
408
+ end
409
+ end
410
+ end