will_paginate 2.1.0
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 +3 -0
- data/LICENSE +18 -0
- data/Manifest.txt +34 -0
- data/README +152 -0
- data/Rakefile +65 -0
- data/config/release.rb +82 -0
- data/lib/will_paginate/collection.rb +132 -0
- data/lib/will_paginate/core_ext.rb +80 -0
- data/lib/will_paginate/finder.rb +214 -0
- data/lib/will_paginate/version.rb +9 -0
- data/lib/will_paginate/view_helpers.rb +218 -0
- data/lib/will_paginate.rb +63 -0
- data/setup.rb +1585 -0
- data/test/array_pagination_test.rb +131 -0
- data/test/boot.rb +23 -0
- data/test/console +9 -0
- data/test/finder_test.rb +322 -0
- data/test/fixtures/admin.rb +3 -0
- data/test/fixtures/developer.rb +11 -0
- data/test/fixtures/developers_projects.yml +13 -0
- data/test/fixtures/project.rb +15 -0
- data/test/fixtures/projects.yml +7 -0
- data/test/fixtures/replies.yml +29 -0
- data/test/fixtures/reply.rb +5 -0
- data/test/fixtures/schema.rb +38 -0
- data/test/fixtures/topic.rb +4 -0
- data/test/fixtures/topics.yml +30 -0
- data/test/fixtures/user.rb +2 -0
- data/test/fixtures/users.yml +35 -0
- data/test/helper.rb +25 -0
- data/test/lib/activerecord_test_case.rb +23 -0
- data/test/lib/activerecord_test_connector.rb +60 -0
- data/test/lib/load_fixtures.rb +13 -0
- data/test/pagination_test.rb +257 -0
- metadata +99 -0
data/test/helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
# gem install redgreen for colored test output
|
5
|
+
begin require 'redgreen'; rescue LoadError; end
|
6
|
+
|
7
|
+
require File.join(File.dirname(__FILE__), 'boot') unless defined?(ActiveRecord)
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
protected
|
11
|
+
def assert_respond_to_all object, methods
|
12
|
+
methods.each do |method|
|
13
|
+
[method.to_s, method.to_sym].each { |m| assert_respond_to object, m }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Wrap tests that use Mocha and skip if unavailable.
|
19
|
+
def uses_mocha(test_name)
|
20
|
+
require 'mocha' unless Object.const_defined?(:Mocha)
|
21
|
+
rescue LoadError => load_error
|
22
|
+
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
|
23
|
+
else
|
24
|
+
yield
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'activerecord_test_connector')
|
2
|
+
|
3
|
+
class ActiveRecordTestCase < Test::Unit::TestCase
|
4
|
+
# Set our fixture path
|
5
|
+
if ActiveRecordTestConnector.able_to_connect
|
6
|
+
self.fixture_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
|
7
|
+
self.use_transactional_fixtures = true
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.fixtures(*args)
|
11
|
+
super if ActiveRecordTestConnector.connected
|
12
|
+
end
|
13
|
+
|
14
|
+
def run(*args)
|
15
|
+
super if ActiveRecordTestConnector.connected
|
16
|
+
end
|
17
|
+
|
18
|
+
# Default so Test::Unit::TestCase doesn't complain
|
19
|
+
def test_truth
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
ActiveRecordTestConnector.setup
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/version'
|
3
|
+
require 'active_record/fixtures'
|
4
|
+
|
5
|
+
class ActiveRecordTestConnector
|
6
|
+
cattr_accessor :able_to_connect
|
7
|
+
cattr_accessor :connected
|
8
|
+
|
9
|
+
# Set our defaults
|
10
|
+
self.connected = false
|
11
|
+
self.able_to_connect = true
|
12
|
+
|
13
|
+
def self.setup
|
14
|
+
unless self.connected || !self.able_to_connect
|
15
|
+
setup_connection
|
16
|
+
load_schema
|
17
|
+
# require_fixture_models
|
18
|
+
Dependencies.load_paths.unshift(File.dirname(__FILE__) + "/../fixtures")
|
19
|
+
self.connected = true
|
20
|
+
end
|
21
|
+
rescue Exception => e # errors from ActiveRecord setup
|
22
|
+
$stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
|
23
|
+
#$stderr.puts " #{e.backtrace.join("\n ")}\n"
|
24
|
+
self.able_to_connect = false
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def self.setup_connection
|
30
|
+
db = ENV['DB'].blank?? 'sqlite3' : ENV['DB']
|
31
|
+
|
32
|
+
configurations = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'database.yml'))
|
33
|
+
raise "no configuration for '#{db}'" unless configurations.key? db
|
34
|
+
configuration = configurations[db]
|
35
|
+
|
36
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
|
37
|
+
puts "using #{configuration['adapter']} adapter" unless ENV['DB'].blank?
|
38
|
+
|
39
|
+
ActiveRecord::Base.establish_connection(configuration)
|
40
|
+
ActiveRecord::Base.configurations = { db => configuration }
|
41
|
+
ActiveRecord::Base.connection
|
42
|
+
|
43
|
+
unless Object.const_defined?(:QUOTED_TYPE)
|
44
|
+
Object.send :const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.load_schema
|
49
|
+
ActiveRecord::Base.silence do
|
50
|
+
ActiveRecord::Migration.verbose = false
|
51
|
+
load File.dirname(__FILE__) + "/../fixtures/schema.rb"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.require_fixture_models
|
56
|
+
models = Dir.glob(File.dirname(__FILE__) + "/../fixtures/*.rb")
|
57
|
+
models = (models.grep(/user.rb/) + models).uniq
|
58
|
+
models.each { |f| require f }
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
dirname = File.dirname(__FILE__)
|
2
|
+
require File.join(dirname, '..', 'boot')
|
3
|
+
require File.join(dirname, 'activerecord_test_connector')
|
4
|
+
|
5
|
+
# setup the connection
|
6
|
+
ActiveRecordTestConnector.setup
|
7
|
+
|
8
|
+
# load all fixtures
|
9
|
+
fixture_path = File.join(dirname, '..', 'fixtures')
|
10
|
+
Fixtures.create_fixtures(fixture_path, ActiveRecord::Base.connection.tables)
|
11
|
+
|
12
|
+
require 'will_paginate'
|
13
|
+
WillPaginate.enable_activerecord
|
@@ -0,0 +1,257 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
require 'action_controller'
|
3
|
+
require File.dirname(__FILE__) + '/lib/html_inner_text'
|
4
|
+
|
5
|
+
ActionController::Routing::Routes.draw do |map|
|
6
|
+
map.connect ':controller/:action/:id'
|
7
|
+
end
|
8
|
+
|
9
|
+
ActionController::Base.perform_caching = false
|
10
|
+
|
11
|
+
require 'will_paginate'
|
12
|
+
WillPaginate.enable_actionpack
|
13
|
+
|
14
|
+
class PaginationTest < Test::Unit::TestCase
|
15
|
+
|
16
|
+
class DevelopersController < ActionController::Base
|
17
|
+
def list_developers
|
18
|
+
@options = session[:wp] || {}
|
19
|
+
|
20
|
+
@developers = (1..11).to_a.paginate(
|
21
|
+
:page => params[@options[:param_name] || :page] || 1,
|
22
|
+
:per_page => params[:per_page] || 4
|
23
|
+
)
|
24
|
+
|
25
|
+
render :inline => '<%= will_paginate @developers, @options %>'
|
26
|
+
end
|
27
|
+
|
28
|
+
def guess_collection_name
|
29
|
+
@developers = session[:wp]
|
30
|
+
@options = session[:wp_options]
|
31
|
+
render :inline => '<%= will_paginate @options %>'
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
def rescue_errors(e) raise e end
|
36
|
+
def rescue_action(e) raise e end
|
37
|
+
end
|
38
|
+
|
39
|
+
def setup
|
40
|
+
@controller = DevelopersController.new
|
41
|
+
@request = ActionController::TestRequest.new
|
42
|
+
@response = ActionController::TestResponse.new
|
43
|
+
super
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_will_paginate
|
47
|
+
get :list_developers
|
48
|
+
|
49
|
+
entries = assigns :developers
|
50
|
+
assert entries
|
51
|
+
assert_equal 4, entries.size
|
52
|
+
|
53
|
+
assert_select 'div.pagination', 1, 'no main DIV' do |pagination|
|
54
|
+
assert_select 'a[href]', 3 do |elements|
|
55
|
+
validate_page_numbers [2,3,2], elements
|
56
|
+
assert_select elements.last, ':last-child', "Next »"
|
57
|
+
end
|
58
|
+
assert_select 'span', 2
|
59
|
+
assert_select 'span.disabled:first-child', "« Previous"
|
60
|
+
assert_select 'span.current', entries.current_page.to_s
|
61
|
+
assert_equal '« Previous 1 2 3 Next »', pagination.first.inner_text
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_will_paginate_with_options
|
66
|
+
get :list_developers, { :page => 2 }, :wp => {
|
67
|
+
:class => 'will_paginate', :prev_label => 'Prev', :next_label => 'Next'
|
68
|
+
}
|
69
|
+
assert_response :success
|
70
|
+
|
71
|
+
entries = assigns :developers
|
72
|
+
assert entries
|
73
|
+
assert_equal 4, entries.size
|
74
|
+
|
75
|
+
assert_select 'div.will_paginate', 1, 'no main DIV' do
|
76
|
+
assert_select 'a[href]', 4 do |elements|
|
77
|
+
validate_page_numbers [1,1,3,3], elements
|
78
|
+
assert_select elements.first, 'a', "Prev"
|
79
|
+
assert_select elements.last, 'a', "Next"
|
80
|
+
end
|
81
|
+
assert_select 'span.current', entries.current_page.to_s
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_will_paginate_without_container
|
86
|
+
get :list_developers, {}, :wp => { :container => false }
|
87
|
+
assert_select 'div.pagination', 0, 'no main DIV'
|
88
|
+
assert_select 'a[href]', 3
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_will_paginate_without_page_links
|
92
|
+
get :list_developers, { :page => 2 }, :wp => { :page_links => false }
|
93
|
+
assert_select 'a[href]', 2 do |elements|
|
94
|
+
validate_page_numbers [1,3], elements
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_will_paginate_preserves_parameters_on_get
|
99
|
+
get :list_developers, :foo => { :bar => 'baz' }
|
100
|
+
assert_links_match /foo%5Bbar%5D=baz/
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_will_paginate_doesnt_preserve_parameters_on_post
|
104
|
+
post :list_developers, :foo => 'bar'
|
105
|
+
assert_no_links_match /foo=bar/
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_adding_additional_parameters
|
109
|
+
get :list_developers, {}, :wp => { :params => { :foo => 'bar' } }
|
110
|
+
assert_links_match /foo=bar/
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_removing_arbitrary_parameters
|
114
|
+
get :list_developers, { :foo => 'bar' }, :wp => { :params => { :foo => nil } }
|
115
|
+
assert_no_links_match /foo=bar/
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_adding_additional_route_parameters
|
119
|
+
get :list_developers, {}, :wp => { :params => { :controller => 'baz' } }
|
120
|
+
assert_links_match %r{\Wbaz/list_developers\W}
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_will_paginate_with_custom_page_param
|
124
|
+
get :list_developers, { :developers_page => 2 }, :wp => { :param_name => :developers_page }
|
125
|
+
assert_response :success
|
126
|
+
|
127
|
+
entries = assigns :developers
|
128
|
+
assert entries
|
129
|
+
assert_equal 4, entries.size
|
130
|
+
|
131
|
+
assert_select 'div.pagination', 1, 'no main DIV' do
|
132
|
+
assert_select 'a[href]', 4 do |elements|
|
133
|
+
validate_page_numbers [1,1,3,3], elements, :developers_page
|
134
|
+
end
|
135
|
+
assert_select 'span.current', entries.current_page.to_s
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_will_paginate_windows
|
140
|
+
get :list_developers, { :page => 6, :per_page => 1 }, :wp => { :inner_window => 1 }
|
141
|
+
assert_response :success
|
142
|
+
|
143
|
+
entries = assigns :developers
|
144
|
+
assert entries
|
145
|
+
assert_equal 1, entries.size
|
146
|
+
|
147
|
+
assert_select 'div.pagination', 1, 'no main DIV' do |pagination|
|
148
|
+
assert_select 'a[href]', 8 do |elements|
|
149
|
+
validate_page_numbers [5,1,2,5,7,10,11,7], elements
|
150
|
+
assert_select elements.first, 'a', "« Previous"
|
151
|
+
assert_select elements.last, 'a', "Next »"
|
152
|
+
end
|
153
|
+
assert_select 'span.current', entries.current_page.to_s
|
154
|
+
assert_equal '« Previous 1 2 ... 5 6 7 ... 10 11 Next »', pagination.first.inner_text
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_will_paginate_eliminates_small_gaps
|
159
|
+
get :list_developers, { :page => 6, :per_page => 1 }, :wp => { :inner_window => 2 }
|
160
|
+
assert_response :success
|
161
|
+
|
162
|
+
assert_select 'div.pagination', 1, 'no main DIV' do
|
163
|
+
assert_select 'a[href]', 12 do |elements|
|
164
|
+
validate_page_numbers [5,1,2,3,4,5,7,8,9,10,11,7], elements
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_no_pagination
|
170
|
+
get :list_developers, :per_page => 12
|
171
|
+
entries = assigns :developers
|
172
|
+
assert_equal 1, entries.page_count
|
173
|
+
assert_equal 11, entries.size
|
174
|
+
|
175
|
+
assert_equal '', @response.body
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_faulty_input_raises_error
|
179
|
+
assert_raise WillPaginate::InvalidPage do
|
180
|
+
get :list_developers, :page => 'foo'
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
uses_mocha 'helper internals' do
|
185
|
+
def test_collection_name_can_be_guessed
|
186
|
+
collection = mock
|
187
|
+
collection.expects(:page_count).returns(1)
|
188
|
+
get :guess_collection_name, {}, :wp => collection
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_inferred_collection_name_raises_error_when_nil
|
193
|
+
ex = assert_raise ArgumentError do
|
194
|
+
get :guess_collection_name, {}, :wp => nil
|
195
|
+
end
|
196
|
+
assert ex.message.include?('@developers')
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_setting_id_for_container
|
200
|
+
get :list_developers
|
201
|
+
assert_select 'div.pagination', 1 do |div|
|
202
|
+
assert_nil div.first['id']
|
203
|
+
end
|
204
|
+
# magic ID
|
205
|
+
get :list_developers, {}, :wp => { :id => true }
|
206
|
+
assert_select 'div.pagination', 1 do |div|
|
207
|
+
assert_equal 'fixnums_pagination', div.first['id']
|
208
|
+
end
|
209
|
+
# explicit ID
|
210
|
+
get :list_developers, {}, :wp => { :id => 'custom_id' }
|
211
|
+
assert_select 'div.pagination', 1 do |div|
|
212
|
+
assert_equal 'custom_id', div.first['id']
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
protected
|
217
|
+
|
218
|
+
def validate_page_numbers expected, links, param_name = :page
|
219
|
+
param_pattern = /\W#{param_name}=([^&]*)/
|
220
|
+
|
221
|
+
assert_equal(expected, links.map { |e|
|
222
|
+
e['href'] =~ param_pattern
|
223
|
+
$1 ? $1.to_i : $1
|
224
|
+
})
|
225
|
+
end
|
226
|
+
|
227
|
+
def assert_links_match pattern
|
228
|
+
assert_select 'div.pagination a[href]' do |elements|
|
229
|
+
elements.each do |el|
|
230
|
+
assert_match pattern, el['href']
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def assert_no_links_match pattern
|
236
|
+
assert_select 'div.pagination a[href]' do |elements|
|
237
|
+
elements.each do |el|
|
238
|
+
assert_no_match pattern, el['href']
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
class ViewHelpersTest < Test::Unit::TestCase
|
245
|
+
include WillPaginate::ViewHelpers
|
246
|
+
|
247
|
+
def test_page_entries_info
|
248
|
+
arr = ('a'..'z').to_a
|
249
|
+
collection = arr.paginate :page => 2, :per_page => 5
|
250
|
+
assert_equal %{Displaying entries <b>6 - 10</b> of <b>26</b> in total},
|
251
|
+
page_entries_info(collection)
|
252
|
+
|
253
|
+
collection = arr.paginate :page => 7, :per_page => 4
|
254
|
+
assert_equal %{Displaying entries <b>25 - 26</b> of <b>26</b> in total},
|
255
|
+
page_entries_info(collection)
|
256
|
+
end
|
257
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: will_paginate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Mislav Marohni\xC4\x87"
|
8
|
+
- PJ Hyett
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-03-02 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activesupport
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.4.4
|
24
|
+
version:
|
25
|
+
description: A Rails plugin that provides pagination solutions for querying models and rendering pagination links in views.
|
26
|
+
email: mislav.marohnic@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README
|
33
|
+
- LICENSE
|
34
|
+
files:
|
35
|
+
- CHANGELOG
|
36
|
+
- LICENSE
|
37
|
+
- Manifest.txt
|
38
|
+
- README
|
39
|
+
- Rakefile
|
40
|
+
- config/release.rb
|
41
|
+
- lib/will_paginate.rb
|
42
|
+
- lib/will_paginate/collection.rb
|
43
|
+
- lib/will_paginate/core_ext.rb
|
44
|
+
- lib/will_paginate/finder.rb
|
45
|
+
- lib/will_paginate/version.rb
|
46
|
+
- lib/will_paginate/view_helpers.rb
|
47
|
+
- setup.rb
|
48
|
+
- test/array_pagination_test.rb
|
49
|
+
- test/boot.rb
|
50
|
+
- test/console
|
51
|
+
- test/finder_test.rb
|
52
|
+
- test/fixtures/admin.rb
|
53
|
+
- test/fixtures/developer.rb
|
54
|
+
- test/fixtures/developers_projects.yml
|
55
|
+
- test/fixtures/project.rb
|
56
|
+
- test/fixtures/projects.yml
|
57
|
+
- test/fixtures/replies.yml
|
58
|
+
- test/fixtures/reply.rb
|
59
|
+
- test/fixtures/schema.rb
|
60
|
+
- test/fixtures/topic.rb
|
61
|
+
- test/fixtures/topics.yml
|
62
|
+
- test/fixtures/user.rb
|
63
|
+
- test/fixtures/users.yml
|
64
|
+
- test/helper.rb
|
65
|
+
- test/lib/activerecord_test_case.rb
|
66
|
+
- test/lib/activerecord_test_connector.rb
|
67
|
+
- test/lib/load_fixtures.rb
|
68
|
+
- test/pagination_test.rb
|
69
|
+
has_rdoc: true
|
70
|
+
homepage: http://github.com/mislav/will_paginate/wikis
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --main
|
74
|
+
- README
|
75
|
+
- --inline-source
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
version:
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project: will-paginate
|
94
|
+
rubygems_version: 1.0.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 2
|
97
|
+
summary: A Rails plugin that provides pagination solutions for querying models and rendering pagination links in views.
|
98
|
+
test_files: []
|
99
|
+
|