will_paginate 2.2.2 → 2.3.11
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.rdoc +110 -0
- data/README.rdoc +33 -61
- data/Rakefile +30 -93
- data/lib/will_paginate.rb +24 -20
- data/lib/will_paginate/collection.rb +7 -6
- data/lib/will_paginate/core_ext.rb +13 -2
- data/lib/will_paginate/finder.rb +42 -17
- data/lib/will_paginate/named_scope.rb +54 -16
- data/lib/will_paginate/named_scope_patch.rb +7 -9
- data/lib/will_paginate/version.rb +4 -4
- data/lib/will_paginate/view_helpers.rb +150 -74
- data/test/collection_test.rb +3 -0
- data/test/database.yml +3 -3
- data/test/finder_test.rb +71 -14
- data/test/fixtures/developer.rb +1 -0
- data/test/fixtures/topic.rb +4 -0
- data/test/helper.rb +4 -1
- data/test/lib/activerecord_test_case.rb +7 -0
- data/test/lib/activerecord_test_connector.rb +9 -3
- data/test/lib/view_test_process.rb +106 -0
- data/test/tasks.rake +59 -0
- data/test/view_test.rb +114 -85
- metadata +46 -20
- data/CHANGELOG +0 -49
data/test/collection_test.rb
CHANGED
data/test/database.yml
CHANGED
@@ -9,14 +9,14 @@ sqlite2:
|
|
9
9
|
|
10
10
|
mysql:
|
11
11
|
adapter: mysql
|
12
|
-
username:
|
13
|
-
password:
|
12
|
+
username: root
|
13
|
+
password:
|
14
14
|
encoding: utf8
|
15
15
|
database: will_paginate_unittest
|
16
16
|
|
17
17
|
postgres:
|
18
18
|
adapter: postgresql
|
19
19
|
username: mislav
|
20
|
-
password:
|
20
|
+
password:
|
21
21
|
database: will_paginate_unittest
|
22
22
|
min_messages: warning
|
data/test/finder_test.rb
CHANGED
@@ -261,6 +261,12 @@ class FinderTest < ActiveRecordTestCase
|
|
261
261
|
assert_equal 1, entries.total_entries, 'only one topic should be found'
|
262
262
|
end
|
263
263
|
end
|
264
|
+
|
265
|
+
def test_named_scope_with_include
|
266
|
+
project = projects(:active_record)
|
267
|
+
entries = project.topics.with_replies_starting_with('AR ').paginate(:page => 1, :per_page => 1)
|
268
|
+
assert_equal 1, entries.size
|
269
|
+
end
|
264
270
|
|
265
271
|
## misc ##
|
266
272
|
|
@@ -278,11 +284,11 @@ class FinderTest < ActiveRecordTestCase
|
|
278
284
|
# this functionality is temporarily removed
|
279
285
|
def xtest_pagination_defines_method
|
280
286
|
pager = "paginate_by_created_at"
|
281
|
-
assert !User.methods.
|
287
|
+
assert !User.methods.include_method?(pager), "User methods should not include `#{pager}` method"
|
282
288
|
# paginate!
|
283
289
|
assert 0, User.send(pager, nil, :page => 1).total_entries
|
284
290
|
# the paging finder should now be defined
|
285
|
-
assert User.methods.
|
291
|
+
assert User.methods.include_method?(pager), "`#{pager}` method should be defined on User"
|
286
292
|
end
|
287
293
|
|
288
294
|
# Is this Rails 2.0? Find out by testing find_all which was removed in [6998]
|
@@ -340,6 +346,12 @@ class FinderTest < ActiveRecordTestCase
|
|
340
346
|
Developer.paginate :select => 'DISTINCT salary', :page => 2
|
341
347
|
end
|
342
348
|
|
349
|
+
def test_count_with_scoped_select_when_distinct
|
350
|
+
Developer.stubs(:find).returns([])
|
351
|
+
Developer.expects(:count).with(:select => 'DISTINCT users.id').returns(0)
|
352
|
+
Developer.distinct.paginate :page => 2
|
353
|
+
end
|
354
|
+
|
343
355
|
def test_should_use_scoped_finders_if_present
|
344
356
|
# scope-out compatibility
|
345
357
|
Topic.expects(:find_best).returns(Array.new(5))
|
@@ -349,18 +361,15 @@ class FinderTest < ActiveRecordTestCase
|
|
349
361
|
end
|
350
362
|
|
351
363
|
def test_paginate_by_sql
|
352
|
-
|
353
|
-
Developer.
|
354
|
-
|
355
|
-
|
356
|
-
entries = Developer.paginate_by_sql 'sql', :page => 2, :per_page => 3
|
364
|
+
sql = "SELECT * FROM users WHERE type = 'Developer' ORDER BY id"
|
365
|
+
entries = Developer.paginate_by_sql(sql, :page => 2, :per_page => 3)
|
366
|
+
assert_equal 11, entries.total_entries
|
367
|
+
assert_equal [users(:dev_4), users(:dev_5), users(:dev_6)], entries
|
357
368
|
end
|
358
369
|
|
359
370
|
def test_paginate_by_sql_respects_total_entries_setting
|
360
|
-
|
361
|
-
Developer.
|
362
|
-
|
363
|
-
entries = Developer.paginate_by_sql 'sql', :page => 1, :total_entries => 999
|
371
|
+
sql = "SELECT * FROM users"
|
372
|
+
entries = Developer.paginate_by_sql(sql, :page => 1, :total_entries => 999)
|
364
373
|
assert_equal 999, entries.total_entries
|
365
374
|
end
|
366
375
|
|
@@ -390,12 +399,20 @@ class FinderTest < ActiveRecordTestCase
|
|
390
399
|
|
391
400
|
def test_paginating_finder_doesnt_mangle_options
|
392
401
|
Developer.expects(:find).returns([])
|
393
|
-
options = { :page => 1 }
|
394
|
-
options.expects(:delete).never
|
402
|
+
options = { :page => 1, :per_page => 2, :foo => 'bar' }
|
395
403
|
options_before = options.dup
|
396
404
|
|
397
405
|
Developer.paginate(options)
|
398
|
-
assert_equal
|
406
|
+
assert_equal options_before, options
|
407
|
+
end
|
408
|
+
|
409
|
+
def test_paginate_by_sql_doesnt_change_original_query
|
410
|
+
query = 'SQL QUERY'
|
411
|
+
original_query = query.dup
|
412
|
+
Developer.expects(:find_by_sql).returns([])
|
413
|
+
|
414
|
+
Developer.paginate_by_sql query, :page => 1
|
415
|
+
assert_equal original_query, query
|
399
416
|
end
|
400
417
|
|
401
418
|
def test_paginated_each
|
@@ -412,5 +429,45 @@ class FinderTest < ActiveRecordTestCase
|
|
412
429
|
|
413
430
|
assert_equal 14, Developer.paginated_each(:page => '2') { }
|
414
431
|
end
|
432
|
+
|
433
|
+
def test_paginated_each_with_named_scope
|
434
|
+
assert_equal 2, Developer.poor.paginated_each(:per_page => 1) {
|
435
|
+
assert_equal 11, Developer.count
|
436
|
+
}
|
437
|
+
end
|
438
|
+
|
439
|
+
# detect ActiveRecord 2.1
|
440
|
+
if ActiveRecord::Base.private_methods.include_method?(:references_eager_loaded_tables?)
|
441
|
+
def test_removes_irrelevant_includes_in_count
|
442
|
+
Developer.expects(:find).returns([1])
|
443
|
+
Developer.expects(:count).with({}).returns(0)
|
444
|
+
|
445
|
+
Developer.paginate :page => 1, :per_page => 1, :include => :projects
|
446
|
+
end
|
447
|
+
|
448
|
+
def test_doesnt_remove_referenced_includes_in_count
|
449
|
+
Developer.expects(:find).returns([1])
|
450
|
+
Developer.expects(:count).with({ :include => :projects, :conditions => 'projects.id > 2' }).returns(0)
|
451
|
+
|
452
|
+
Developer.paginate :page => 1, :per_page => 1,
|
453
|
+
:include => :projects, :conditions => 'projects.id > 2'
|
454
|
+
end
|
455
|
+
end
|
456
|
+
|
457
|
+
def test_paginate_from
|
458
|
+
result = Developer.paginate(:from => 'users', :page => 1, :per_page => 1)
|
459
|
+
assert_equal 1, result.size
|
460
|
+
end
|
461
|
+
|
462
|
+
def test_hmt_with_include
|
463
|
+
# ticket #220
|
464
|
+
reply = projects(:active_record).replies.find(:first, :order => 'replies.id')
|
465
|
+
assert_equal replies(:decisive), reply
|
466
|
+
|
467
|
+
# ticket #223
|
468
|
+
Project.find(1, :include => :replies)
|
469
|
+
|
470
|
+
# I cannot reproduce any of the failures from those reports :(
|
471
|
+
end
|
415
472
|
end
|
416
473
|
end
|
data/test/fixtures/developer.rb
CHANGED
data/test/fixtures/topic.rb
CHANGED
@@ -3,4 +3,8 @@ class Topic < ActiveRecord::Base
|
|
3
3
|
belongs_to :project
|
4
4
|
|
5
5
|
named_scope :mentions_activerecord, :conditions => ['topics.title LIKE ?', '%ActiveRecord%']
|
6
|
+
|
7
|
+
named_scope :with_replies_starting_with, lambda { |text|
|
8
|
+
{ :conditions => "replies.content LIKE '#{text}%' ", :include => :replies }
|
9
|
+
}
|
6
10
|
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')
|
@@ -16,16 +16,20 @@ class ActiveRecordTestConnector
|
|
16
16
|
unless self.connected || !self.able_to_connect
|
17
17
|
setup_connection
|
18
18
|
load_schema
|
19
|
-
|
19
|
+
add_load_path FIXTURES_PATH
|
20
20
|
self.connected = true
|
21
21
|
end
|
22
22
|
rescue Exception => e # errors from ActiveRecord setup
|
23
|
-
$stderr.puts "\nSkipping ActiveRecord tests: #{e}"
|
24
|
-
$stderr.puts "Install SQLite3 to run the full test suite for will_paginate.\n\n"
|
23
|
+
$stderr.puts "\nSkipping ActiveRecord tests: #{e}\n\n"
|
25
24
|
self.able_to_connect = false
|
26
25
|
end
|
27
26
|
|
28
27
|
private
|
28
|
+
|
29
|
+
def self.add_load_path(path)
|
30
|
+
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
|
31
|
+
dep.load_paths.unshift path
|
32
|
+
end
|
29
33
|
|
30
34
|
def self.setup_connection
|
31
35
|
db = ENV['DB'].blank?? 'sqlite3' : ENV['DB']
|
@@ -37,6 +41,8 @@ class ActiveRecordTestConnector
|
|
37
41
|
ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
|
38
42
|
puts "using #{configuration['adapter']} adapter" unless ENV['DB'].blank?
|
39
43
|
|
44
|
+
gem 'sqlite3-ruby' if 'sqlite3' == db
|
45
|
+
|
40
46
|
ActiveRecord::Base.establish_connection(configuration)
|
41
47
|
ActiveRecord::Base.configurations = { db => configuration }
|
42
48
|
prepare ActiveRecord::Base.connection
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'will_paginate/core_ext'
|
1
2
|
require 'action_controller'
|
2
3
|
require 'action_controller/test_process'
|
3
4
|
|
@@ -6,11 +7,112 @@ WillPaginate.enable_actionpack
|
|
6
7
|
|
7
8
|
ActionController::Routing::Routes.draw do |map|
|
8
9
|
map.connect 'dummy/page/:page', :controller => 'dummy'
|
10
|
+
map.connect 'dummy/dots/page.:page', :controller => 'dummy', :action => 'dots'
|
11
|
+
map.connect 'ibocorp/:page', :controller => 'ibocorp',
|
12
|
+
:requirements => { :page => /\d+/ },
|
13
|
+
:defaults => { :page => 1 }
|
14
|
+
|
9
15
|
map.connect ':controller/:action/:id'
|
10
16
|
end
|
11
17
|
|
12
18
|
ActionController::Base.perform_caching = false
|
13
19
|
|
20
|
+
class WillPaginate::ViewTestCase < Test::Unit::TestCase
|
21
|
+
if defined?(ActionController::TestCase::Assertions)
|
22
|
+
include ActionController::TestCase::Assertions
|
23
|
+
end
|
24
|
+
if defined?(ActiveSupport::Testing::Deprecation)
|
25
|
+
include ActiveSupport::Testing::Deprecation
|
26
|
+
end
|
27
|
+
|
28
|
+
def setup
|
29
|
+
super
|
30
|
+
@controller = DummyController.new
|
31
|
+
@request = @controller.request
|
32
|
+
@html_result = nil
|
33
|
+
@template = '<%= will_paginate collection, options %>'
|
34
|
+
|
35
|
+
@view = ActionView::Base.new
|
36
|
+
@view.assigns['controller'] = @controller
|
37
|
+
@view.assigns['_request'] = @request
|
38
|
+
@view.assigns['_params'] = @request.params
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_no_complain; end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def paginate(collection = {}, options = {}, &block)
|
46
|
+
if collection.instance_of? Hash
|
47
|
+
page_options = { :page => 1, :total_entries => 11, :per_page => 4 }.merge(collection)
|
48
|
+
collection = [1].paginate(page_options)
|
49
|
+
end
|
50
|
+
|
51
|
+
locals = { :collection => collection, :options => options }
|
52
|
+
|
53
|
+
unless @view.respond_to? :render_template
|
54
|
+
# Rails 2.2
|
55
|
+
@html_result = ActionView::InlineTemplate.new(@template).render(@view, locals)
|
56
|
+
else
|
57
|
+
if defined? ActionView::InlineTemplate
|
58
|
+
# Rails 2.1
|
59
|
+
args = [ ActionView::InlineTemplate.new(@view, @template, locals) ]
|
60
|
+
else
|
61
|
+
# older Rails versions
|
62
|
+
args = [nil, @template, nil, locals]
|
63
|
+
end
|
64
|
+
|
65
|
+
@html_result = @view.render_template(*args)
|
66
|
+
end
|
67
|
+
|
68
|
+
@html_document = HTML::Document.new(@html_result, true, false)
|
69
|
+
|
70
|
+
if block_given?
|
71
|
+
classname = options[:class] || WillPaginate::ViewHelpers.pagination_options[:class]
|
72
|
+
assert_select("div.#{classname}", 1, 'no main DIV', &block)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def response_from_page_or_rjs
|
77
|
+
@html_document.root
|
78
|
+
end
|
79
|
+
|
80
|
+
def validate_page_numbers expected, links, param_name = :page
|
81
|
+
param_pattern = /\W#{CGI.escape(param_name.to_s)}=([^&]*)/
|
82
|
+
|
83
|
+
assert_equal(expected, links.map { |e|
|
84
|
+
e['href'] =~ param_pattern
|
85
|
+
$1 ? $1.to_i : $1
|
86
|
+
})
|
87
|
+
end
|
88
|
+
|
89
|
+
def assert_links_match pattern, links = nil, numbers = nil
|
90
|
+
links ||= assert_select 'div.pagination a[href]' do |elements|
|
91
|
+
elements
|
92
|
+
end
|
93
|
+
|
94
|
+
pages = [] if numbers
|
95
|
+
|
96
|
+
links.each do |el|
|
97
|
+
assert_match pattern, el['href']
|
98
|
+
if numbers
|
99
|
+
el['href'] =~ pattern
|
100
|
+
pages << ($1.nil?? nil : $1.to_i)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
assert_equal numbers, pages, "page numbers don't match" if numbers
|
105
|
+
end
|
106
|
+
|
107
|
+
def assert_no_links_match pattern
|
108
|
+
assert_select 'div.pagination a[href]' do |elements|
|
109
|
+
elements.each do |el|
|
110
|
+
assert_no_match pattern, el['href']
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
14
116
|
class DummyRequest
|
15
117
|
attr_accessor :symbolized_path_parameters
|
16
118
|
|
@@ -46,6 +148,10 @@ class DummyController
|
|
46
148
|
@request = DummyRequest.new
|
47
149
|
@url = ActionController::UrlRewriter.new(@request, @request.params)
|
48
150
|
end
|
151
|
+
|
152
|
+
def params
|
153
|
+
@request.params
|
154
|
+
end
|
49
155
|
|
50
156
|
def url_for(params)
|
51
157
|
@url.rewrite(params)
|
data/test/tasks.rake
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
desc 'Test the will_paginate plugin.'
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.pattern = 'test/**/*_test.rb'
|
6
|
+
t.verbose = true
|
7
|
+
t.libs << 'test'
|
8
|
+
end
|
9
|
+
|
10
|
+
# I want to specify environment variables at call time
|
11
|
+
class EnvTestTask < Rake::TestTask
|
12
|
+
attr_accessor :env
|
13
|
+
|
14
|
+
def ruby(*args)
|
15
|
+
env.each { |key, value| ENV[key] = value } if env
|
16
|
+
super
|
17
|
+
env.keys.each { |key| ENV.delete key } if env
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
for configuration in %w( sqlite3 mysql postgres )
|
22
|
+
EnvTestTask.new("test_#{configuration}") do |t|
|
23
|
+
t.pattern = 'test/finder_test.rb'
|
24
|
+
t.verbose = true
|
25
|
+
t.env = { 'DB' => configuration }
|
26
|
+
t.libs << 'test'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
task :test_databases => %w(test_mysql test_sqlite3 test_postgres)
|
31
|
+
|
32
|
+
desc %{Test everything on SQLite3, MySQL and PostgreSQL}
|
33
|
+
task :test_full => %w(test test_mysql test_postgres)
|
34
|
+
|
35
|
+
desc %{Test everything with Rails 2.1.x, 2.0.x & 1.2.x gems}
|
36
|
+
task :test_all do
|
37
|
+
all = Rake::Task['test_full']
|
38
|
+
versions = %w(2.3.2 2.2.2 2.1.0 2.0.4 1.2.6)
|
39
|
+
versions.each do |version|
|
40
|
+
ENV['RAILS_VERSION'] = "~> #{version}"
|
41
|
+
all.invoke
|
42
|
+
reset_invoked unless version == versions.last
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def reset_invoked
|
47
|
+
%w( test_full test test_mysql test_postgres ).each do |name|
|
48
|
+
Rake::Task[name].instance_variable_set '@already_invoked', false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
task :rcov do
|
53
|
+
excludes = %w( lib/will_paginate/named_scope*
|
54
|
+
lib/will_paginate/core_ext.rb
|
55
|
+
lib/will_paginate.rb
|
56
|
+
rails* )
|
57
|
+
|
58
|
+
system %[rcov -Itest:lib test/*.rb -x #{excludes.join(',')}]
|
59
|
+
end
|
data/test/view_test.rb
CHANGED
@@ -1,22 +1,19 @@
|
|
1
1
|
require 'helper'
|
2
|
-
require 'action_controller'
|
3
2
|
require 'lib/view_test_process'
|
4
3
|
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@controller = DummyController.new
|
10
|
-
@request = @controller.request
|
11
|
-
@html_result = nil
|
12
|
-
@template = '<%= will_paginate collection, options %>'
|
13
|
-
|
14
|
-
@view = ActionView::Base.new
|
15
|
-
@view.assigns['controller'] = @controller
|
16
|
-
@view.assigns['_request'] = @request
|
17
|
-
@view.assigns['_params'] = @request.params
|
4
|
+
class AdditionalLinkAttributesRenderer < WillPaginate::LinkRenderer
|
5
|
+
def initialize(link_attributes = nil)
|
6
|
+
super()
|
7
|
+
@additional_link_attributes = link_attributes || { :default => 'true' }
|
18
8
|
end
|
19
9
|
|
10
|
+
def page_link(page, text, attributes = {})
|
11
|
+
@template.link_to text, url_for(page), attributes.merge(@additional_link_attributes)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ViewTest < WillPaginate::ViewTestCase
|
16
|
+
|
20
17
|
## basic pagination ##
|
21
18
|
|
22
19
|
def test_will_paginate
|
@@ -39,7 +36,7 @@ class ViewTest < Test::Unit::TestCase
|
|
39
36
|
|
40
37
|
def test_will_paginate_with_options
|
41
38
|
paginate({ :page => 2 },
|
42
|
-
:class => 'will_paginate', :
|
39
|
+
:class => 'will_paginate', :previous_label => 'Prev', :next_label => 'Next') do
|
43
40
|
assert_select 'a[href]', 4 do |elements|
|
44
41
|
validate_page_numbers [1,1,3,3], elements
|
45
42
|
# test rel attribute values:
|
@@ -57,12 +54,40 @@ class ViewTest < Test::Unit::TestCase
|
|
57
54
|
end
|
58
55
|
end
|
59
56
|
|
57
|
+
def test_will_paginate_using_renderer_class
|
58
|
+
paginate({}, :renderer => AdditionalLinkAttributesRenderer) do
|
59
|
+
assert_select 'a[default=true]', 3
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_will_paginate_using_renderer_instance
|
64
|
+
renderer = WillPaginate::LinkRenderer.new
|
65
|
+
renderer.gap_marker = '<span class="my-gap">~~</span>'
|
66
|
+
|
67
|
+
paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
|
68
|
+
assert_select 'span.my-gap', '~~'
|
69
|
+
end
|
70
|
+
|
71
|
+
renderer = AdditionalLinkAttributesRenderer.new(:title => 'rendered')
|
72
|
+
paginate({}, :renderer => renderer) do
|
73
|
+
assert_select 'a[title=rendered]', 3
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
60
77
|
def test_prev_next_links_have_classnames
|
61
78
|
paginate do |pagination|
|
62
79
|
assert_select 'span.disabled.prev_page:first-child'
|
63
80
|
assert_select 'a.next_page[href]:last-child'
|
64
81
|
end
|
65
82
|
end
|
83
|
+
|
84
|
+
def test_prev_label_deprecated
|
85
|
+
assert_deprecated ':previous_label' do
|
86
|
+
paginate({ :page => 2 }, :prev_label => 'Deprecated') do
|
87
|
+
assert_select 'a[href]:first-child', 'Deprecated'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
66
91
|
|
67
92
|
def test_full_output
|
68
93
|
paginate
|
@@ -78,6 +103,16 @@ class ViewTest < Test::Unit::TestCase
|
|
78
103
|
assert_dom_equal expected, @html_result
|
79
104
|
end
|
80
105
|
|
106
|
+
def test_escaping_of_urls
|
107
|
+
paginate({:page => 1, :per_page => 1, :total_entries => 2},
|
108
|
+
:page_links => false, :params => { :tag => '<br>' })
|
109
|
+
|
110
|
+
assert_select 'a[href]', 1 do |links|
|
111
|
+
query = links.first['href'].split('?', 2)[1]
|
112
|
+
assert_equal %w(page=2 tag=%3Cbr%3E), query.split('&').sort
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
81
116
|
## advanced options for pagination ##
|
82
117
|
|
83
118
|
def test_will_paginate_without_container
|
@@ -149,27 +184,56 @@ class ViewTest < Test::Unit::TestCase
|
|
149
184
|
array = ('a'..'z').to_a
|
150
185
|
|
151
186
|
paginate array.paginate(:page => 2, :per_page => 5)
|
152
|
-
assert_equal %{Displaying
|
187
|
+
assert_equal %{Displaying strings <b>6 - 10</b> of <b>26</b> in total},
|
153
188
|
@html_result
|
154
189
|
|
155
190
|
paginate array.paginate(:page => 7, :per_page => 4)
|
156
|
-
assert_equal %{Displaying
|
191
|
+
assert_equal %{Displaying strings <b>25 - 26</b> of <b>26</b> in total},
|
157
192
|
@html_result
|
158
193
|
end
|
159
194
|
|
195
|
+
uses_mocha 'class name' do
|
196
|
+
def test_page_entries_info_with_longer_class_name
|
197
|
+
@template = '<%= page_entries_info collection %>'
|
198
|
+
collection = ('a'..'z').to_a.paginate
|
199
|
+
collection.first.stubs(:class).returns(mock('class', :name => 'ProjectType'))
|
200
|
+
|
201
|
+
paginate collection
|
202
|
+
assert @html_result.index('project types'), "expected <#{@html_result.inspect}> to mention 'project types'"
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
160
206
|
def test_page_entries_info_with_single_page_collection
|
161
207
|
@template = '<%= page_entries_info collection %>'
|
162
208
|
|
163
209
|
paginate(('a'..'d').to_a.paginate(:page => 1, :per_page => 5))
|
164
|
-
assert_equal %{Displaying <b>all 4</b>
|
210
|
+
assert_equal %{Displaying <b>all 4</b> strings}, @html_result
|
165
211
|
|
166
212
|
paginate(['a'].paginate(:page => 1, :per_page => 5))
|
167
|
-
assert_equal %{Displaying <b>1</b>
|
213
|
+
assert_equal %{Displaying <b>1</b> string}, @html_result
|
168
214
|
|
169
215
|
paginate([].paginate(:page => 1, :per_page => 5))
|
170
216
|
assert_equal %{No entries found}, @html_result
|
171
217
|
end
|
172
218
|
|
219
|
+
def test_page_entries_info_with_custom_entry_name
|
220
|
+
@template = '<%= page_entries_info collection, :entry_name => "author" %>'
|
221
|
+
|
222
|
+
entries = (1..20).to_a
|
223
|
+
|
224
|
+
paginate(entries.paginate(:page => 1, :per_page => 5))
|
225
|
+
assert_equal %{Displaying authors <b>1 - 5</b> of <b>20</b> in total}, @html_result
|
226
|
+
|
227
|
+
paginate(entries.paginate(:page => 1, :per_page => 20))
|
228
|
+
assert_equal %{Displaying <b>all 20</b> authors}, @html_result
|
229
|
+
|
230
|
+
paginate(['a'].paginate(:page => 1, :per_page => 5))
|
231
|
+
assert_equal %{Displaying <b>1</b> author}, @html_result
|
232
|
+
|
233
|
+
paginate([].paginate(:page => 1, :per_page => 5))
|
234
|
+
assert_equal %{No authors found}, @html_result
|
235
|
+
end
|
236
|
+
|
173
237
|
## parameter handling in page links ##
|
174
238
|
|
175
239
|
def test_will_paginate_preserves_parameters_on_get
|
@@ -190,6 +254,11 @@ class ViewTest < Test::Unit::TestCase
|
|
190
254
|
assert_links_match /foo=bar/
|
191
255
|
end
|
192
256
|
|
257
|
+
def test_adding_anchor_parameter
|
258
|
+
paginate({}, :params => { :anchor => 'anchor' })
|
259
|
+
assert_links_match /#anchor$/
|
260
|
+
end
|
261
|
+
|
193
262
|
def test_removing_arbitrary_parameters
|
194
263
|
@request.params :foo => 'bar'
|
195
264
|
paginate({}, :params => { :foo => nil })
|
@@ -208,6 +277,14 @@ class ViewTest < Test::Unit::TestCase
|
|
208
277
|
end
|
209
278
|
end
|
210
279
|
end
|
280
|
+
|
281
|
+
def test_will_paginate_with_atmark_url
|
282
|
+
@request.symbolized_path_parameters[:action] = "@tag"
|
283
|
+
renderer = WillPaginate::LinkRenderer.new
|
284
|
+
|
285
|
+
paginate({ :page => 1 }, :renderer=>renderer)
|
286
|
+
assert_links_match %r[/foo/@tag\?page=\d]
|
287
|
+
end
|
211
288
|
|
212
289
|
def test_complex_custom_page_param
|
213
290
|
@request.params :developers => { :page => 2 }
|
@@ -229,6 +306,24 @@ class ViewTest < Test::Unit::TestCase
|
|
229
306
|
end
|
230
307
|
end
|
231
308
|
|
309
|
+
def test_custom_routing_page_param_with_dot_separator
|
310
|
+
@request.symbolized_path_parameters.update :controller => 'dummy', :action => 'dots'
|
311
|
+
paginate :per_page => 2 do
|
312
|
+
assert_select 'a[href]', 6 do |links|
|
313
|
+
assert_links_match %r{/page\.(\d+)$}, links, [2, 3, 4, 5, 6, 2]
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_custom_routing_with_first_page_hidden
|
319
|
+
@request.symbolized_path_parameters.update :controller => 'ibocorp', :action => nil
|
320
|
+
paginate :page => 2, :per_page => 2 do
|
321
|
+
assert_select 'a[href]', 7 do |links|
|
322
|
+
assert_links_match %r{/ibocorp(?:/(\d+))?$}, links, [nil, nil, 3, 4, 5, 6, 3]
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
232
327
|
## internal hardcore stuff ##
|
233
328
|
|
234
329
|
class LegacyCollection < WillPaginate::Collection
|
@@ -275,70 +370,4 @@ class ViewTest < Test::Unit::TestCase
|
|
275
370
|
end
|
276
371
|
end
|
277
372
|
|
278
|
-
|
279
|
-
protected
|
280
|
-
|
281
|
-
def paginate(collection = {}, options = {}, &block)
|
282
|
-
if collection.instance_of? Hash
|
283
|
-
page_options = { :page => 1, :total_entries => 11, :per_page => 4 }.merge(collection)
|
284
|
-
collection = [1].paginate(page_options)
|
285
|
-
end
|
286
|
-
|
287
|
-
locals = { :collection => collection, :options => options }
|
288
|
-
|
289
|
-
if defined? ActionView::Template
|
290
|
-
# Rails 2.1
|
291
|
-
args = [ ActionView::Template.new(@view, @template, false, locals, true, nil) ]
|
292
|
-
else
|
293
|
-
# older Rails versions
|
294
|
-
args = [nil, @template, nil, locals]
|
295
|
-
end
|
296
|
-
|
297
|
-
@html_result = @view.render_template(*args)
|
298
|
-
@html_document = HTML::Document.new(@html_result, true, false)
|
299
|
-
|
300
|
-
if block_given?
|
301
|
-
classname = options[:class] || WillPaginate::ViewHelpers.pagination_options[:class]
|
302
|
-
assert_select("div.#{classname}", 1, 'no main DIV', &block)
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
def response_from_page_or_rjs
|
307
|
-
@html_document.root
|
308
|
-
end
|
309
|
-
|
310
|
-
def validate_page_numbers expected, links, param_name = :page
|
311
|
-
param_pattern = /\W#{CGI.escape(param_name.to_s)}=([^&]*)/
|
312
|
-
|
313
|
-
assert_equal(expected, links.map { |e|
|
314
|
-
e['href'] =~ param_pattern
|
315
|
-
$1 ? $1.to_i : $1
|
316
|
-
})
|
317
|
-
end
|
318
|
-
|
319
|
-
def assert_links_match pattern, links = nil, numbers = nil
|
320
|
-
links ||= assert_select 'div.pagination a[href]' do |elements|
|
321
|
-
elements
|
322
|
-
end
|
323
|
-
|
324
|
-
pages = [] if numbers
|
325
|
-
|
326
|
-
links.each do |el|
|
327
|
-
assert_match pattern, el['href']
|
328
|
-
if numbers
|
329
|
-
el['href'] =~ pattern
|
330
|
-
pages << $1.to_i
|
331
|
-
end
|
332
|
-
end
|
333
|
-
|
334
|
-
assert_equal pages, numbers, "page numbers don't match" if numbers
|
335
|
-
end
|
336
|
-
|
337
|
-
def assert_no_links_match pattern
|
338
|
-
assert_select 'div.pagination a[href]' do |elements|
|
339
|
-
elements.each do |el|
|
340
|
-
assert_no_match pattern, el['href']
|
341
|
-
end
|
342
|
-
end
|
343
|
-
end
|
344
373
|
end
|