public
Fork of mislav/will_paginate
Description: Most awesome pagination solution for Rails
Homepage: http://github.com/mislav/will_paginate/wikis
Clone URL: git://github.com/bronson/will_paginate.git
Cleanup options handling; the original hash should stay intact.


git-svn-id: svn://errtheblog.com/svn/plugins/will_paginate@450 
1eaa51fe-a21a-0410-9c2e-ae7a00a434c4
mislav (author)
Sat Feb 23 14:40:38 -0800 2008
commit  92f227568a091d47a4dd13de32401b22daf6934b
tree    ea32b66baf824a7d6bc1aaa754672ccee4c636f8
parent  2fda1c7527e28cda8ca4c4a1a7398d688e612fc7
...
55
56
57
58
59
60
 
 
61
62
63
...
67
68
69
70
 
 
71
72
 
 
73
74
75
76
 
77
78
79
...
91
92
93
94
 
95
96
97
98
99
 
 
100
101
102
...
131
132
133
134
 
135
136
137
...
139
140
141
142
 
143
144
145
...
149
150
151
152
 
153
154
155
...
173
174
175
176
177
178
 
 
 
179
180
181
182
 
183
184
185
186
187
 
 
 
188
189
190
...
55
56
57
 
 
 
58
59
60
61
62
...
66
67
68
 
69
70
71
 
72
73
74
75
76
 
77
78
79
80
...
92
93
94
 
95
96
 
 
97
 
98
99
100
101
102
...
131
132
133
 
134
135
136
137
...
139
140
141
 
142
143
144
145
...
149
150
151
 
152
153
154
155
...
173
174
175
 
 
 
176
177
178
179
180
181
 
182
183
184
 
 
 
185
186
187
188
189
190
0
@@ -55,9 +55,8 @@ module WillPaginate
0
     module ClassMethods
0
       def paginate(*args, &block)
0
         options = args.pop
0
- options = options.dup unless !options or options.key? :finder
0
- page, per_page, total_entries = wp_parse_options!(options)
0
- finder = options.delete(:finder) || 'find'
0
+ page, per_page, total_entries = wp_parse_options(options)
0
+ finder = (options[:finder] || 'find').to_s
0
 
0
         if finder == 'find'
0
           # an array of IDs may have been given:
0
@@ -67,13 +66,15 @@ module WillPaginate
0
         end
0
 
0
         WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
0
- args << options.except(:count).merge(:offset => pager.offset, :limit => pager.per_page)
0
+ count_options = options.except :page, :per_page, :total_entries, :finder
0
+ find_options = count_options.except(:count).update(:offset => pager.offset, :limit => pager.per_page)
0
           
0
- @options_from_last_find = nil
0
+ args << find_options
0
+ # @options_from_last_find = nil
0
           pager.replace send(finder, *args, &block)
0
           
0
           # magic counting for user convenience:
0
- pager.total_entries = wp_count!(options, args, finder) unless pager.total_entries
0
+ pager.total_entries = wp_count(count_options, args, finder) unless pager.total_entries
0
         end
0
       end
0
       
0
@@ -91,12 +92,11 @@ module WillPaginate
0
       # application.
0
       #
0
       def paginate_by_sql(sql, options)
0
- WillPaginate::Collection.create(*wp_parse_options!(options)) do |pager|
0
+ WillPaginate::Collection.create(*wp_parse_options(options)) do |pager|
0
           query = sanitize_sql(sql)
0
- options.update :offset => pager.offset, :limit => pager.per_page
0
-
0
           original_query = query.dup
0
- add_limit! query, options
0
+ # add limit, offset
0
+ add_limit! query, :offset => pager.offset, :limit => pager.per_page
0
           # perfom the find
0
           pager.replace find_by_sql(query)
0
           
0
@@ -131,7 +131,7 @@ module WillPaginate
0
         finder.sub!('find', 'find_all') if finder.index('find_by_') == 0
0
         
0
         options = args.pop
0
- raise ArgumentError, 'hash parameters expected' unless options.respond_to? :symbolize_keys!
0
+ raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
0
         options = options.dup
0
         options[:finder] = finder
0
         args << options
0
@@ -139,7 +139,7 @@ module WillPaginate
0
         paginate(*args, &block)
0
       end
0
 
0
- def wp_count!(options, args, finder)
0
+ def wp_count(options, args, finder)
0
         excludees = [:count, :order, :limit, :offset, :readonly]
0
         unless options[:select] and options[:select] =~ /^\s*DISTINCT\b/i
0
           excludees << :select # only exclude the select param if it doesn't begin with DISTINCT
0
@@ -149,7 +149,7 @@ module WillPaginate
0
 
0
         # merge the hash found in :count
0
         # this allows you to specify :select, :order, or anything else just for the count query
0
- count_options.update(options.delete(:count) || {}) if options.key? :count
0
+ count_options.update options[:count] if options[:count]
0
 
0
         # we may have to scope ...
0
         counter = Proc.new { count(count_options) }
0
@@ -173,18 +173,18 @@ module WillPaginate
0
         count.respond_to?(:length) ? count.length : count
0
       end
0
 
0
- def wp_parse_options!(options)
0
- raise ArgumentError, 'hash parameters expected' unless options.respond_to? :symbolize_keys!
0
- options.symbolize_keys!
0
+ def wp_parse_options(options)
0
+ raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
0
+ options = options.symbolize_keys
0
         raise ArgumentError, ':page parameter required' unless options.key? :page
0
         
0
         if options[:count] and options[:total_entries]
0
- raise ArgumentError, ':count and :total_entries are mutually exclusive parameters'
0
+ raise ArgumentError, ':count and :total_entries are mutually exclusive'
0
         end
0
 
0
- page = options.delete(:page) || 1
0
- per_page = options.delete(:per_page) || self.per_page
0
- total = options.delete(:total_entries)
0
+ page = options[:page] || 1
0
+ per_page = options[:per_page] || self.per_page
0
+ total = options[:total_entries]
0
         [page, per_page, total]
0
       end
0
 
...
307
308
309
 
 
 
 
 
 
 
 
 
 
 
310
311
...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
0
@@ -307,5 +307,16 @@ class FinderTest < ActiveRecordTestCase
0
       
0
       Developer.paginate_by_id(ids, :per_page => 3, :page => 2, :order => 'id')
0
     end
0
+
0
+ def test_paginating_finder_doesnt_mangle_options
0
+ Developer.expects(:find).returns([])
0
+ Developer.expects(:count).returns(0)
0
+ options = { :page => 1 }
0
+ options.expects(:delete).never
0
+ options_before = options.dup
0
+
0
+ Developer.paginate(options)
0
+ assert_equal options, options_before
0
+ end
0
   end
0
 end

Comments

    No one has commented yet.