will_paginate 3.3.1 → 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 +2 -2
- data/lib/will_paginate/locale/en.yml +2 -0
- data/lib/will_paginate/version.rb +3 -3
- data/lib/will_paginate/view_helpers/action_view.rb +6 -2
- data/lib/will_paginate/view_helpers/link_renderer.rb +7 -5
- data/lib/will_paginate.rb +0 -12
- metadata +8 -38
- 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/finders/active_record_spec.rb +0 -424
- data/spec/finders/activerecord_test_connector.rb +0 -107
- 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 -91
- data/spec/per_page_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -41
- data/spec/view_helpers/action_view_spec.rb +0 -481
- 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 -105
@@ -1,424 +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 "overrides empty? count call with a total_entries fixed value" do
|
167
|
-
lambda {
|
168
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => 999
|
169
|
-
topics.should_not be_empty
|
170
|
-
}.should run_queries(0)
|
171
|
-
end
|
172
|
-
|
173
|
-
it "removes :include for count" do
|
174
|
-
lambda {
|
175
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
176
|
-
developers.total_entries.should == 11
|
177
|
-
$query_sql.last.should_not =~ /\bJOIN\b/
|
178
|
-
}.should run_queries(1)
|
179
|
-
end
|
180
|
-
|
181
|
-
it "keeps :include for count when they are referenced in :conditions" do
|
182
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
183
|
-
with_condition = developers.where('projects.id > 1')
|
184
|
-
with_condition = with_condition.references(:projects) if with_condition.respond_to?(:references)
|
185
|
-
with_condition.total_entries.should == 1
|
186
|
-
|
187
|
-
$query_sql.last.should =~ /\bJOIN\b/
|
188
|
-
end
|
189
|
-
|
190
|
-
it "should count with group" do
|
191
|
-
Developer.group(:salary).page(1).total_entries.should == 4
|
192
|
-
end
|
193
|
-
|
194
|
-
it "should count with select" do
|
195
|
-
Topic.select('title, content').page(1).total_entries.should == 4
|
196
|
-
end
|
197
|
-
|
198
|
-
it "removes :reorder for count with group" do
|
199
|
-
Project.group(:id).reorder(:id).page(1).total_entries
|
200
|
-
$query_sql.last.should_not =~ /\ORDER\b/
|
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
|
-
expected = topics(:ar)
|
244
|
-
lambda {
|
245
|
-
sql = "select id, title, content from topics order by topics.title"
|
246
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
|
247
|
-
topics.first.should == expected
|
248
|
-
}.should run_queries(2)
|
249
|
-
|
250
|
-
$query_sql.last.should include('COUNT')
|
251
|
-
$query_sql.last.should_not include('order by topics.title')
|
252
|
-
end
|
253
|
-
|
254
|
-
it "shouldn't change the original query string" do
|
255
|
-
query = 'select * from topics where 1 = 2'
|
256
|
-
original_query = query.dup
|
257
|
-
Topic.paginate_by_sql(query, :page => 1)
|
258
|
-
query.should == original_query
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
it "doesn't mangle options" do
|
263
|
-
options = { :page => 1 }
|
264
|
-
options.expects(:delete).never
|
265
|
-
options_before = options.dup
|
266
|
-
|
267
|
-
Topic.paginate(options)
|
268
|
-
options.should == options_before
|
269
|
-
end
|
270
|
-
|
271
|
-
it "should get first page of Topics with a single query" do
|
272
|
-
lambda {
|
273
|
-
result = Topic.paginate :page => nil
|
274
|
-
result.to_a # trigger loading of records
|
275
|
-
result.current_page.should == 1
|
276
|
-
result.total_pages.should == 1
|
277
|
-
result.size.should == 4
|
278
|
-
}.should run_queries(1)
|
279
|
-
end
|
280
|
-
|
281
|
-
it "should get second (inexistent) page of Topics, requiring 1 query" do
|
282
|
-
lambda {
|
283
|
-
result = Topic.paginate :page => 2
|
284
|
-
result.total_pages.should == 1
|
285
|
-
result.should be_empty
|
286
|
-
}.should run_queries(1)
|
287
|
-
end
|
288
|
-
|
289
|
-
describe "associations" do
|
290
|
-
it "should paginate" do
|
291
|
-
dhh = users(:david)
|
292
|
-
expected_name_ordered = projects(:action_controller, :active_record)
|
293
|
-
expected_id_ordered = projects(:active_record, :action_controller)
|
294
|
-
|
295
|
-
lambda {
|
296
|
-
# with association-specified order
|
297
|
-
result = ignore_deprecation {
|
298
|
-
dhh.projects.includes(:topics).order('projects.name').paginate(:page => 1)
|
299
|
-
}
|
300
|
-
result.to_a.should == expected_name_ordered
|
301
|
-
result.total_entries.should == 2
|
302
|
-
}.should run_queries(2)
|
303
|
-
|
304
|
-
# with explicit order
|
305
|
-
result = dhh.projects.paginate(:page => 1).reorder('projects.id')
|
306
|
-
result.should == expected_id_ordered
|
307
|
-
result.total_entries.should == 2
|
308
|
-
|
309
|
-
lambda {
|
310
|
-
dhh.projects.order('projects.id').limit(4).to_a
|
311
|
-
}.should_not raise_error
|
312
|
-
|
313
|
-
result = dhh.projects.paginate(:page => 1, :per_page => 4).reorder('projects.id')
|
314
|
-
result.should == expected_id_ordered
|
315
|
-
|
316
|
-
# has_many with implicit order
|
317
|
-
topic = Topic.find(1)
|
318
|
-
expected = replies(:spam, :witty_retort)
|
319
|
-
# FIXME: wow, this is ugly
|
320
|
-
topic.replies.paginate(:page => 1).map(&:id).sort.should == expected.map(&:id).sort
|
321
|
-
topic.replies.paginate(:page => 1).reorder('replies.id ASC').should == expected.reverse
|
322
|
-
end
|
323
|
-
|
324
|
-
it "should paginate through association extension" do
|
325
|
-
project = Project.order('id').first
|
326
|
-
expected = [replies(:brave)]
|
327
|
-
|
328
|
-
lambda {
|
329
|
-
result = project.replies.only_recent.paginate(:page => 1)
|
330
|
-
result.should == expected
|
331
|
-
}.should run_queries(1)
|
332
|
-
end
|
333
|
-
end
|
334
|
-
|
335
|
-
it "should paginate with joins" do
|
336
|
-
result = nil
|
337
|
-
join_sql = 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id'
|
338
|
-
|
339
|
-
lambda {
|
340
|
-
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
341
|
-
result.to_a # trigger loading of records
|
342
|
-
result.size.should == 2
|
343
|
-
developer_names = result.map(&:name)
|
344
|
-
developer_names.should include('David')
|
345
|
-
developer_names.should include('Jamis')
|
346
|
-
}.should run_queries(1)
|
347
|
-
|
348
|
-
lambda {
|
349
|
-
expected = result.to_a
|
350
|
-
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
351
|
-
result.should == expected
|
352
|
-
result.total_entries.should == 2
|
353
|
-
}.should run_queries(1)
|
354
|
-
end
|
355
|
-
|
356
|
-
it "should paginate with group" do
|
357
|
-
result = nil
|
358
|
-
lambda {
|
359
|
-
result = Developer.select('salary').order('salary').group('salary').
|
360
|
-
paginate(:page => 1, :per_page => 10).to_a
|
361
|
-
}.should run_queries(1)
|
362
|
-
|
363
|
-
expected = users(:david, :jamis, :dev_10, :poor_jamis).map(&:salary).sort
|
364
|
-
result.map(&:salary).should == expected
|
365
|
-
end
|
366
|
-
|
367
|
-
it "should not paginate with dynamic finder" do
|
368
|
-
lambda {
|
369
|
-
Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
|
370
|
-
}.should raise_error(NoMethodError)
|
371
|
-
end
|
372
|
-
|
373
|
-
describe "scopes" do
|
374
|
-
it "should paginate" do
|
375
|
-
result = Developer.poor.paginate :page => 1, :per_page => 1
|
376
|
-
result.size.should == 1
|
377
|
-
result.total_entries.should == 2
|
378
|
-
end
|
379
|
-
|
380
|
-
it "should paginate on habtm association" do
|
381
|
-
project = projects(:active_record)
|
382
|
-
lambda {
|
383
|
-
result = ignore_deprecation { project.developers.poor.paginate :page => 1, :per_page => 1 }
|
384
|
-
result.size.should == 1
|
385
|
-
result.total_entries.should == 1
|
386
|
-
}.should run_queries(2)
|
387
|
-
end
|
388
|
-
|
389
|
-
it "should paginate on hmt association" do
|
390
|
-
project = projects(:active_record)
|
391
|
-
expected = [replies(:brave)]
|
392
|
-
|
393
|
-
lambda {
|
394
|
-
result = project.replies.recent.paginate :page => 1, :per_page => 1
|
395
|
-
result.should == expected
|
396
|
-
result.total_entries.should == 1
|
397
|
-
}.should run_queries(2)
|
398
|
-
end
|
399
|
-
|
400
|
-
it "should paginate on has_many association" do
|
401
|
-
project = projects(:active_record)
|
402
|
-
expected = [topics(:ar)]
|
403
|
-
|
404
|
-
lambda {
|
405
|
-
result = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
|
406
|
-
result.should == expected
|
407
|
-
result.total_entries.should == 1
|
408
|
-
}.should run_queries(2)
|
409
|
-
end
|
410
|
-
end
|
411
|
-
|
412
|
-
it "should not paginate an array of IDs" do
|
413
|
-
lambda {
|
414
|
-
Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
|
415
|
-
}.should raise_error(ArgumentError)
|
416
|
-
end
|
417
|
-
|
418
|
-
it "errors out for invalid values" do |variable|
|
419
|
-
lambda {
|
420
|
-
# page that results in an offset larger than BIGINT
|
421
|
-
Project.page(307445734561825862)
|
422
|
-
}.should raise_error(WillPaginate::InvalidPage, "invalid offset: 9223372036854775830")
|
423
|
-
end
|
424
|
-
end
|
@@ -1,107 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'active_record/fixtures'
|
3
|
-
require 'stringio'
|
4
|
-
require 'erb'
|
5
|
-
|
6
|
-
$query_count = 0
|
7
|
-
$query_sql = []
|
8
|
-
|
9
|
-
ignore_sql = /
|
10
|
-
^(
|
11
|
-
PRAGMA | SHOW\ (max_identifier_length|search_path) |
|
12
|
-
SELECT\ (currval|CAST|@@IDENTITY|@@ROWCOUNT) |
|
13
|
-
SHOW\ ((FULL\ )?FIELDS|TABLES)
|
14
|
-
)\b |
|
15
|
-
\bFROM\ (sqlite_master|pg_tables|pg_attribute)\b
|
16
|
-
/x
|
17
|
-
|
18
|
-
ActiveSupport::Notifications.subscribe(/^sql\./) do |*args|
|
19
|
-
payload = args.last
|
20
|
-
unless payload[:name] =~ /^Fixture/ or payload[:sql] =~ ignore_sql
|
21
|
-
$query_count += 1
|
22
|
-
$query_sql << payload[:sql]
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
module ActiverecordTestConnector
|
27
|
-
extend self
|
28
|
-
|
29
|
-
attr_accessor :connected
|
30
|
-
|
31
|
-
FIXTURES_PATH = File.expand_path('../../fixtures', __FILE__)
|
32
|
-
|
33
|
-
# Set our defaults
|
34
|
-
self.connected = false
|
35
|
-
|
36
|
-
def setup
|
37
|
-
unless self.connected
|
38
|
-
setup_connection
|
39
|
-
load_schema
|
40
|
-
add_load_path FIXTURES_PATH
|
41
|
-
self.connected = true
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
def add_load_path(path)
|
48
|
-
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
|
49
|
-
dep.autoload_paths.unshift path
|
50
|
-
end
|
51
|
-
|
52
|
-
def setup_connection
|
53
|
-
db = ENV['DB'].blank?? 'sqlite3' : ENV['DB']
|
54
|
-
|
55
|
-
erb = ERB.new(File.read(File.expand_path('../../database.yml', __FILE__)))
|
56
|
-
configurations = YAML.load(erb.result)
|
57
|
-
raise "no configuration for '#{db}'" unless configurations.key? db
|
58
|
-
configuration = configurations[db]
|
59
|
-
|
60
|
-
# ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
|
61
|
-
puts "using #{configuration['adapter']} adapter"
|
62
|
-
|
63
|
-
ActiveRecord::Base.configurations = { db => configuration }
|
64
|
-
ActiveRecord::Base.establish_connection(db.to_sym)
|
65
|
-
ActiveRecord::Base.default_timezone = :utc
|
66
|
-
end
|
67
|
-
|
68
|
-
def load_schema
|
69
|
-
begin
|
70
|
-
$stdout = StringIO.new
|
71
|
-
ActiveRecord::Migration.verbose = false
|
72
|
-
load File.join(FIXTURES_PATH, 'schema.rb')
|
73
|
-
ensure
|
74
|
-
$stdout = STDOUT
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
module FixtureSetup
|
79
|
-
def fixtures(*tables)
|
80
|
-
table_names = tables.map { |t| t.to_s }
|
81
|
-
|
82
|
-
fixtures = ActiveRecord::FixtureSet.create_fixtures(ActiverecordTestConnector::FIXTURES_PATH, table_names)
|
83
|
-
@@loaded_fixtures = {}
|
84
|
-
@@fixture_cache = {}
|
85
|
-
|
86
|
-
unless fixtures.nil?
|
87
|
-
fixtures.each { |f| @@loaded_fixtures[f.table_name] = f }
|
88
|
-
end
|
89
|
-
|
90
|
-
table_names.each do |table_name|
|
91
|
-
define_method(table_name) do |*names|
|
92
|
-
@@fixture_cache[table_name] ||= {}
|
93
|
-
|
94
|
-
instances = names.map do |name|
|
95
|
-
if @@loaded_fixtures[table_name][name.to_s]
|
96
|
-
@@fixture_cache[table_name][name] ||= @@loaded_fixtures[table_name][name.to_s].find
|
97
|
-
else
|
98
|
-
raise StandardError, "No fixture with name '#{name}' found for table '#{table_name}'"
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
instances.size == 1 ? instances.first : instances
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
data/spec/fixtures/admin.rb
DELETED
data/spec/fixtures/developer.rb
DELETED
data/spec/fixtures/project.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
class Project < ActiveRecord::Base
|
2
|
-
has_and_belongs_to_many :developers, :join_table => 'developers_projects'
|
3
|
-
|
4
|
-
has_many :topics
|
5
|
-
# :finder_sql => 'SELECT * FROM topics WHERE (topics.project_id = #{id})',
|
6
|
-
# :counter_sql => 'SELECT COUNT(*) FROM topics WHERE (topics.project_id = #{id})'
|
7
|
-
|
8
|
-
has_many :replies, :through => :topics do
|
9
|
-
def only_recent(params = {})
|
10
|
-
where(['replies.created_at > ?', 15.minutes.ago])
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
data/spec/fixtures/projects.yml
DELETED
data/spec/fixtures/replies.yml
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
witty_retort:
|
2
|
-
id: 1
|
3
|
-
topic_id: 1
|
4
|
-
content: Birdman is better!
|
5
|
-
created_at: <%= 6.hours.ago.utc.to_s(:db) %>
|
6
|
-
|
7
|
-
another:
|
8
|
-
id: 2
|
9
|
-
topic_id: 2
|
10
|
-
content: Nuh uh!
|
11
|
-
created_at: <%= 1.hour.ago.utc.to_s(:db) %>
|
12
|
-
|
13
|
-
spam:
|
14
|
-
id: 3
|
15
|
-
topic_id: 1
|
16
|
-
content: Nice site!
|
17
|
-
created_at: <%= 1.hour.ago.utc.to_s(:db) %>
|
18
|
-
|
19
|
-
decisive:
|
20
|
-
id: 4
|
21
|
-
topic_id: 4
|
22
|
-
content: "I'm getting to the bottom of this"
|
23
|
-
created_at: <%= 30.minutes.ago.utc.to_s(:db) %>
|
24
|
-
|
25
|
-
brave:
|
26
|
-
id: 5
|
27
|
-
topic_id: 4
|
28
|
-
content: "AR doesn't scare me a bit"
|
29
|
-
created_at: <%= 10.minutes.ago.utc.to_s(:db) %>
|
data/spec/fixtures/reply.rb
DELETED
data/spec/fixtures/schema.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
ActiveRecord::Schema.define do
|
2
|
-
|
3
|
-
create_table "users", :force => true do |t|
|
4
|
-
t.column "name", :text
|
5
|
-
t.column "salary", :integer, :default => 70000
|
6
|
-
t.column "created_at", :datetime
|
7
|
-
t.column "updated_at", :datetime
|
8
|
-
t.column "type", :text
|
9
|
-
end
|
10
|
-
|
11
|
-
create_table "projects", :force => true do |t|
|
12
|
-
t.column "name", :text
|
13
|
-
end
|
14
|
-
|
15
|
-
create_table "developers_projects", :id => false, :force => true do |t|
|
16
|
-
t.column "developer_id", :integer, :null => false
|
17
|
-
t.column "project_id", :integer, :null => false
|
18
|
-
t.column "joined_on", :date
|
19
|
-
t.column "access_level", :integer, :default => 1
|
20
|
-
end
|
21
|
-
|
22
|
-
create_table "topics", :force => true do |t|
|
23
|
-
t.column "project_id", :integer
|
24
|
-
t.column "title", :string
|
25
|
-
t.column "subtitle", :string
|
26
|
-
t.column "content", :text
|
27
|
-
t.column "created_at", :datetime
|
28
|
-
t.column "updated_at", :datetime
|
29
|
-
end
|
30
|
-
|
31
|
-
create_table "replies", :force => true do |t|
|
32
|
-
t.column "content", :text
|
33
|
-
t.column "created_at", :datetime
|
34
|
-
t.column "updated_at", :datetime
|
35
|
-
t.column "topic_id", :integer
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
data/spec/fixtures/topic.rb
DELETED
data/spec/fixtures/topics.yml
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
futurama:
|
2
|
-
id: 1
|
3
|
-
title: Isnt futurama awesome?
|
4
|
-
subtitle: It really is, isnt it.
|
5
|
-
content: I like futurama
|
6
|
-
created_at: <%= 1.day.ago.utc.to_s(:db) %>
|
7
|
-
updated_at:
|
8
|
-
|
9
|
-
harvey_birdman:
|
10
|
-
id: 2
|
11
|
-
title: Harvey Birdman is the king of all men
|
12
|
-
subtitle: yup
|
13
|
-
content: He really is
|
14
|
-
created_at: <%= 2.hours.ago.utc.to_s(:db) %>
|
15
|
-
updated_at:
|
16
|
-
|
17
|
-
rails:
|
18
|
-
id: 3
|
19
|
-
project_id: 1
|
20
|
-
title: Rails is nice
|
21
|
-
subtitle: It makes me happy
|
22
|
-
content: except when I have to hack internals to fix pagination. even then really.
|
23
|
-
created_at: <%= 20.minutes.ago.utc.to_s(:db) %>
|
24
|
-
|
25
|
-
ar:
|
26
|
-
id: 4
|
27
|
-
project_id: 1
|
28
|
-
title: ActiveRecord sometimes freaks me out
|
29
|
-
content: "I mean, what's the deal with eager loading?"
|
30
|
-
created_at: <%= 15.minutes.ago.utc.to_s(:db) %>
|