will_paginate 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of will_paginate might be problematic. Click here for more details.

@@ -1,257 +0,0 @@
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 &raquo;"
57
- end
58
- assert_select 'span', 2
59
- assert_select 'span.disabled:first-child', "&laquo; Previous"
60
- assert_select 'span.current', entries.current_page.to_s
61
- assert_equal '&laquo; Previous 1 2 3 Next &raquo;', 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', "&laquo; Previous"
151
- assert_select elements.last, 'a', "Next &raquo;"
152
- end
153
- assert_select 'span.current', entries.current_page.to_s
154
- assert_equal '&laquo; Previous 1 2 ... 5 6 7 ... 10 11 Next &raquo;', 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&nbsp;-&nbsp;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&nbsp;-&nbsp;26</b> of <b>26</b> in total},
255
- page_entries_info(collection)
256
- end
257
- end