will_paginate 3.0.4 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +7 -10
- data/lib/will_paginate/active_record.rb +18 -17
- data/lib/will_paginate/deprecation.rb +3 -3
- data/lib/will_paginate/i18n.rb +3 -3
- data/lib/will_paginate/locale/en.yml +4 -0
- data/lib/will_paginate/mongoid.rb +48 -0
- data/lib/will_paginate/page_number.rb +7 -11
- data/lib/will_paginate/railtie.rb +19 -13
- data/lib/will_paginate/version.rb +2 -2
- data/lib/will_paginate/view_helpers/action_view.rb +12 -5
- data/lib/will_paginate/view_helpers/hanami.rb +41 -0
- data/lib/will_paginate/view_helpers/link_renderer.rb +23 -17
- data/lib/will_paginate/view_helpers/link_renderer_base.rb +1 -1
- data/lib/will_paginate/view_helpers.rb +3 -1
- data/lib/will_paginate.rb +0 -12
- metadata +29 -58
- data/Rakefile +0 -25
- data/lib/will_paginate/data_mapper.rb +0 -95
- data/lib/will_paginate/view_helpers/merb.rb +0 -26
- data/spec/ci.rb +0 -29
- data/spec/collection_spec.rb +0 -139
- data/spec/console +0 -12
- data/spec/console_fixtures.rb +0 -28
- data/spec/database.yml +0 -22
- data/spec/finders/active_record_spec.rb +0 -556
- data/spec/finders/activerecord_test_connector.rb +0 -115
- data/spec/finders/data_mapper_spec.rb +0 -103
- data/spec/finders/data_mapper_test_connector.rb +0 -54
- data/spec/finders/sequel_spec.rb +0 -67
- data/spec/finders/sequel_test_connector.rb +0 -15
- data/spec/fixtures/admin.rb +0 -3
- data/spec/fixtures/developer.rb +0 -13
- data/spec/fixtures/developers_projects.yml +0 -13
- data/spec/fixtures/project.rb +0 -15
- data/spec/fixtures/projects.yml +0 -6
- data/spec/fixtures/replies.yml +0 -29
- data/spec/fixtures/reply.rb +0 -9
- data/spec/fixtures/schema.rb +0 -38
- data/spec/fixtures/topic.rb +0 -7
- data/spec/fixtures/topics.yml +0 -30
- data/spec/fixtures/user.rb +0 -2
- data/spec/fixtures/users.yml +0 -35
- data/spec/page_number_spec.rb +0 -65
- data/spec/per_page_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -71
- data/spec/view_helpers/action_view_spec.rb +0 -423
- data/spec/view_helpers/base_spec.rb +0 -130
- data/spec/view_helpers/link_renderer_base_spec.rb +0 -87
- data/spec/view_helpers/view_example_group.rb +0 -121
@@ -1,556 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'will_paginate/active_record'
|
3
|
-
require File.expand_path('../activerecord_test_connector', __FILE__)
|
4
|
-
|
5
|
-
ActiverecordTestConnector.setup
|
6
|
-
abort unless ActiverecordTestConnector.able_to_connect
|
7
|
-
|
8
|
-
describe WillPaginate::ActiveRecord do
|
9
|
-
|
10
|
-
extend ActiverecordTestConnector::FixtureSetup
|
11
|
-
|
12
|
-
fixtures :topics, :replies, :users, :projects, :developers_projects
|
13
|
-
|
14
|
-
it "should integrate with ActiveRecord::Base" do
|
15
|
-
ActiveRecord::Base.should respond_to(:paginate)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should paginate" do
|
19
|
-
lambda {
|
20
|
-
users = User.paginate(:page => 1, :per_page => 5).to_a
|
21
|
-
users.length.should == 5
|
22
|
-
}.should run_queries(2)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should fail when encountering unknown params" do
|
26
|
-
lambda {
|
27
|
-
User.paginate :foo => 'bar', :page => 1, :per_page => 4
|
28
|
-
}.should raise_error(ArgumentError)
|
29
|
-
end
|
30
|
-
|
31
|
-
describe "relation" do
|
32
|
-
it "should return a relation" do
|
33
|
-
rel = nil
|
34
|
-
lambda {
|
35
|
-
rel = Developer.paginate(:page => 1)
|
36
|
-
rel.per_page.should == 10
|
37
|
-
rel.current_page.should == 1
|
38
|
-
}.should run_queries(0)
|
39
|
-
|
40
|
-
lambda {
|
41
|
-
rel.total_pages.should == 2
|
42
|
-
}.should run_queries(1)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should keep per-class per_page number" do
|
46
|
-
rel = Developer.order('id').paginate(:page => 1)
|
47
|
-
rel.per_page.should == 10
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should be able to change per_page number" do
|
51
|
-
rel = Developer.order('id').paginate(:page => 1).limit(5)
|
52
|
-
rel.per_page.should == 5
|
53
|
-
end
|
54
|
-
|
55
|
-
it "remembers pagination in sub-relations" do
|
56
|
-
rel = Topic.paginate(:page => 2, :per_page => 3)
|
57
|
-
lambda {
|
58
|
-
rel.total_entries.should == 4
|
59
|
-
}.should run_queries(1)
|
60
|
-
rel = rel.mentions_activerecord
|
61
|
-
rel.current_page.should == 2
|
62
|
-
rel.per_page.should == 3
|
63
|
-
lambda {
|
64
|
-
rel.total_entries.should == 1
|
65
|
-
}.should run_queries(1)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "supports the page() method" do
|
69
|
-
rel = Developer.page('1').order('id')
|
70
|
-
rel.current_page.should == 1
|
71
|
-
rel.per_page.should == 10
|
72
|
-
rel.offset.should == 0
|
73
|
-
|
74
|
-
rel = rel.limit(5).page(2)
|
75
|
-
rel.per_page.should == 5
|
76
|
-
rel.offset.should == 5
|
77
|
-
end
|
78
|
-
|
79
|
-
it "raises on invalid page number" do
|
80
|
-
lambda {
|
81
|
-
Developer.page('foo')
|
82
|
-
}.should raise_error(ArgumentError)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "supports first limit() then page()" do
|
86
|
-
rel = Developer.limit(3).page(3)
|
87
|
-
rel.offset.should == 6
|
88
|
-
end
|
89
|
-
|
90
|
-
it "supports first page() then limit()" do
|
91
|
-
rel = Developer.page(3).limit(3)
|
92
|
-
rel.offset.should == 6
|
93
|
-
end
|
94
|
-
|
95
|
-
it "supports #first" do
|
96
|
-
rel = Developer.order('id').page(2).per_page(4)
|
97
|
-
rel.first.should == users(:dev_5)
|
98
|
-
rel.first(2).should == users(:dev_5, :dev_6)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "supports #last" do
|
102
|
-
rel = Developer.order('id').page(2).per_page(4)
|
103
|
-
rel.last.should == users(:dev_8)
|
104
|
-
rel.last(2).should == users(:dev_7, :dev_8)
|
105
|
-
rel.page(3).last.should == users(:poor_jamis)
|
106
|
-
end
|
107
|
-
|
108
|
-
it "keeps pagination data after 'scoped'" do
|
109
|
-
rel = Developer.page(2).scoped
|
110
|
-
rel.per_page.should == 10
|
111
|
-
rel.offset.should == 10
|
112
|
-
rel.current_page.should == 2
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
describe "counting" do
|
117
|
-
it "should guess the total count" do
|
118
|
-
lambda {
|
119
|
-
topics = Topic.paginate :page => 2, :per_page => 3
|
120
|
-
topics.total_entries.should == 4
|
121
|
-
}.should run_queries(1)
|
122
|
-
end
|
123
|
-
|
124
|
-
it "should guess that there are no records" do
|
125
|
-
lambda {
|
126
|
-
topics = Topic.where(:project_id => 999).paginate :page => 1, :per_page => 3
|
127
|
-
topics.total_entries.should == 0
|
128
|
-
}.should run_queries(1)
|
129
|
-
end
|
130
|
-
|
131
|
-
it "forgets count in sub-relations" do
|
132
|
-
lambda {
|
133
|
-
topics = Topic.paginate :page => 1, :per_page => 3
|
134
|
-
topics.total_entries.should == 4
|
135
|
-
topics.where('1 = 1').total_entries.should == 4
|
136
|
-
}.should run_queries(2)
|
137
|
-
end
|
138
|
-
|
139
|
-
it "remembers custom count options in sub-relations" do
|
140
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :count => {:conditions => "title LIKE '%futurama%'"}
|
141
|
-
topics.total_entries.should == 1
|
142
|
-
topics.length.should == 3
|
143
|
-
lambda {
|
144
|
-
topics.order('id').total_entries.should == 1
|
145
|
-
}.should run_queries(1)
|
146
|
-
end
|
147
|
-
|
148
|
-
it "supports empty? method" do
|
149
|
-
topics = Topic.paginate :page => 1, :per_page => 3
|
150
|
-
lambda {
|
151
|
-
topics.should_not be_empty
|
152
|
-
}.should run_queries(1)
|
153
|
-
end
|
154
|
-
|
155
|
-
it "support empty? for grouped queries" do
|
156
|
-
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
157
|
-
lambda {
|
158
|
-
topics.should_not be_empty
|
159
|
-
}.should run_queries(1)
|
160
|
-
end
|
161
|
-
|
162
|
-
it "supports `size` for grouped queries" do
|
163
|
-
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
164
|
-
lambda {
|
165
|
-
topics.size.should == {nil=>2, 1=>2}
|
166
|
-
}.should run_queries(1)
|
167
|
-
end
|
168
|
-
|
169
|
-
it "overrides total_entries count with a fixed value" do
|
170
|
-
lambda {
|
171
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => 999
|
172
|
-
topics.total_entries.should == 999
|
173
|
-
# value is kept even in sub-relations
|
174
|
-
topics.where('1 = 1').total_entries.should == 999
|
175
|
-
}.should run_queries(0)
|
176
|
-
end
|
177
|
-
|
178
|
-
it "supports a non-int for total_entries" do
|
179
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => "999"
|
180
|
-
topics.total_entries.should == 999
|
181
|
-
end
|
182
|
-
|
183
|
-
it "removes :include for count" do
|
184
|
-
lambda {
|
185
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
186
|
-
developers.total_entries.should == 11
|
187
|
-
$query_sql.last.should_not =~ /\bJOIN\b/
|
188
|
-
}.should run_queries(1)
|
189
|
-
end
|
190
|
-
|
191
|
-
it "keeps :include for count when they are referenced in :conditions" do
|
192
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
193
|
-
with_condition = developers.where('projects.id > 1')
|
194
|
-
with_condition.total_entries.should == 1
|
195
|
-
|
196
|
-
$query_sql.last.should =~ /\bJOIN\b/
|
197
|
-
end
|
198
|
-
|
199
|
-
it "should count with group" do
|
200
|
-
Developer.group(:salary).page(1).total_entries.should == 4
|
201
|
-
end
|
202
|
-
|
203
|
-
it "should not have zero total_pages when the result set is empty" do
|
204
|
-
Developer.where("1 = 2").page(1).total_pages.should == 1
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
it "should not ignore :select parameter when it says DISTINCT" do
|
209
|
-
users = User.select('DISTINCT salary').paginate :page => 2
|
210
|
-
users.total_entries.should == 5
|
211
|
-
end
|
212
|
-
|
213
|
-
describe "paginate_by_sql" do
|
214
|
-
it "should respond" do
|
215
|
-
User.should respond_to(:paginate_by_sql)
|
216
|
-
end
|
217
|
-
|
218
|
-
it "should paginate" do
|
219
|
-
lambda {
|
220
|
-
sql = "select content from topics where content like '%futurama%'"
|
221
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 1
|
222
|
-
topics.total_entries.should == 1
|
223
|
-
topics.first.attributes.has_key?('title').should be_false
|
224
|
-
}.should run_queries(2)
|
225
|
-
end
|
226
|
-
|
227
|
-
it "should respect total_entries setting" do
|
228
|
-
lambda {
|
229
|
-
sql = "select content from topics"
|
230
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 1, :total_entries => 999
|
231
|
-
topics.total_entries.should == 999
|
232
|
-
}.should run_queries(1)
|
233
|
-
end
|
234
|
-
|
235
|
-
it "defaults to page 1" do
|
236
|
-
sql = "select content from topics"
|
237
|
-
topics = Topic.paginate_by_sql sql, :page => nil, :per_page => 1
|
238
|
-
topics.current_page.should == 1
|
239
|
-
topics.size.should == 1
|
240
|
-
end
|
241
|
-
|
242
|
-
it "should strip the order when counting" do
|
243
|
-
lambda {
|
244
|
-
sql = "select id, title, content from topics order by topics.title"
|
245
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
|
246
|
-
topics.first.should == topics(:ar)
|
247
|
-
}.should run_queries(2)
|
248
|
-
|
249
|
-
$query_sql.last.should include('COUNT')
|
250
|
-
$query_sql.last.should_not include('order by topics.title')
|
251
|
-
end
|
252
|
-
|
253
|
-
it "shouldn't change the original query string" do
|
254
|
-
query = 'select * from topics where 1 = 2'
|
255
|
-
original_query = query.dup
|
256
|
-
Topic.paginate_by_sql(query, :page => 1)
|
257
|
-
query.should == original_query
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
it "doesn't mangle options" do
|
262
|
-
options = { :page => 1 }
|
263
|
-
options.expects(:delete).never
|
264
|
-
options_before = options.dup
|
265
|
-
|
266
|
-
Topic.paginate(options)
|
267
|
-
options.should == options_before
|
268
|
-
end
|
269
|
-
|
270
|
-
it "should get first page of Topics with a single query" do
|
271
|
-
lambda {
|
272
|
-
result = Topic.paginate :page => nil
|
273
|
-
result.to_a # trigger loading of records
|
274
|
-
result.current_page.should == 1
|
275
|
-
result.total_pages.should == 1
|
276
|
-
result.size.should == 4
|
277
|
-
}.should run_queries(1)
|
278
|
-
end
|
279
|
-
|
280
|
-
it "should get second (inexistent) page of Topics, requiring 2 queries" do
|
281
|
-
lambda {
|
282
|
-
result = Topic.paginate :page => 2
|
283
|
-
result.total_pages.should == 1
|
284
|
-
result.should be_empty
|
285
|
-
}.should run_queries(2)
|
286
|
-
end
|
287
|
-
|
288
|
-
it "should paginate with :order" do
|
289
|
-
result = Topic.paginate :page => 1, :order => 'created_at DESC'
|
290
|
-
result.should == topics(:futurama, :harvey_birdman, :rails, :ar).reverse
|
291
|
-
result.total_pages.should == 1
|
292
|
-
end
|
293
|
-
|
294
|
-
it "should paginate with :conditions" do
|
295
|
-
result = Topic.paginate :page => 1, :order => 'id ASC',
|
296
|
-
:conditions => ["created_at > ?", 30.minutes.ago]
|
297
|
-
result.should == topics(:rails, :ar)
|
298
|
-
result.total_pages.should == 1
|
299
|
-
end
|
300
|
-
|
301
|
-
it "should paginate with :include and :conditions" do
|
302
|
-
result = Topic.paginate \
|
303
|
-
:page => 1,
|
304
|
-
:include => :replies,
|
305
|
-
:conditions => "replies.content LIKE 'Bird%' ",
|
306
|
-
:per_page => 10
|
307
|
-
|
308
|
-
expected = Topic.find :all,
|
309
|
-
:include => 'replies',
|
310
|
-
:conditions => "replies.content LIKE 'Bird%' ",
|
311
|
-
:limit => 10
|
312
|
-
|
313
|
-
result.should == expected
|
314
|
-
result.total_entries.should == 1
|
315
|
-
end
|
316
|
-
|
317
|
-
it "should paginate with :include and :order" do
|
318
|
-
result = nil
|
319
|
-
lambda {
|
320
|
-
result = Topic.paginate(:page => 1, :include => :replies, :per_page => 10,
|
321
|
-
:order => 'replies.created_at asc, topics.created_at asc').to_a
|
322
|
-
}.should run_queries(2)
|
323
|
-
|
324
|
-
expected = Topic.find :all,
|
325
|
-
:include => 'replies',
|
326
|
-
:order => 'replies.created_at asc, topics.created_at asc',
|
327
|
-
:limit => 10
|
328
|
-
|
329
|
-
result.should == expected
|
330
|
-
result.total_entries.should == 4
|
331
|
-
end
|
332
|
-
|
333
|
-
describe "associations" do
|
334
|
-
it "should paginate with include" do
|
335
|
-
project = projects(:active_record)
|
336
|
-
|
337
|
-
result = project.topics.paginate \
|
338
|
-
:page => 1,
|
339
|
-
:include => :replies,
|
340
|
-
:conditions => ["replies.content LIKE ?", 'Nice%'],
|
341
|
-
:per_page => 10
|
342
|
-
|
343
|
-
expected = Topic.find :all,
|
344
|
-
:include => 'replies',
|
345
|
-
:conditions => ["project_id = ? AND replies.content LIKE ?", project.id, 'Nice%'],
|
346
|
-
:limit => 10
|
347
|
-
|
348
|
-
result.should == expected
|
349
|
-
end
|
350
|
-
|
351
|
-
it "should paginate" do
|
352
|
-
dhh = users(:david)
|
353
|
-
expected_name_ordered = projects(:action_controller, :active_record)
|
354
|
-
expected_id_ordered = projects(:active_record, :action_controller)
|
355
|
-
|
356
|
-
lambda {
|
357
|
-
# with association-specified order
|
358
|
-
result = ignore_deprecation { dhh.projects.paginate(:page => 1) }
|
359
|
-
result.should == expected_name_ordered
|
360
|
-
result.total_entries.should == 2
|
361
|
-
}.should run_queries(2)
|
362
|
-
|
363
|
-
# with explicit order
|
364
|
-
result = dhh.projects.paginate(:page => 1).reorder('projects.id')
|
365
|
-
result.should == expected_id_ordered
|
366
|
-
result.total_entries.should == 2
|
367
|
-
|
368
|
-
lambda {
|
369
|
-
dhh.projects.find(:all, :order => 'projects.id', :limit => 4)
|
370
|
-
}.should_not raise_error
|
371
|
-
|
372
|
-
result = dhh.projects.paginate(:page => 1, :per_page => 4).reorder('projects.id')
|
373
|
-
result.should == expected_id_ordered
|
374
|
-
|
375
|
-
# has_many with implicit order
|
376
|
-
topic = Topic.find(1)
|
377
|
-
expected = replies(:spam, :witty_retort)
|
378
|
-
# FIXME: wow, this is ugly
|
379
|
-
topic.replies.paginate(:page => 1).map(&:id).sort.should == expected.map(&:id).sort
|
380
|
-
topic.replies.paginate(:page => 1).reorder('replies.id ASC').should == expected.reverse
|
381
|
-
end
|
382
|
-
|
383
|
-
it "should paginate through association extension" do
|
384
|
-
project = Project.order('id').first
|
385
|
-
expected = [replies(:brave)]
|
386
|
-
|
387
|
-
lambda {
|
388
|
-
result = project.replies.only_recent.paginate(:page => 1)
|
389
|
-
result.should == expected
|
390
|
-
}.should run_queries(1)
|
391
|
-
end
|
392
|
-
end
|
393
|
-
|
394
|
-
it "should paginate with joins" do
|
395
|
-
result = nil
|
396
|
-
join_sql = 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id'
|
397
|
-
|
398
|
-
lambda {
|
399
|
-
result = Developer.paginate(:page => 1, :joins => join_sql, :conditions => 'project_id = 1')
|
400
|
-
result.to_a # trigger loading of records
|
401
|
-
result.size.should == 2
|
402
|
-
developer_names = result.map(&:name)
|
403
|
-
developer_names.should include('David')
|
404
|
-
developer_names.should include('Jamis')
|
405
|
-
}.should run_queries(1)
|
406
|
-
|
407
|
-
lambda {
|
408
|
-
expected = result.to_a
|
409
|
-
result = Developer.paginate(:page => 1, :joins => join_sql,
|
410
|
-
:conditions => 'project_id = 1', :count => { :select => "users.id" }).to_a
|
411
|
-
result.should == expected
|
412
|
-
result.total_entries.should == 2
|
413
|
-
}.should run_queries(1)
|
414
|
-
end
|
415
|
-
|
416
|
-
it "should paginate with group" do
|
417
|
-
result = nil
|
418
|
-
lambda {
|
419
|
-
result = Developer.paginate(:page => 1, :per_page => 10,
|
420
|
-
:group => 'salary', :select => 'salary', :order => 'salary').to_a
|
421
|
-
}.should run_queries(1)
|
422
|
-
|
423
|
-
expected = users(:david, :jamis, :dev_10, :poor_jamis).map(&:salary).sort
|
424
|
-
result.map(&:salary).should == expected
|
425
|
-
end
|
426
|
-
|
427
|
-
it "should not paginate with dynamic finder" do
|
428
|
-
lambda {
|
429
|
-
Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
|
430
|
-
}.should raise_error(NoMethodError)
|
431
|
-
end
|
432
|
-
|
433
|
-
it "should paginate with_scope" do
|
434
|
-
result = Developer.with_poor_ones { Developer.paginate :page => 1 }
|
435
|
-
result.size.should == 2
|
436
|
-
result.total_entries.should == 2
|
437
|
-
end
|
438
|
-
|
439
|
-
describe "scopes" do
|
440
|
-
it "should paginate" do
|
441
|
-
result = Developer.poor.paginate :page => 1, :per_page => 1
|
442
|
-
result.size.should == 1
|
443
|
-
result.total_entries.should == 2
|
444
|
-
end
|
445
|
-
|
446
|
-
it "should paginate on habtm association" do
|
447
|
-
project = projects(:active_record)
|
448
|
-
lambda {
|
449
|
-
result = ignore_deprecation { project.developers.poor.paginate :page => 1, :per_page => 1 }
|
450
|
-
result.size.should == 1
|
451
|
-
result.total_entries.should == 1
|
452
|
-
}.should run_queries(2)
|
453
|
-
end
|
454
|
-
|
455
|
-
it "should paginate on hmt association" do
|
456
|
-
project = projects(:active_record)
|
457
|
-
expected = [replies(:brave)]
|
458
|
-
|
459
|
-
lambda {
|
460
|
-
result = project.replies.recent.paginate :page => 1, :per_page => 1
|
461
|
-
result.should == expected
|
462
|
-
result.total_entries.should == 1
|
463
|
-
}.should run_queries(2)
|
464
|
-
end
|
465
|
-
|
466
|
-
it "should paginate on has_many association" do
|
467
|
-
project = projects(:active_record)
|
468
|
-
expected = [topics(:ar)]
|
469
|
-
|
470
|
-
lambda {
|
471
|
-
result = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
|
472
|
-
result.should == expected
|
473
|
-
result.total_entries.should == 1
|
474
|
-
}.should run_queries(2)
|
475
|
-
end
|
476
|
-
end
|
477
|
-
|
478
|
-
it "should paginate with :readonly option" do
|
479
|
-
lambda {
|
480
|
-
Developer.paginate :readonly => true, :page => 1
|
481
|
-
}.should_not raise_error
|
482
|
-
end
|
483
|
-
|
484
|
-
it "should not paginate an array of IDs" do
|
485
|
-
lambda {
|
486
|
-
Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
|
487
|
-
}.should raise_error(ArgumentError)
|
488
|
-
end
|
489
|
-
|
490
|
-
it "errors out for invalid values" do |variable|
|
491
|
-
lambda {
|
492
|
-
# page that results in an offset larger than BIGINT
|
493
|
-
Project.page(307445734561825862)
|
494
|
-
}.should raise_error(WillPaginate::InvalidPage, "invalid offset: 9223372036854775830")
|
495
|
-
end
|
496
|
-
|
497
|
-
protected
|
498
|
-
|
499
|
-
def ignore_deprecation
|
500
|
-
ActiveSupport::Deprecation.silence { yield }
|
501
|
-
end
|
502
|
-
|
503
|
-
def run_queries(num)
|
504
|
-
QueryCountMatcher.new(num)
|
505
|
-
end
|
506
|
-
|
507
|
-
def show_queries(&block)
|
508
|
-
counter = QueryCountMatcher.new(nil)
|
509
|
-
counter.run block
|
510
|
-
ensure
|
511
|
-
queries = counter.performed_queries
|
512
|
-
if queries.any?
|
513
|
-
puts queries
|
514
|
-
else
|
515
|
-
puts "no queries"
|
516
|
-
end
|
517
|
-
end
|
518
|
-
|
519
|
-
end
|
520
|
-
|
521
|
-
class QueryCountMatcher
|
522
|
-
def initialize(num)
|
523
|
-
@expected_count = num
|
524
|
-
end
|
525
|
-
|
526
|
-
def matches?(block)
|
527
|
-
run(block)
|
528
|
-
|
529
|
-
if @expected_count.respond_to? :include?
|
530
|
-
@expected_count.include? @count
|
531
|
-
else
|
532
|
-
@count == @expected_count
|
533
|
-
end
|
534
|
-
end
|
535
|
-
|
536
|
-
def run(block)
|
537
|
-
$query_count = 0
|
538
|
-
$query_sql = []
|
539
|
-
block.call
|
540
|
-
ensure
|
541
|
-
@queries = $query_sql.dup
|
542
|
-
@count = $query_count
|
543
|
-
end
|
544
|
-
|
545
|
-
def performed_queries
|
546
|
-
@queries
|
547
|
-
end
|
548
|
-
|
549
|
-
def failure_message
|
550
|
-
"expected #{@expected_count} queries, got #{@count}\n#{@queries.join("\n")}"
|
551
|
-
end
|
552
|
-
|
553
|
-
def negative_failure_message
|
554
|
-
"expected query count not to be #{@expected_count}"
|
555
|
-
end
|
556
|
-
end
|
@@ -1,115 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'active_record/fixtures'
|
3
|
-
require 'active_support/multibyte' # needed for Ruby 1.9.1
|
4
|
-
|
5
|
-
$query_count = 0
|
6
|
-
$query_sql = []
|
7
|
-
|
8
|
-
ignore_sql = /
|
9
|
-
^(
|
10
|
-
PRAGMA | SHOW\ max_identifier_length |
|
11
|
-
SELECT\ (currval|CAST|@@IDENTITY|@@ROWCOUNT) |
|
12
|
-
SHOW\ (FIELDS|TABLES)
|
13
|
-
)\b |
|
14
|
-
\bFROM\ (sqlite_master|pg_tables|pg_attribute)\b
|
15
|
-
/x
|
16
|
-
|
17
|
-
ActiveSupport::Notifications.subscribe(/^sql\./) do |*args|
|
18
|
-
payload = args.last
|
19
|
-
unless payload[:name] =~ /^Fixture/ or payload[:sql] =~ ignore_sql
|
20
|
-
$query_count += 1
|
21
|
-
$query_sql << payload[:sql]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
module ActiverecordTestConnector
|
26
|
-
extend self
|
27
|
-
|
28
|
-
attr_accessor :able_to_connect
|
29
|
-
attr_accessor :connected
|
30
|
-
|
31
|
-
FIXTURES_PATH = File.expand_path('../../fixtures', __FILE__)
|
32
|
-
|
33
|
-
Fixtures = defined?(ActiveRecord::FixtureSet) ? ActiveRecord::FixtureSet :
|
34
|
-
defined?(ActiveRecord::Fixtures) ? ActiveRecord::Fixtures :
|
35
|
-
::Fixtures
|
36
|
-
|
37
|
-
# Set our defaults
|
38
|
-
self.connected = false
|
39
|
-
self.able_to_connect = true
|
40
|
-
|
41
|
-
def setup
|
42
|
-
unless self.connected || !self.able_to_connect
|
43
|
-
setup_connection
|
44
|
-
load_schema
|
45
|
-
add_load_path FIXTURES_PATH
|
46
|
-
self.connected = true
|
47
|
-
end
|
48
|
-
rescue Exception => e # errors from ActiveRecord setup
|
49
|
-
$stderr.puts "\nSkipping ActiveRecord tests: #{e}\n\n"
|
50
|
-
self.able_to_connect = false
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def add_load_path(path)
|
56
|
-
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
|
57
|
-
dep.autoload_paths.unshift path
|
58
|
-
end
|
59
|
-
|
60
|
-
def setup_connection
|
61
|
-
db = ENV['DB'].blank?? 'sqlite3' : ENV['DB']
|
62
|
-
|
63
|
-
configurations = YAML.load_file(File.expand_path('../../database.yml', __FILE__))
|
64
|
-
raise "no configuration for '#{db}'" unless configurations.key? db
|
65
|
-
configuration = configurations[db]
|
66
|
-
|
67
|
-
# ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
|
68
|
-
puts "using #{configuration['adapter']} adapter"
|
69
|
-
|
70
|
-
ActiveRecord::Base.configurations = { db => configuration }
|
71
|
-
ActiveRecord::Base.establish_connection(db)
|
72
|
-
ActiveRecord::Base.default_timezone = :utc
|
73
|
-
end
|
74
|
-
|
75
|
-
def load_schema
|
76
|
-
ActiveRecord::Base.silence do
|
77
|
-
ActiveRecord::Migration.verbose = false
|
78
|
-
load File.join(FIXTURES_PATH, 'schema.rb')
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
module FixtureSetup
|
83
|
-
def fixtures(*tables)
|
84
|
-
table_names = tables.map { |t| t.to_s }
|
85
|
-
|
86
|
-
fixtures = Fixtures.create_fixtures ActiverecordTestConnector::FIXTURES_PATH, table_names
|
87
|
-
@@loaded_fixtures = {}
|
88
|
-
@@fixture_cache = {}
|
89
|
-
|
90
|
-
unless fixtures.nil?
|
91
|
-
if fixtures.instance_of?(Fixtures)
|
92
|
-
@@loaded_fixtures[fixtures.table_name] = fixtures
|
93
|
-
else
|
94
|
-
fixtures.each { |f| @@loaded_fixtures[f.table_name] = f }
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
table_names.each do |table_name|
|
99
|
-
define_method(table_name) do |*fixtures|
|
100
|
-
@@fixture_cache[table_name] ||= {}
|
101
|
-
|
102
|
-
instances = fixtures.map do |fixture|
|
103
|
-
if @@loaded_fixtures[table_name][fixture.to_s]
|
104
|
-
@@fixture_cache[table_name][fixture] ||= @@loaded_fixtures[table_name][fixture.to_s].find
|
105
|
-
else
|
106
|
-
raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
instances.size == 1 ? instances.first : instances
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|