stilkov-will_paginate 2.3.6 → 2.3.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/will_paginate.rb +1 -5
- data/lib/will_paginate/collection.rb +1 -1
- data/lib/will_paginate/finder.rb +11 -7
- data/lib/will_paginate/named_scope.rb +6 -6
- data/lib/will_paginate/named_scope_patch.rb +7 -7
- data/lib/will_paginate/version.rb +1 -1
- data/lib/will_paginate/view_helpers.rb +40 -10
- data/test/helper.rb +4 -1
- data/test/lib/activerecord_test_case.rb +7 -0
- data/test/lib/view_test_process.rb +7 -0
- data/test/tasks.rake +2 -2
- data/test/view_test.rb +66 -6
- metadata +2 -2
data/lib/will_paginate.rb
CHANGED
@@ -68,11 +68,7 @@ module WillPaginate
|
|
68
68
|
|
69
69
|
def self.warn(message, callstack = caller)
|
70
70
|
message = 'WillPaginate: ' + message.strip.gsub(/\s+/, ' ')
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.silenced?
|
75
|
-
ActiveSupport::Deprecation.silenced?
|
71
|
+
ActiveSupport::Deprecation.warn(message, callstack)
|
76
72
|
end
|
77
73
|
end
|
78
74
|
end
|
@@ -82,7 +82,7 @@ module WillPaginate
|
|
82
82
|
#
|
83
83
|
# The Array#paginate API has since then changed, but this still serves as a
|
84
84
|
# fine example of WillPaginate::Collection usage.
|
85
|
-
def self.create(page, per_page, total = nil
|
85
|
+
def self.create(page, per_page, total = nil)
|
86
86
|
pager = new(page, per_page, total)
|
87
87
|
yield pager
|
88
88
|
pager
|
data/lib/will_paginate/finder.rb
CHANGED
@@ -61,7 +61,7 @@ module WillPaginate
|
|
61
61
|
#
|
62
62
|
# All other options (+conditions+, +order+, ...) are forwarded to +find+
|
63
63
|
# and +count+ calls.
|
64
|
-
def paginate(*args
|
64
|
+
def paginate(*args)
|
65
65
|
options = args.pop
|
66
66
|
page, per_page, total_entries = wp_parse_options(options)
|
67
67
|
finder = (options[:finder] || 'find').to_s
|
@@ -79,7 +79,7 @@ module WillPaginate
|
|
79
79
|
|
80
80
|
args << find_options
|
81
81
|
# @options_from_last_find = nil
|
82
|
-
pager.replace
|
82
|
+
pager.replace(send(finder, *args) { |*a| yield(*a) if block_given? })
|
83
83
|
|
84
84
|
# magic counting for user convenience:
|
85
85
|
pager.total_entries = wp_count(count_options, args, finder) unless pager.total_entries
|
@@ -96,7 +96,7 @@ module WillPaginate
|
|
96
96
|
#
|
97
97
|
# See {Faking Cursors in ActiveRecord}[http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord]
|
98
98
|
# where Jamis Buck describes this and a more efficient way for MySQL.
|
99
|
-
def paginated_each(options = {}
|
99
|
+
def paginated_each(options = {})
|
100
100
|
options = { :order => 'id', :page => 1 }.merge options
|
101
101
|
options[:page] = options[:page].to_i
|
102
102
|
options[:total_entries] = 0 # skip the individual count queries
|
@@ -106,7 +106,7 @@ module WillPaginate
|
|
106
106
|
collection = paginate(options)
|
107
107
|
with_exclusive_scope(:find => {}) do
|
108
108
|
# using exclusive scope so that the block is yielded in scope-free context
|
109
|
-
total += collection.each
|
109
|
+
total += collection.each { |item| yield item }.size
|
110
110
|
end
|
111
111
|
options[:page] += 1
|
112
112
|
end until collection.size < collection.per_page
|
@@ -161,10 +161,14 @@ module WillPaginate
|
|
161
161
|
|
162
162
|
protected
|
163
163
|
|
164
|
-
def method_missing_with_paginate(method, *args
|
164
|
+
def method_missing_with_paginate(method, *args) #:nodoc:
|
165
165
|
# did somebody tried to paginate? if not, let them be
|
166
166
|
unless method.to_s.index('paginate') == 0
|
167
|
-
|
167
|
+
if block_given?
|
168
|
+
return method_missing_without_paginate(method, *args) { |*a| yield(*a) }
|
169
|
+
else
|
170
|
+
return method_missing_without_paginate(method, *args)
|
171
|
+
end
|
168
172
|
end
|
169
173
|
|
170
174
|
# paginate finders are really just find_* with limit and offset
|
@@ -177,7 +181,7 @@ module WillPaginate
|
|
177
181
|
options[:finder] = finder
|
178
182
|
args << options
|
179
183
|
|
180
|
-
paginate(*args
|
184
|
+
paginate(*args) { |*a| yield(*a) if block_given? }
|
181
185
|
end
|
182
186
|
|
183
187
|
# Does the not-so-trivial job of finding out the total number of entries
|
@@ -83,7 +83,7 @@ module WillPaginate
|
|
83
83
|
#
|
84
84
|
# expected_options = { :conditions => { :colored => 'red' } }
|
85
85
|
# assert_equal expected_options, Shirt.colored('red').proxy_options
|
86
|
-
def named_scope(name, options = {}
|
86
|
+
def named_scope(name, options = {})
|
87
87
|
name = name.to_sym
|
88
88
|
scopes[name] = lambda do |parent_scope, *args|
|
89
89
|
Scope.new(parent_scope, case options
|
@@ -91,7 +91,7 @@ module WillPaginate
|
|
91
91
|
options
|
92
92
|
when Proc
|
93
93
|
options.call(*args)
|
94
|
-
end
|
94
|
+
end) { |*a| yield(*a) if block_given? }
|
95
95
|
end
|
96
96
|
(class << self; self end).instance_eval do
|
97
97
|
define_method name do |*args|
|
@@ -112,9 +112,9 @@ module WillPaginate
|
|
112
112
|
|
113
113
|
delegate :scopes, :with_scope, :to => :proxy_scope
|
114
114
|
|
115
|
-
def initialize(proxy_scope, options
|
115
|
+
def initialize(proxy_scope, options)
|
116
116
|
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
|
117
|
-
extend Module.new(
|
117
|
+
extend Module.new { |*args| yield(*args) } if block_given?
|
118
118
|
@proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
|
119
119
|
end
|
120
120
|
|
@@ -152,12 +152,12 @@ module WillPaginate
|
|
152
152
|
end
|
153
153
|
|
154
154
|
private
|
155
|
-
def method_missing(method, *args
|
155
|
+
def method_missing(method, *args)
|
156
156
|
if scopes.include?(method)
|
157
157
|
scopes[method].call(self, *args)
|
158
158
|
else
|
159
159
|
with_scope :find => proxy_options do
|
160
|
-
proxy_scope.send(method, *args
|
160
|
+
proxy_scope.send(method, *args) { |*a| yield(*a) if block_given? }
|
161
161
|
end
|
162
162
|
end
|
163
163
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
ActiveRecord::Associations::AssociationProxy.class_eval do
|
2
2
|
protected
|
3
|
-
def with_scope(*args
|
4
|
-
@reflection.klass.send
|
3
|
+
def with_scope(*args)
|
4
|
+
@reflection.klass.send(:with_scope, *args) { |*a| yield(*a) if block_given? }
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
@@ -10,11 +10,11 @@ end
|
|
10
10
|
klass.class_eval do
|
11
11
|
protected
|
12
12
|
alias :method_missing_without_scopes :method_missing_without_paginate
|
13
|
-
def method_missing_without_paginate(method, *args
|
13
|
+
def method_missing_without_paginate(method, *args)
|
14
14
|
if @reflection.klass.scopes.include?(method)
|
15
|
-
@reflection.klass.scopes[method].call(self, *args
|
15
|
+
@reflection.klass.scopes[method].call(self, *args) { |*a| yield(*a) if block_given? }
|
16
16
|
else
|
17
|
-
method_missing_without_scopes(method, *args
|
17
|
+
method_missing_without_scopes(method, *args) { |*a| yield(*a) if block_given? }
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -23,14 +23,14 @@ end
|
|
23
23
|
# Rails 1.2.6
|
24
24
|
ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
|
25
25
|
protected
|
26
|
-
def method_missing(method, *args
|
26
|
+
def method_missing(method, *args)
|
27
27
|
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
28
28
|
super
|
29
29
|
elsif @reflection.klass.scopes.include?(method)
|
30
30
|
@reflection.klass.scopes[method].call(self, *args)
|
31
31
|
else
|
32
32
|
@reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
|
33
|
-
@reflection.klass.send(method, *args
|
33
|
+
@reflection.klass.send(method, *args) { |*a| yield(*a) if block_given? }
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
@@ -30,9 +30,10 @@ module WillPaginate
|
|
30
30
|
:separator => ' ', # single space is friendly to spiders and non-graphic browsers
|
31
31
|
:param_name => :page,
|
32
32
|
:params => nil,
|
33
|
-
:renderer =>
|
33
|
+
:renderer => nil,
|
34
34
|
:page_links => true,
|
35
|
-
:container => true
|
35
|
+
:container => true,
|
36
|
+
:xml => false
|
36
37
|
}
|
37
38
|
mattr_reader :pagination_options
|
38
39
|
|
@@ -99,11 +100,12 @@ module WillPaginate
|
|
99
100
|
|
100
101
|
options = options.symbolize_keys.reverse_merge WillPaginate::ViewHelpers.pagination_options
|
101
102
|
if options[:prev_label]
|
102
|
-
WillPaginate::Deprecation::warn(":prev_label view parameter is now :previous_label; the old name has been deprecated
|
103
|
+
WillPaginate::Deprecation::warn(":prev_label view parameter is now :previous_label; the old name has been deprecated", caller)
|
103
104
|
options[:previous_label] = options.delete(:prev_label)
|
104
105
|
end
|
105
106
|
|
106
107
|
# get the renderer instance
|
108
|
+
options[:renderer] ||= options[:xml] ? 'WillPaginate::XmlLinkRenderer' : 'WillPaginate::LinkRenderer'
|
107
109
|
renderer = case options[:renderer]
|
108
110
|
when String
|
109
111
|
options[:renderer].to_s.constantize.new
|
@@ -111,7 +113,9 @@ module WillPaginate
|
|
111
113
|
options[:renderer].new
|
112
114
|
else
|
113
115
|
options[:renderer]
|
114
|
-
end
|
116
|
+
end
|
117
|
+
|
118
|
+
|
115
119
|
# render HTML for pagination
|
116
120
|
renderer.prepare collection, options, self
|
117
121
|
renderer.to_html
|
@@ -185,12 +189,11 @@ module WillPaginate
|
|
185
189
|
|
186
190
|
def self.total_pages_for_collection(collection) #:nodoc:
|
187
191
|
if collection.respond_to?('page_count') and !collection.respond_to?('total_pages')
|
188
|
-
WillPaginate::Deprecation.warn
|
192
|
+
WillPaginate::Deprecation.warn %{
|
189
193
|
You are using a paginated collection of class #{collection.class.name}
|
190
194
|
which conforms to the old API of WillPaginate::Collection by using
|
191
195
|
`page_count`, while the current method name is `total_pages`. Please
|
192
|
-
upgrade yours or 3rd-party code that provides the paginated collection
|
193
|
-
MSG
|
196
|
+
upgrade yours or 3rd-party code that provides the paginated collection}, caller
|
194
197
|
class << collection
|
195
198
|
def total_pages; page_count; end
|
196
199
|
end
|
@@ -251,7 +254,11 @@ module WillPaginate
|
|
251
254
|
end
|
252
255
|
|
253
256
|
protected
|
254
|
-
|
257
|
+
|
258
|
+
def options
|
259
|
+
@options
|
260
|
+
end
|
261
|
+
|
255
262
|
# Collects link items for visible page numbers.
|
256
263
|
def windowed_links
|
257
264
|
prev = nil
|
@@ -322,8 +329,7 @@ module WillPaginate
|
|
322
329
|
stringified_merge @url_params, @options[:params] if @options[:params]
|
323
330
|
|
324
331
|
if complex = param_name.index(/[^\w-]/)
|
325
|
-
page_param = (
|
326
|
-
parse_query_parameters("#{param_name}=#{page}")
|
332
|
+
page_param = parse_query_parameters("#{param_name}=#{page}")
|
327
333
|
|
328
334
|
stringified_merge @url_params, page_param
|
329
335
|
else
|
@@ -386,5 +392,29 @@ module WillPaginate
|
|
386
392
|
end
|
387
393
|
end
|
388
394
|
end
|
395
|
+
|
396
|
+
def parse_query_parameters(params)
|
397
|
+
if defined?(CGIMethods)
|
398
|
+
CGIMethods.parse_query_parameters(params)
|
399
|
+
elsif defined?(ActionController::AbstractRequest)
|
400
|
+
ActionController::AbstractRequest.parse_query_parameters(params)
|
401
|
+
elsif defined?(ActionController::UrlEncodedPairParser)
|
402
|
+
# For Rails > 2.2
|
403
|
+
ActionController::UrlEncodedPairParser.parse_query_parameters(params)
|
404
|
+
else
|
405
|
+
# For Rails > 2.3
|
406
|
+
Rack::Utils.parse_nested_query(params)
|
407
|
+
end
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
class XmlLinkRenderer < LinkRenderer
|
412
|
+
def to_html
|
413
|
+
buffer = options[:buffer] || ""
|
414
|
+
xml = options[:builder] || Builder::XmlMarkup.new(:target => buffer, :indent => 2, :margin => 1)
|
415
|
+
xml.link :rel => 'next', :href => url_for(@collection.next_page) unless @collection.next_page.nil?
|
416
|
+
xml.link :rel => 'prev', :href => url_for(@collection.previous_page) unless @collection.previous_page.nil?
|
417
|
+
xml.target!
|
418
|
+
end
|
389
419
|
end
|
390
420
|
end
|
data/test/helper.rb
CHANGED
@@ -29,7 +29,10 @@ end
|
|
29
29
|
|
30
30
|
# Wrap tests that use Mocha and skip if unavailable.
|
31
31
|
def uses_mocha(test_name)
|
32
|
-
|
32
|
+
unless Object.const_defined?(:Mocha)
|
33
|
+
gem 'mocha', '>= 0.9.5'
|
34
|
+
require 'mocha'
|
35
|
+
end
|
33
36
|
rescue LoadError => load_error
|
34
37
|
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
|
35
38
|
else
|
@@ -1,6 +1,13 @@
|
|
1
1
|
require 'lib/activerecord_test_connector'
|
2
2
|
|
3
3
|
class ActiveRecordTestCase < Test::Unit::TestCase
|
4
|
+
if defined?(ActiveSupport::Testing::SetupAndTeardown)
|
5
|
+
include ActiveSupport::Testing::SetupAndTeardown
|
6
|
+
end
|
7
|
+
|
8
|
+
if defined?(ActiveRecord::TestFixtures)
|
9
|
+
include ActiveRecord::TestFixtures
|
10
|
+
end
|
4
11
|
# Set our fixture path
|
5
12
|
if ActiveRecordTestConnector.able_to_connect
|
6
13
|
self.fixture_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
|
@@ -17,6 +17,13 @@ end
|
|
17
17
|
ActionController::Base.perform_caching = false
|
18
18
|
|
19
19
|
class WillPaginate::ViewTestCase < Test::Unit::TestCase
|
20
|
+
if defined?(ActionController::TestCase::Assertions)
|
21
|
+
include ActionController::TestCase::Assertions
|
22
|
+
end
|
23
|
+
if defined?(ActiveSupport::Testing::Deprecation)
|
24
|
+
include ActiveSupport::Testing::Deprecation
|
25
|
+
end
|
26
|
+
|
20
27
|
def setup
|
21
28
|
super
|
22
29
|
@controller = DummyController.new
|
data/test/tasks.rake
CHANGED
@@ -32,10 +32,10 @@ task :test_databases => %w(test_mysql test_sqlite3 test_postgres)
|
|
32
32
|
desc %{Test everything on SQLite3, MySQL and PostgreSQL}
|
33
33
|
task :test_full => %w(test test_mysql test_postgres)
|
34
34
|
|
35
|
-
desc %{Test everything with Rails 2.1.x, 2.0.x & 1.2.x gems}
|
35
|
+
desc %{Test everything with Rails 2.3.x, 2.2.x, 2.1.x, 2.0.x & 1.2.x gems}
|
36
36
|
task :test_all do
|
37
37
|
all = Rake::Task['test_full']
|
38
|
-
versions = %w(2.1.0 2.0.4 1.2.6)
|
38
|
+
versions = %w(2.3.1 2.2.2 2.1.0 2.0.4 1.2.6)
|
39
39
|
versions.each do |version|
|
40
40
|
ENV['RAILS_VERSION'] = "~> #{version}"
|
41
41
|
all.invoke
|
data/test/view_test.rb
CHANGED
@@ -165,6 +165,64 @@ class ViewTest < WillPaginate::ViewTestCase
|
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
168
|
+
## XML rendering ##
|
169
|
+
|
170
|
+
def test_xml_pagination_output_with_prev_and_next
|
171
|
+
paginate({:page => 2, :per_page => 5, :total_entries => 22}, :xml => true)
|
172
|
+
expected = <<-XML
|
173
|
+
<link rel="next" href="/foo/bar?page=3" />
|
174
|
+
<link rel="prev" href="/foo/bar?page=1" />
|
175
|
+
XML
|
176
|
+
assert_dom_equal expected, @html_result
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_xml_pagination_output_with_prev_only
|
180
|
+
paginate({:page => 2, :per_page => 5, :total_entries => 10}, :xml => true)
|
181
|
+
expected = <<-XML
|
182
|
+
<link rel="prev" href="/foo/bar?page=1" />
|
183
|
+
XML
|
184
|
+
assert_dom_equal expected, @html_result
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_xml_pagination_output_with_next_only
|
188
|
+
paginate({:page => 1, :per_page => 5, :total_entries => 10}, :xml => true)
|
189
|
+
expected = <<-XML
|
190
|
+
<link rel="next" href="/foo/bar?page=2" />
|
191
|
+
XML
|
192
|
+
assert_dom_equal expected, @html_result
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_xml_pagination_output_with_single_page
|
196
|
+
paginate({:page => 1, :per_page => 5, :total_entries => 5}, :xml => true)
|
197
|
+
expected = ""
|
198
|
+
assert_dom_equal expected, @html_result
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_xml_pagination_output_with_builder
|
202
|
+
expected = <<-XML
|
203
|
+
<navigation>
|
204
|
+
<link rel="next" href="/foo/bar?page=3" />
|
205
|
+
<link rel="prev" href="/foo/bar?page=1" />
|
206
|
+
</navigation>
|
207
|
+
XML
|
208
|
+
xml = Builder::XmlMarkup.new(:indent => 4, :margin => 1)
|
209
|
+
xml.navigation do
|
210
|
+
paginate({:page => 2, :per_page => 5, :total_entries => 22}, {:xml => true, :builder => xml})
|
211
|
+
end
|
212
|
+
assert_dom_equal expected, xml.target!
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_xml_pagination_output_with_buffer
|
216
|
+
buffer =" Result: \n"
|
217
|
+
paginate({:page => 2, :per_page => 5, :total_entries => 22}, {:xml => true, :buffer => buffer})
|
218
|
+
expected = <<-XML
|
219
|
+
Result:
|
220
|
+
<link rel="next" href="/foo/bar?page=3" />
|
221
|
+
<link rel="prev" href="/foo/bar?page=1" />
|
222
|
+
XML
|
223
|
+
assert_dom_equal expected, buffer
|
224
|
+
end
|
225
|
+
|
168
226
|
## other helpers ##
|
169
227
|
|
170
228
|
def test_paginated_section
|
@@ -192,13 +250,15 @@ class ViewTest < WillPaginate::ViewTestCase
|
|
192
250
|
@html_result
|
193
251
|
end
|
194
252
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
253
|
+
uses_mocha 'class name' do
|
254
|
+
def test_page_entries_info_with_longer_class_name
|
255
|
+
@template = '<%= page_entries_info collection %>'
|
256
|
+
collection = ('a'..'z').to_a.paginate
|
257
|
+
collection.first.stubs(:class).returns(mock('class', :name => 'ProjectType'))
|
199
258
|
|
200
|
-
|
201
|
-
|
259
|
+
paginate collection
|
260
|
+
assert @html_result.index('project types'), "expected <#{@html_result.inspect}> to mention 'project types'"
|
261
|
+
end
|
202
262
|
end
|
203
263
|
|
204
264
|
def test_page_entries_info_with_single_page_collection
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stilkov-will_paginate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Mislav Marohni\xC4\x87"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2009-03-09 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|