will_paginate 3.2.0 → 4.0.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.
- checksums.yaml +4 -4
- data/README.md +3 -5
- data/lib/will_paginate/active_record.rb +3 -3
- data/lib/will_paginate/i18n.rb +3 -3
- data/lib/will_paginate/locale/en.yml +2 -0
- data/lib/will_paginate/page_number.rb +1 -2
- data/lib/will_paginate/railtie.rb +2 -2
- data/lib/will_paginate/version.rb +2 -2
- data/lib/will_paginate/view_helpers/action_view.rb +7 -3
- data/lib/will_paginate/view_helpers/link_renderer.rb +7 -5
- data/lib/will_paginate.rb +0 -12
- metadata +12 -49
- data/lib/will_paginate/data_mapper.rb +0 -100
- data/lib/will_paginate/view_helpers/merb.rb +0 -26
- data/spec/collection_spec.rb +0 -139
- data/spec/console +0 -12
- data/spec/console_fixtures.rb +0 -28
- data/spec/database.yml +0 -29
- data/spec/fake_rubygems.rb +0 -18
- data/spec/finders/active_record_spec.rb +0 -417
- data/spec/finders/activerecord_test_connector.rb +0 -140
- data/spec/finders/data_mapper_spec.rb +0 -114
- data/spec/finders/data_mapper_test_connector.rb +0 -54
- data/spec/finders/mongoid_spec.rb +0 -145
- data/spec/finders/sequel_spec.rb +0 -65
- data/spec/finders/sequel_test_connector.rb +0 -15
- data/spec/fixtures/admin.rb +0 -3
- data/spec/fixtures/developer.rb +0 -9
- data/spec/fixtures/developers_projects.yml +0 -13
- data/spec/fixtures/project.rb +0 -13
- data/spec/fixtures/projects.yml +0 -6
- data/spec/fixtures/replies.yml +0 -29
- data/spec/fixtures/reply.rb +0 -8
- data/spec/fixtures/schema.rb +0 -38
- data/spec/fixtures/topic.rb +0 -8
- data/spec/fixtures/topics.yml +0 -30
- data/spec/fixtures/user.rb +0 -2
- data/spec/fixtures/users.yml +0 -35
- data/spec/matchers/deprecation_matcher.rb +0 -27
- data/spec/matchers/phrase_matcher.rb +0 -19
- data/spec/matchers/query_count_matcher.rb +0 -36
- data/spec/page_number_spec.rb +0 -82
- data/spec/per_page_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -46
- data/spec/view_helpers/action_view_spec.rb +0 -480
- data/spec/view_helpers/base_spec.rb +0 -143
- data/spec/view_helpers/link_renderer_base_spec.rb +0 -87
- data/spec/view_helpers/view_example_group.rb +0 -146
@@ -1,417 +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
|
-
|
7
|
-
describe WillPaginate::ActiveRecord do
|
8
|
-
|
9
|
-
extend ActiverecordTestConnector::FixtureSetup
|
10
|
-
|
11
|
-
fixtures :topics, :replies, :users, :projects, :developers_projects
|
12
|
-
|
13
|
-
it "should integrate with ActiveRecord::Base" do
|
14
|
-
ActiveRecord::Base.should respond_to(:paginate)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should paginate" do
|
18
|
-
lambda {
|
19
|
-
users = User.paginate(:page => 1, :per_page => 5).to_a
|
20
|
-
users.length.should == 5
|
21
|
-
}.should run_queries(2)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should fail when encountering unknown params" do
|
25
|
-
lambda {
|
26
|
-
User.paginate :foo => 'bar', :page => 1, :per_page => 4
|
27
|
-
}.should raise_error(ArgumentError)
|
28
|
-
end
|
29
|
-
|
30
|
-
describe "relation" do
|
31
|
-
it "should return a relation" do
|
32
|
-
rel = nil
|
33
|
-
lambda {
|
34
|
-
rel = Developer.paginate(:page => 1)
|
35
|
-
rel.per_page.should == 10
|
36
|
-
rel.current_page.should == 1
|
37
|
-
}.should run_queries(0)
|
38
|
-
|
39
|
-
lambda {
|
40
|
-
rel.total_pages.should == 2
|
41
|
-
}.should run_queries(1)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should keep per-class per_page number" do
|
45
|
-
rel = Developer.order('id').paginate(:page => 1)
|
46
|
-
rel.per_page.should == 10
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should be able to change per_page number" do
|
50
|
-
rel = Developer.order('id').paginate(:page => 1).limit(5)
|
51
|
-
rel.per_page.should == 5
|
52
|
-
end
|
53
|
-
|
54
|
-
it "remembers pagination in sub-relations" do
|
55
|
-
rel = Topic.paginate(:page => 2, :per_page => 3)
|
56
|
-
lambda {
|
57
|
-
rel.total_entries.should == 4
|
58
|
-
}.should run_queries(1)
|
59
|
-
rel = rel.mentions_activerecord
|
60
|
-
rel.current_page.should == 2
|
61
|
-
rel.per_page.should == 3
|
62
|
-
lambda {
|
63
|
-
rel.total_entries.should == 1
|
64
|
-
}.should run_queries(1)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "supports the page() method" do
|
68
|
-
rel = Developer.page('1').order('id')
|
69
|
-
rel.current_page.should == 1
|
70
|
-
rel.per_page.should == 10
|
71
|
-
rel.offset.should == 0
|
72
|
-
|
73
|
-
rel = rel.limit(5).page(2)
|
74
|
-
rel.per_page.should == 5
|
75
|
-
rel.offset.should == 5
|
76
|
-
end
|
77
|
-
|
78
|
-
it "raises on invalid page number" do
|
79
|
-
lambda {
|
80
|
-
Developer.page('foo')
|
81
|
-
}.should raise_error(ArgumentError)
|
82
|
-
end
|
83
|
-
|
84
|
-
it "supports first limit() then page()" do
|
85
|
-
rel = Developer.limit(3).page(3)
|
86
|
-
rel.offset.should == 6
|
87
|
-
end
|
88
|
-
|
89
|
-
it "supports first page() then limit()" do
|
90
|
-
rel = Developer.page(3).limit(3)
|
91
|
-
rel.offset.should == 6
|
92
|
-
end
|
93
|
-
|
94
|
-
it "supports #first" do
|
95
|
-
rel = Developer.order('id').page(2).per_page(4)
|
96
|
-
rel.first.should == users(:dev_5)
|
97
|
-
rel.first(2).should == users(:dev_5, :dev_6)
|
98
|
-
end
|
99
|
-
|
100
|
-
it "supports #last" do
|
101
|
-
rel = Developer.order('id').page(2).per_page(4)
|
102
|
-
rel.last.should == users(:dev_8)
|
103
|
-
rel.last(2).should == users(:dev_7, :dev_8)
|
104
|
-
rel.page(3).last.should == users(:poor_jamis)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
describe "counting" do
|
109
|
-
it "should guess the total count" do
|
110
|
-
lambda {
|
111
|
-
topics = Topic.paginate :page => 2, :per_page => 3
|
112
|
-
topics.total_entries.should == 4
|
113
|
-
}.should run_queries(1)
|
114
|
-
end
|
115
|
-
|
116
|
-
it "should guess that there are no records" do
|
117
|
-
lambda {
|
118
|
-
topics = Topic.where(:project_id => 999).paginate :page => 1, :per_page => 3
|
119
|
-
topics.total_entries.should == 0
|
120
|
-
}.should run_queries(1)
|
121
|
-
end
|
122
|
-
|
123
|
-
it "forgets count in sub-relations" do
|
124
|
-
lambda {
|
125
|
-
topics = Topic.paginate :page => 1, :per_page => 3
|
126
|
-
topics.total_entries.should == 4
|
127
|
-
topics.where('1 = 1').total_entries.should == 4
|
128
|
-
}.should run_queries(2)
|
129
|
-
end
|
130
|
-
|
131
|
-
it "supports empty? method" do
|
132
|
-
topics = Topic.paginate :page => 1, :per_page => 3
|
133
|
-
lambda {
|
134
|
-
topics.should_not be_empty
|
135
|
-
}.should run_queries(1)
|
136
|
-
end
|
137
|
-
|
138
|
-
it "support empty? for grouped queries" do
|
139
|
-
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
140
|
-
lambda {
|
141
|
-
topics.should_not be_empty
|
142
|
-
}.should run_queries(1)
|
143
|
-
end
|
144
|
-
|
145
|
-
it "supports `size` for grouped queries" do
|
146
|
-
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
147
|
-
lambda {
|
148
|
-
topics.size.should == {nil=>2, 1=>2}
|
149
|
-
}.should run_queries(1)
|
150
|
-
end
|
151
|
-
|
152
|
-
it "overrides total_entries count with a fixed value" do
|
153
|
-
lambda {
|
154
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => 999
|
155
|
-
topics.total_entries.should == 999
|
156
|
-
# value is kept even in sub-relations
|
157
|
-
topics.where('1 = 1').total_entries.should == 999
|
158
|
-
}.should run_queries(0)
|
159
|
-
end
|
160
|
-
|
161
|
-
it "supports a non-int for total_entries" do
|
162
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => "999"
|
163
|
-
topics.total_entries.should == 999
|
164
|
-
end
|
165
|
-
|
166
|
-
it "removes :include for count" do
|
167
|
-
lambda {
|
168
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
169
|
-
developers.total_entries.should == 11
|
170
|
-
$query_sql.last.should_not =~ /\bJOIN\b/
|
171
|
-
}.should run_queries(1)
|
172
|
-
end
|
173
|
-
|
174
|
-
it "keeps :include for count when they are referenced in :conditions" do
|
175
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
176
|
-
with_condition = developers.where('projects.id > 1')
|
177
|
-
with_condition = with_condition.references(:projects) if with_condition.respond_to?(:references)
|
178
|
-
with_condition.total_entries.should == 1
|
179
|
-
|
180
|
-
$query_sql.last.should =~ /\bJOIN\b/
|
181
|
-
end
|
182
|
-
|
183
|
-
it "should count with group" do
|
184
|
-
Developer.group(:salary).page(1).total_entries.should == 4
|
185
|
-
end
|
186
|
-
|
187
|
-
it "should count with select" do
|
188
|
-
Topic.select('title, content').page(1).total_entries.should == 4
|
189
|
-
end
|
190
|
-
|
191
|
-
it "removes :reorder for count with group" do
|
192
|
-
Project.group(:id).reorder(:id).page(1).total_entries
|
193
|
-
$query_sql.last.should_not =~ /\ORDER\b/
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should not have zero total_pages when the result set is empty" do
|
197
|
-
Developer.where("1 = 2").page(1).total_pages.should == 1
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
it "should not ignore :select parameter when it says DISTINCT" do
|
202
|
-
users = User.select('DISTINCT salary').paginate :page => 2
|
203
|
-
users.total_entries.should == 5
|
204
|
-
end
|
205
|
-
|
206
|
-
describe "paginate_by_sql" do
|
207
|
-
it "should respond" do
|
208
|
-
User.should respond_to(:paginate_by_sql)
|
209
|
-
end
|
210
|
-
|
211
|
-
it "should paginate" do
|
212
|
-
lambda {
|
213
|
-
sql = "select content from topics where content like '%futurama%'"
|
214
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 1
|
215
|
-
topics.total_entries.should == 1
|
216
|
-
topics.first.attributes.has_key?('title').should be_false
|
217
|
-
}.should run_queries(2)
|
218
|
-
end
|
219
|
-
|
220
|
-
it "should respect total_entries setting" do
|
221
|
-
lambda {
|
222
|
-
sql = "select content from topics"
|
223
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 1, :total_entries => 999
|
224
|
-
topics.total_entries.should == 999
|
225
|
-
}.should run_queries(1)
|
226
|
-
end
|
227
|
-
|
228
|
-
it "defaults to page 1" do
|
229
|
-
sql = "select content from topics"
|
230
|
-
topics = Topic.paginate_by_sql sql, :page => nil, :per_page => 1
|
231
|
-
topics.current_page.should == 1
|
232
|
-
topics.size.should == 1
|
233
|
-
end
|
234
|
-
|
235
|
-
it "should strip the order when counting" do
|
236
|
-
expected = topics(:ar)
|
237
|
-
lambda {
|
238
|
-
sql = "select id, title, content from topics order by topics.title"
|
239
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
|
240
|
-
topics.first.should == expected
|
241
|
-
}.should run_queries(2)
|
242
|
-
|
243
|
-
$query_sql.last.should include('COUNT')
|
244
|
-
$query_sql.last.should_not include('order by topics.title')
|
245
|
-
end
|
246
|
-
|
247
|
-
it "shouldn't change the original query string" do
|
248
|
-
query = 'select * from topics where 1 = 2'
|
249
|
-
original_query = query.dup
|
250
|
-
Topic.paginate_by_sql(query, :page => 1)
|
251
|
-
query.should == original_query
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
it "doesn't mangle options" do
|
256
|
-
options = { :page => 1 }
|
257
|
-
options.expects(:delete).never
|
258
|
-
options_before = options.dup
|
259
|
-
|
260
|
-
Topic.paginate(options)
|
261
|
-
options.should == options_before
|
262
|
-
end
|
263
|
-
|
264
|
-
it "should get first page of Topics with a single query" do
|
265
|
-
lambda {
|
266
|
-
result = Topic.paginate :page => nil
|
267
|
-
result.to_a # trigger loading of records
|
268
|
-
result.current_page.should == 1
|
269
|
-
result.total_pages.should == 1
|
270
|
-
result.size.should == 4
|
271
|
-
}.should run_queries(1)
|
272
|
-
end
|
273
|
-
|
274
|
-
it "should get second (inexistent) page of Topics, requiring 2 queries" do
|
275
|
-
lambda {
|
276
|
-
result = Topic.paginate :page => 2
|
277
|
-
result.total_pages.should == 1
|
278
|
-
result.should be_empty
|
279
|
-
}.should run_queries(2)
|
280
|
-
end
|
281
|
-
|
282
|
-
describe "associations" do
|
283
|
-
it "should paginate" do
|
284
|
-
dhh = users(:david)
|
285
|
-
expected_name_ordered = projects(:action_controller, :active_record)
|
286
|
-
expected_id_ordered = projects(:active_record, :action_controller)
|
287
|
-
|
288
|
-
lambda {
|
289
|
-
# with association-specified order
|
290
|
-
result = ignore_deprecation {
|
291
|
-
dhh.projects.includes(:topics).order('projects.name').paginate(:page => 1)
|
292
|
-
}
|
293
|
-
result.to_a.should == expected_name_ordered
|
294
|
-
result.total_entries.should == 2
|
295
|
-
}.should run_queries(2)
|
296
|
-
|
297
|
-
# with explicit order
|
298
|
-
result = dhh.projects.paginate(:page => 1).reorder('projects.id')
|
299
|
-
result.should == expected_id_ordered
|
300
|
-
result.total_entries.should == 2
|
301
|
-
|
302
|
-
lambda {
|
303
|
-
dhh.projects.order('projects.id').limit(4).to_a
|
304
|
-
}.should_not raise_error
|
305
|
-
|
306
|
-
result = dhh.projects.paginate(:page => 1, :per_page => 4).reorder('projects.id')
|
307
|
-
result.should == expected_id_ordered
|
308
|
-
|
309
|
-
# has_many with implicit order
|
310
|
-
topic = Topic.find(1)
|
311
|
-
expected = replies(:spam, :witty_retort)
|
312
|
-
# FIXME: wow, this is ugly
|
313
|
-
topic.replies.paginate(:page => 1).map(&:id).sort.should == expected.map(&:id).sort
|
314
|
-
topic.replies.paginate(:page => 1).reorder('replies.id ASC').should == expected.reverse
|
315
|
-
end
|
316
|
-
|
317
|
-
it "should paginate through association extension" do
|
318
|
-
project = Project.order('id').first
|
319
|
-
expected = [replies(:brave)]
|
320
|
-
|
321
|
-
lambda {
|
322
|
-
result = project.replies.only_recent.paginate(:page => 1)
|
323
|
-
result.should == expected
|
324
|
-
}.should run_queries(1)
|
325
|
-
end
|
326
|
-
end
|
327
|
-
|
328
|
-
it "should paginate with joins" do
|
329
|
-
result = nil
|
330
|
-
join_sql = 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id'
|
331
|
-
|
332
|
-
lambda {
|
333
|
-
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
334
|
-
result.to_a # trigger loading of records
|
335
|
-
result.size.should == 2
|
336
|
-
developer_names = result.map(&:name)
|
337
|
-
developer_names.should include('David')
|
338
|
-
developer_names.should include('Jamis')
|
339
|
-
}.should run_queries(1)
|
340
|
-
|
341
|
-
lambda {
|
342
|
-
expected = result.to_a
|
343
|
-
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
344
|
-
result.should == expected
|
345
|
-
result.total_entries.should == 2
|
346
|
-
}.should run_queries(1)
|
347
|
-
end
|
348
|
-
|
349
|
-
it "should paginate with group" do
|
350
|
-
result = nil
|
351
|
-
lambda {
|
352
|
-
result = Developer.select('salary').order('salary').group('salary').
|
353
|
-
paginate(:page => 1, :per_page => 10).to_a
|
354
|
-
}.should run_queries(1)
|
355
|
-
|
356
|
-
expected = users(:david, :jamis, :dev_10, :poor_jamis).map(&:salary).sort
|
357
|
-
result.map(&:salary).should == expected
|
358
|
-
end
|
359
|
-
|
360
|
-
it "should not paginate with dynamic finder" do
|
361
|
-
lambda {
|
362
|
-
Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
|
363
|
-
}.should raise_error(NoMethodError)
|
364
|
-
end
|
365
|
-
|
366
|
-
describe "scopes" do
|
367
|
-
it "should paginate" do
|
368
|
-
result = Developer.poor.paginate :page => 1, :per_page => 1
|
369
|
-
result.size.should == 1
|
370
|
-
result.total_entries.should == 2
|
371
|
-
end
|
372
|
-
|
373
|
-
it "should paginate on habtm association" do
|
374
|
-
project = projects(:active_record)
|
375
|
-
lambda {
|
376
|
-
result = ignore_deprecation { project.developers.poor.paginate :page => 1, :per_page => 1 }
|
377
|
-
result.size.should == 1
|
378
|
-
result.total_entries.should == 1
|
379
|
-
}.should run_queries(2)
|
380
|
-
end
|
381
|
-
|
382
|
-
it "should paginate on hmt association" do
|
383
|
-
project = projects(:active_record)
|
384
|
-
expected = [replies(:brave)]
|
385
|
-
|
386
|
-
lambda {
|
387
|
-
result = project.replies.recent.paginate :page => 1, :per_page => 1
|
388
|
-
result.should == expected
|
389
|
-
result.total_entries.should == 1
|
390
|
-
}.should run_queries(2)
|
391
|
-
end
|
392
|
-
|
393
|
-
it "should paginate on has_many association" do
|
394
|
-
project = projects(:active_record)
|
395
|
-
expected = [topics(:ar)]
|
396
|
-
|
397
|
-
lambda {
|
398
|
-
result = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
|
399
|
-
result.should == expected
|
400
|
-
result.total_entries.should == 1
|
401
|
-
}.should run_queries(2)
|
402
|
-
end
|
403
|
-
end
|
404
|
-
|
405
|
-
it "should not paginate an array of IDs" do
|
406
|
-
lambda {
|
407
|
-
Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
|
408
|
-
}.should raise_error(ArgumentError)
|
409
|
-
end
|
410
|
-
|
411
|
-
it "errors out for invalid values" do |variable|
|
412
|
-
lambda {
|
413
|
-
# page that results in an offset larger than BIGINT
|
414
|
-
Project.page(307445734561825862)
|
415
|
-
}.should raise_error(WillPaginate::InvalidPage, "invalid offset: 9223372036854775830")
|
416
|
-
end
|
417
|
-
end
|
@@ -1,140 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'active_record/fixtures'
|
3
|
-
require 'active_support/multibyte' # needed for Ruby 1.9.1
|
4
|
-
require 'stringio'
|
5
|
-
require 'erb'
|
6
|
-
|
7
|
-
# https://travis-ci.org/mislav/will_paginate/jobs/99999001
|
8
|
-
require 'active_support/core_ext/string/conversions'
|
9
|
-
class String
|
10
|
-
alias to_datetime_without_patch to_datetime
|
11
|
-
def to_datetime
|
12
|
-
to_datetime_without_patch
|
13
|
-
rescue ArgumentError
|
14
|
-
return nil
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
$query_count = 0
|
19
|
-
$query_sql = []
|
20
|
-
|
21
|
-
ignore_sql = /
|
22
|
-
^(
|
23
|
-
PRAGMA | SHOW\ (max_identifier_length|search_path) |
|
24
|
-
SELECT\ (currval|CAST|@@IDENTITY|@@ROWCOUNT) |
|
25
|
-
SHOW\ ((FULL\ )?FIELDS|TABLES)
|
26
|
-
)\b |
|
27
|
-
\bFROM\ (sqlite_master|pg_tables|pg_attribute)\b
|
28
|
-
/x
|
29
|
-
|
30
|
-
ActiveSupport::Notifications.subscribe(/^sql\./) do |*args|
|
31
|
-
payload = args.last
|
32
|
-
unless payload[:name] =~ /^Fixture/ or payload[:sql] =~ ignore_sql
|
33
|
-
$query_count += 1
|
34
|
-
$query_sql << payload[:sql]
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
module ActiverecordTestConnector
|
39
|
-
extend self
|
40
|
-
|
41
|
-
attr_accessor :connected
|
42
|
-
|
43
|
-
FIXTURES_PATH = File.expand_path('../../fixtures', __FILE__)
|
44
|
-
|
45
|
-
Fixtures = defined?(ActiveRecord::FixtureSet) ? ActiveRecord::FixtureSet :
|
46
|
-
defined?(ActiveRecord::Fixtures) ? ActiveRecord::Fixtures :
|
47
|
-
::Fixtures
|
48
|
-
|
49
|
-
# Set our defaults
|
50
|
-
self.connected = false
|
51
|
-
|
52
|
-
def setup
|
53
|
-
unless self.connected
|
54
|
-
setup_connection
|
55
|
-
load_schema
|
56
|
-
add_load_path FIXTURES_PATH
|
57
|
-
self.connected = true
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
private
|
62
|
-
|
63
|
-
def add_load_path(path)
|
64
|
-
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
|
65
|
-
dep.autoload_paths.unshift path
|
66
|
-
end
|
67
|
-
|
68
|
-
def setup_connection
|
69
|
-
db = ENV['DB'].blank?? 'sqlite3' : ENV['DB']
|
70
|
-
|
71
|
-
erb = ERB.new(File.read(File.expand_path('../../database.yml', __FILE__)))
|
72
|
-
configurations = YAML.load(erb.result)
|
73
|
-
raise "no configuration for '#{db}'" unless configurations.key? db
|
74
|
-
configuration = configurations[db]
|
75
|
-
|
76
|
-
# ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
|
77
|
-
puts "using #{configuration['adapter']} adapter"
|
78
|
-
|
79
|
-
ActiveRecord::Base.configurations = { db => configuration }
|
80
|
-
ActiveRecord::Base.establish_connection(db.to_sym)
|
81
|
-
ActiveRecord::Base.default_timezone = :utc
|
82
|
-
|
83
|
-
case configuration['adapter']
|
84
|
-
when 'mysql'
|
85
|
-
fix_primary_key(ActiveRecord::ConnectionAdapters::MysqlAdapter)
|
86
|
-
when 'mysql2'
|
87
|
-
fix_primary_key(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def load_schema
|
92
|
-
begin
|
93
|
-
$stdout = StringIO.new
|
94
|
-
ActiveRecord::Migration.verbose = false
|
95
|
-
load File.join(FIXTURES_PATH, 'schema.rb')
|
96
|
-
ensure
|
97
|
-
$stdout = STDOUT
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def fix_primary_key(adapter_class)
|
102
|
-
if ActiveRecord::VERSION::STRING < "4.1"
|
103
|
-
adapter_class::NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
module FixtureSetup
|
108
|
-
def fixtures(*tables)
|
109
|
-
table_names = tables.map { |t| t.to_s }
|
110
|
-
|
111
|
-
fixtures = Fixtures.create_fixtures ActiverecordTestConnector::FIXTURES_PATH, table_names
|
112
|
-
@@loaded_fixtures = {}
|
113
|
-
@@fixture_cache = {}
|
114
|
-
|
115
|
-
unless fixtures.nil?
|
116
|
-
if fixtures.instance_of?(Fixtures)
|
117
|
-
@@loaded_fixtures[fixtures.table_name] = fixtures
|
118
|
-
else
|
119
|
-
fixtures.each { |f| @@loaded_fixtures[f.table_name] = f }
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
table_names.each do |table_name|
|
124
|
-
define_method(table_name) do |*fixtures|
|
125
|
-
@@fixture_cache[table_name] ||= {}
|
126
|
-
|
127
|
-
instances = fixtures.map do |fixture|
|
128
|
-
if @@loaded_fixtures[table_name][fixture.to_s]
|
129
|
-
@@fixture_cache[table_name][fixture] ||= @@loaded_fixtures[table_name][fixture.to_s].find
|
130
|
-
else
|
131
|
-
raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
instances.size == 1 ? instances.first : instances
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
@@ -1,114 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
if !ENV['SKIP_NONRAILS_TESTS']
|
4
|
-
require 'will_paginate/data_mapper'
|
5
|
-
require File.expand_path('../data_mapper_test_connector', __FILE__)
|
6
|
-
datamapper_loaded = true
|
7
|
-
else
|
8
|
-
datamapper_loaded = false
|
9
|
-
end
|
10
|
-
|
11
|
-
describe WillPaginate::DataMapper do
|
12
|
-
|
13
|
-
it "has per_page" do
|
14
|
-
Animal.per_page.should == 30
|
15
|
-
begin
|
16
|
-
Animal.per_page = 10
|
17
|
-
Animal.per_page.should == 10
|
18
|
-
|
19
|
-
subclass = Class.new(Animal)
|
20
|
-
subclass.per_page.should == 10
|
21
|
-
ensure
|
22
|
-
Animal.per_page = 30
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
it "doesn't make normal collections appear paginated" do
|
27
|
-
Animal.all.should_not be_paginated
|
28
|
-
end
|
29
|
-
|
30
|
-
it "paginates to first page by default" do
|
31
|
-
animals = Animal.paginate(:page => nil)
|
32
|
-
|
33
|
-
animals.should be_paginated
|
34
|
-
animals.current_page.should == 1
|
35
|
-
animals.per_page.should == 30
|
36
|
-
animals.offset.should == 0
|
37
|
-
animals.total_entries.should == 3
|
38
|
-
animals.total_pages.should == 1
|
39
|
-
end
|
40
|
-
|
41
|
-
it "paginates to first page, explicit limit" do
|
42
|
-
animals = Animal.paginate(:page => 1, :per_page => 2)
|
43
|
-
|
44
|
-
animals.current_page.should == 1
|
45
|
-
animals.per_page.should == 2
|
46
|
-
animals.total_entries.should == 3
|
47
|
-
animals.total_pages.should == 2
|
48
|
-
animals.map {|a| a.name }.should == %w[ Dog Cat ]
|
49
|
-
end
|
50
|
-
|
51
|
-
it "paginates to second page" do
|
52
|
-
animals = Animal.paginate(:page => 2, :per_page => 2)
|
53
|
-
|
54
|
-
animals.current_page.should == 2
|
55
|
-
animals.offset.should == 2
|
56
|
-
animals.map {|a| a.name }.should == %w[ Lion ]
|
57
|
-
end
|
58
|
-
|
59
|
-
it "paginates a collection" do
|
60
|
-
friends = Animal.all(:notes.like => '%friend%')
|
61
|
-
friends.paginate(:page => 1).per_page.should == 30
|
62
|
-
friends.paginate(:page => 1, :per_page => 1).total_entries.should == 2
|
63
|
-
end
|
64
|
-
|
65
|
-
it "paginates a limited collection" do
|
66
|
-
animals = Animal.all(:limit => 2).paginate(:page => 1)
|
67
|
-
animals.per_page.should == 2
|
68
|
-
end
|
69
|
-
|
70
|
-
it "has page() method" do
|
71
|
-
Animal.page(2).per_page.should == 30
|
72
|
-
Animal.page(2).offset.should == 30
|
73
|
-
Animal.page(2).current_page.should == 2
|
74
|
-
Animal.all(:limit => 2).page(2).per_page.should == 2
|
75
|
-
end
|
76
|
-
|
77
|
-
it "has total_pages at 1 for empty collections" do
|
78
|
-
Animal.all(:conditions => ['1=2']).page(1).total_pages.should == 1
|
79
|
-
end
|
80
|
-
|
81
|
-
it "overrides total_entries count with a fixed value" do
|
82
|
-
lambda {
|
83
|
-
animals = Animal.paginate :page => 1, :per_page => 3, :total_entries => 999
|
84
|
-
animals.total_entries.should == 999
|
85
|
-
}.should run_queries(0)
|
86
|
-
end
|
87
|
-
|
88
|
-
it "supports a non-int for total_entries" do
|
89
|
-
topics = Animal.paginate :page => 1, :per_page => 3, :total_entries => "999"
|
90
|
-
topics.total_entries.should == 999
|
91
|
-
end
|
92
|
-
|
93
|
-
|
94
|
-
it "can iterate and then call WP methods" do
|
95
|
-
animals = Animal.all(:limit => 2).page(1)
|
96
|
-
animals.each { |a| }
|
97
|
-
animals.total_entries.should == 3
|
98
|
-
end
|
99
|
-
|
100
|
-
it "augments to_a to return a WP::Collection" do
|
101
|
-
animals = Animal.all(:limit => 2).page(1)
|
102
|
-
array = animals.to_a
|
103
|
-
array.size.should == 2
|
104
|
-
array.should be_kind_of(WillPaginate::Collection)
|
105
|
-
array.current_page.should == 1
|
106
|
-
array.per_page.should == 2
|
107
|
-
end
|
108
|
-
|
109
|
-
it "doesn't have a problem assigning has-one-through relationship" do
|
110
|
-
human = Human.create :name => "Mislav"
|
111
|
-
human.pet = Animal.first
|
112
|
-
end
|
113
|
-
|
114
|
-
end if datamapper_loaded
|