public this repo is viewable by everyone
Description: Most awesome pagination solution for Rails
Homepage: http://github.com/mislav/will_paginate/wikis
Clone URL: git://github.com/mislav/will_paginate.git
CHANGED LinkRenderer to receive collection, options and reference to view 
template NOT in constructor, but with the #prepare method.
This is a step towards supporting passing of LinkRenderer (or subclass) 
instances that may be preconfigured in some way.
mislav (author)
16 days ago
commit  2e45f01bd13fae007412896335d266983e11ec03
tree    a5d917a1ff32f6f5e558bf0480d2f696f14e4e9b
parent  5586e8ea4b99394baa4a60539e766aa9d2a8a8b6
...
49
50
51
52
 
 
53
54
55
56
57
 
 
58
59
60
...
91
92
93
94
 
 
95
96
97
98
 
 
 
 
99
100
101
102
103
104
 
105
106
 
107
108
109
...
179
180
181
182
183
 
 
 
 
 
 
 
 
 
184
185
186
187
188
 
189
190
191
 
 
 
192
193
194
...
197
198
199
200
201
 
 
202
203
204
...
218
219
220
221
222
223
224
225
226
227
228
229
230
...
267
268
269
270
271
272
273
 
 
274
275
 
276
277
278
...
284
285
286
287
288
289
290
...
49
50
51
 
52
53
54
55
56
 
 
57
58
59
60
61
...
92
93
94
 
95
96
97
 
 
 
98
99
100
101
102
 
 
 
 
 
103
104
105
106
107
108
109
...
179
180
181
 
 
182
183
184
185
186
187
188
189
190
191
192
193
194
 
195
196
197
198
199
200
201
202
203
204
...
207
208
209
 
 
210
211
212
213
214
...
228
229
230
 
 
 
 
 
 
 
231
232
233
...
270
271
272
 
273
274
 
275
276
277
 
278
279
280
281
...
287
288
289
 
290
291
292
0
@@ -49,12 +49,13 @@ module WillPaginate
0
     # * <tt>:param_name</tt> -- parameter name for page number in URLs (default: <tt>:page</tt>)
0
     # * <tt>:params</tt> -- additional parameters when generating pagination links
0
     # (eg. <tt>:controller => "foo", :action => nil</tt>)
0
- # * <tt>:renderer</tt> -- class name of the link renderer (default: WillPaginate::LinkRenderer)
0
+ # * <tt>:renderer</tt> -- class name, class or instance of a link renderer (default:
0
+ # <tt>WillPaginate::LinkRenderer</tt>)
0
     # * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
0
     # * <tt>:container</tt> -- toggles rendering of the DIV container for pagination links, set to
0
     # false only when you are rendering your own pagination markup (default: true)
0
- # * <tt>:id</tt> -- HTML ID for the container (default: nil). Pass +true+ to have the ID automatically
0
- # generated from the class name of objects in collection: for example, paginating
0
+ # * <tt>:id</tt> -- HTML ID for the container (default: nil). Pass +true+ to have the ID
0
+ # automatically generated from the class name of objects in collection: for example, paginating
0
     # ArticleComment models would yield an ID of "article_comments_pagination".
0
     #
0
     # All options beside listed ones are passed as HTML attributes to the container
0
@@ -91,19 +92,18 @@ module WillPaginate
0
       return nil unless WillPaginate::ViewHelpers.total_pages_for_collection(collection) > 1
0
       
0
       options = options.symbolize_keys.reverse_merge WillPaginate::ViewHelpers.pagination_options
0
- # create the renderer instance
0
+
0
+ # get the renderer instance
0
       renderer = case options[:renderer]
0
- when String, Class
0
- renderer_class = options[:renderer].to_s.constantize
0
- renderer_class.new collection, options, self
0
+ when String
0
+ options[:renderer].to_s.constantize.new
0
+ when Class
0
+ options[:renderer].new
0
       else
0
- returning(options[:renderer]) do |r|
0
- r.collection = collection
0
- r.options = options
0
- r.template = self
0
- end
0
+ options[:renderer]
0
       end
0
       # render HTML for pagination
0
+ renderer.prepare collection, options, self
0
       renderer.to_html
0
     end
0
     
0
@@ -179,16 +179,26 @@ module WillPaginate
0
   # links. It is used by +will_paginate+ helper internally.
0
   class LinkRenderer
0
 
0
- attr_accessor :collection, :options, :template
0
-
0
+ # The gap in page links is represented by:
0
+ #
0
+ # <span class="gap">&hellip;</span>
0
+ attr_accessor :gap_marker
0
+
0
+ def initialize
0
+ @gap_marker = '<span class="gap">&hellip;</span>'
0
+ end
0
+
0
     # * +collection+ is a WillPaginate::Collection instance or any other object
0
     # that conforms to that API
0
     # * +options+ are forwarded from +will_paginate+ view helper
0
     # * +template+ is the reference to the template being rendered
0
- def initialize(collection, options, template)
0
+ def prepare(collection, options, template)
0
       @collection = collection
0
       @options = options
0
       @template = template
0
+
0
+ # reset values in case we're re-using this instance
0
+ @total_pages = @param_name = @url_string = nil
0
     end
0
 
0
     # Process it! This method returns the complete HTML string which contains
0
@@ -197,8 +207,8 @@ module WillPaginate
0
     def to_html
0
       links = @options[:page_links] ? windowed_links : []
0
       # previous/next buttons
0
- links.unshift page_link_or_span(@collection.previous_page, %w(disabled prev_page), @options[:prev_label])
0
- links.push page_link_or_span(@collection.next_page, %w(disabled next_page), @options[:next_label])
0
+ links.unshift page_link_or_span(@collection.previous_page, 'disabled prev_page', @options[:prev_label])
0
+ links.push page_link_or_span(@collection.next_page, 'disabled next_page', @options[:next_label])
0
       
0
       html = links.join(@options[:separator])
0
       @options[:container] ? @template.content_tag(:div, html, html_attributes) : html
0
@@ -218,13 +228,6 @@ module WillPaginate
0
     
0
   protected
0
 
0
- # The gap in page links is represented by:
0
- #
0
- # <span class="gap">&hellip;</span>
0
- def gap_marker
0
- '<span class="gap">&hellip;</span>'
0
- end
0
-
0
     # Collects link items for visible page numbers.
0
     def windowed_links
0
       prev = nil
0
@@ -267,12 +270,12 @@ module WillPaginate
0
     
0
     def page_link_or_span(page, span_class, text = nil)
0
       text ||= page.to_s
0
- classnames = Array[*span_class]
0
       
0
       if page and page != current_page
0
- page_link page, text, :rel => rel_value(page), :class => classnames[1]
0
+ classnames = span_class && span_class.index(' ') && span_class.split(' ', 2).last
0
+ page_link page, text, :rel => rel_value(page), :class => classnames
0
       else
0
- page_span page, text, :class => classnames.join(' ')
0
+ page_span page, text, :class => span_class
0
       end
0
     end
0
 
0
@@ -284,7 +287,6 @@ module WillPaginate
0
       @template.content_tag :span, text, attributes
0
     end
0
 
0
-
0
     # Returns URL params for +page_link_or_span+, taking the current GET params
0
     # and <tt>:params</tt> option into account.
0
     def url_for(page)
...
2
3
4
5
6
7
8
9
10
11
 
 
 
12
13
14
...
16
17
18
19
20
21
22
...
60
61
62
63
64
 
 
65
66
67
68
69
70
 
 
 
 
 
 
 
 
 
 
71
72
73
...
2
3
4
 
 
 
 
 
 
 
5
6
7
8
9
10
...
12
13
14
 
15
16
17
...
55
56
57
 
 
58
59
60
61
62
63
 
 
64
65
66
67
68
69
70
71
72
73
74
75
76
0
@@ -2,13 +2,9 @@ require 'helper'
0
 require 'lib/view_test_process'
0
 
0
 class AdditionalLinkAttributesRenderer < WillPaginate::LinkRenderer
0
- def initialize(*arguments)
0
- if arguments.size == 3
0
- super(*arguments)
0
- @additional_link_attributes = {:default => 'true'}
0
- else
0
- @additional_link_attributes = arguments.extract_options!
0
- end
0
+ def initialize(link_attributes = nil)
0
+ super()
0
+ @additional_link_attributes = link_attributes || { :default => 'true' }
0
   end
0
 
0
   def page_link(page, text, attributes = {})
0
@@ -16,7 +12,6 @@ class AdditionalLinkAttributesRenderer < WillPaginate::LinkRenderer
0
   end
0
 end
0
 
0
-
0
 class ViewTest < WillPaginate::ViewTestCase
0
   
0
   ## basic pagination ##
0
@@ -60,14 +55,22 @@ class ViewTest < WillPaginate::ViewTestCase
0
   end
0
 
0
   def test_will_paginate_using_renderer_class
0
- paginate({},:renderer => AdditionalLinkAttributesRenderer) do
0
- assert_select 'a[default~=true]'
0
+ paginate({}, :renderer => AdditionalLinkAttributesRenderer) do
0
+ assert_select 'a[default=true]', 3
0
     end
0
   end
0
 
0
   def test_will_paginate_using_renderer_instance
0
- paginate({},:renderer => AdditionalLinkAttributesRenderer.new(:title => 'rendered')) do
0
- assert_select 'a[title=rendered]'
0
+ renderer = WillPaginate::LinkRenderer.new
0
+ renderer.gap_marker = '<span class="my-gap">~~</span>'
0
+
0
+ paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
0
+ assert_select 'span.my-gap', '~~'
0
+ end
0
+
0
+ renderer = AdditionalLinkAttributesRenderer.new(:title => 'rendered')
0
+ paginate({}, :renderer => renderer) do
0
+ assert_select 'a[title=rendered]', 3
0
     end
0
   end
0
 

Comments

  • chriseppstein 16 days ago

    very nice. Thank you for taking my change to the next level.