will_paginate 3.0.7 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/will_paginate/active_record.rb +5 -14
- data/lib/will_paginate/data_mapper.rb +6 -1
- data/lib/will_paginate/deprecation.rb +1 -1
- data/lib/will_paginate/mongoid.rb +46 -0
- data/lib/will_paginate/railtie.rb +5 -2
- data/lib/will_paginate/version.rb +2 -2
- data/spec/finders/active_record_spec.rb +9 -168
- data/spec/finders/activerecord_test_connector.rb +12 -1
- data/spec/finders/data_mapper_spec.rb +13 -0
- data/spec/finders/mongoid_spec.rb +140 -0
- data/spec/fixtures/admin.rb +1 -1
- data/spec/fixtures/developer.rb +1 -8
- data/spec/fixtures/project.rb +1 -1
- data/spec/page_number_spec.rb +29 -11
- data/spec/spec_helper.rb +21 -1
- data/spec/view_helpers/action_view_spec.rb +31 -19
- data/spec/view_helpers/base_spec.rb +1 -0
- data/spec/view_helpers/view_example_group.rb +26 -6
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3445d24047dd419909e314bc5f1b9eb3fdb7f73
|
4
|
+
data.tar.gz: 2e6ce5705bc95b523bdd8a7ffa9d8e0b35acc36f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdc9b0d1f20379dc1df3dc503796834b329976c90a63f4e0f14b77152aef8721bea240f850d0abc0c0171a3e1ad10d5c88a354136e47d4bb933b29a205311d7d
|
7
|
+
data.tar.gz: c35edfea8e81e70fef045d9f1c3c32bea89723a1bd74a6c2edb438c62fc54c3356eb4704329c42b35760178c71a505ee0d86c0109a6f7cd3fdacd5276af4eb74
|
data/README.md
CHANGED
@@ -58,4 +58,4 @@ Happy paginating.
|
|
58
58
|
[install]: https://github.com/mislav/will_paginate/wiki/Installation "will_paginate installation"
|
59
59
|
[group]: http://groups.google.com/group/will_paginate "will_paginate discussion and support group"
|
60
60
|
[issues]: https://github.com/mislav/will_paginate/issues
|
61
|
-
[css]: http://mislav.
|
61
|
+
[css]: http://mislav.github.io/will_paginate/
|
@@ -2,11 +2,6 @@ require 'will_paginate/per_page'
|
|
2
2
|
require 'will_paginate/page_number'
|
3
3
|
require 'will_paginate/collection'
|
4
4
|
require 'active_record'
|
5
|
-
begin
|
6
|
-
require 'active_record/deprecated_finders'
|
7
|
-
rescue LoadError
|
8
|
-
# only for Rails 4.1
|
9
|
-
end
|
10
5
|
|
11
6
|
module WillPaginate
|
12
7
|
# = Paginating finders for ActiveRecord models
|
@@ -26,7 +21,7 @@ module WillPaginate
|
|
26
21
|
include WillPaginate::CollectionMethods
|
27
22
|
|
28
23
|
attr_accessor :current_page
|
29
|
-
attr_writer :total_entries
|
24
|
+
attr_writer :total_entries
|
30
25
|
|
31
26
|
def per_page(value = nil)
|
32
27
|
if value.nil? then limit_value
|
@@ -88,9 +83,6 @@ module WillPaginate
|
|
88
83
|
excluded = [:order, :limit, :offset, :reorder]
|
89
84
|
excluded << :includes unless eager_loading?
|
90
85
|
rel = self.except(*excluded)
|
91
|
-
# TODO: hack. decide whether to keep
|
92
|
-
rel = rel.apply_finder_options(@wp_count_options) if defined? @wp_count_options
|
93
|
-
|
94
86
|
column_name = (select_for_count(rel) || :all)
|
95
87
|
rel.count(column_name)
|
96
88
|
else
|
@@ -142,7 +134,6 @@ module WillPaginate
|
|
142
134
|
def copy_will_paginate_data(other)
|
143
135
|
other.current_page = current_page unless other.current_page
|
144
136
|
other.total_entries = nil if defined? @total_entries_queried
|
145
|
-
other.wp_count_options = @wp_count_options if defined? @wp_count_options
|
146
137
|
other
|
147
138
|
end
|
148
139
|
|
@@ -158,15 +149,15 @@ module WillPaginate
|
|
158
149
|
def paginate(options)
|
159
150
|
options = options.dup
|
160
151
|
pagenum = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
|
152
|
+
options.delete(:page)
|
161
153
|
per_page = options.delete(:per_page) || self.per_page
|
162
154
|
total = options.delete(:total_entries)
|
163
155
|
|
164
|
-
|
165
|
-
|
156
|
+
if options.any?
|
157
|
+
raise ArgumentError, "unsupported parameters: %p" % options.keys
|
158
|
+
end
|
166
159
|
|
167
160
|
rel = limit(per_page.to_i).page(pagenum)
|
168
|
-
rel = rel.apply_finder_options(options) if options.any?
|
169
|
-
rel.wp_count_options = count_options if count_options
|
170
161
|
rel.total_entries = total.to_i unless total.blank?
|
171
162
|
rel
|
172
163
|
end
|
@@ -21,11 +21,15 @@ module WillPaginate
|
|
21
21
|
options = options.dup
|
22
22
|
pagenum = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
|
23
23
|
per_page = options.delete(:per_page) || self.per_page
|
24
|
+
total = options.delete(:total_entries)
|
24
25
|
|
25
26
|
options.delete(:page)
|
26
27
|
options[:limit] = per_page.to_i
|
27
28
|
|
28
|
-
|
29
|
+
|
30
|
+
col = all(options).page(pagenum)
|
31
|
+
col.total_entries = total.to_i unless total.nil? || (total.kind_of?(String) && total.strip.empty?)
|
32
|
+
col
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
@@ -33,6 +37,7 @@ module WillPaginate
|
|
33
37
|
include WillPaginate::CollectionMethods
|
34
38
|
|
35
39
|
attr_accessor :current_page
|
40
|
+
attr_writer :total_entries
|
36
41
|
|
37
42
|
def paginated?
|
38
43
|
!current_page.nil?
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'will_paginate/collection'
|
3
|
+
|
4
|
+
module WillPaginate
|
5
|
+
module Mongoid
|
6
|
+
module CriteriaMethods
|
7
|
+
def paginate(options = {})
|
8
|
+
extend CollectionMethods
|
9
|
+
@current_page = WillPaginate::PageNumber(options[:page] || @current_page || 1)
|
10
|
+
@page_multiplier = current_page - 1
|
11
|
+
pp = (options[:per_page] || per_page || WillPaginate.per_page).to_i
|
12
|
+
limit(pp).skip(@page_multiplier * pp)
|
13
|
+
end
|
14
|
+
|
15
|
+
def per_page(value = :non_given)
|
16
|
+
if value == :non_given
|
17
|
+
options[:limit] == 0 ? nil : options[:limit] # in new Mongoid versions a nil limit is saved as 0
|
18
|
+
else
|
19
|
+
limit(value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def page(page)
|
24
|
+
paginate(:page => page)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module CollectionMethods
|
29
|
+
attr_reader :current_page
|
30
|
+
|
31
|
+
def total_entries
|
32
|
+
@total_entries ||= count
|
33
|
+
end
|
34
|
+
|
35
|
+
def total_pages
|
36
|
+
(total_entries / per_page.to_f).ceil
|
37
|
+
end
|
38
|
+
|
39
|
+
def offset
|
40
|
+
@page_multiplier * per_page
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
::Mongoid::Criteria.send(:include, CriteriaMethods)
|
45
|
+
end
|
46
|
+
end
|
@@ -32,14 +32,17 @@ module WillPaginate
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def self.add_locale_path(config)
|
35
|
-
config.i18n.
|
35
|
+
config.i18n.load_path.unshift(*WillPaginate::I18n.load_path)
|
36
36
|
end
|
37
37
|
|
38
38
|
# Extending the exception handler middleware so it properly detects
|
39
39
|
# WillPaginate::InvalidPage regardless of it being a tag module.
|
40
40
|
module ShowExceptionsPatch
|
41
41
|
extend ActiveSupport::Concern
|
42
|
-
included
|
42
|
+
included do
|
43
|
+
alias_method :status_code_without_paginate, :status_code
|
44
|
+
alias_method :status_code, :status_code_with_paginate
|
45
|
+
end
|
43
46
|
def status_code_with_paginate(exception = @exception)
|
44
47
|
if exception.is_a?(WillPaginate::InvalidPage) or
|
45
48
|
(exception.respond_to?(:original_exception) &&
|
@@ -104,13 +104,6 @@ describe WillPaginate::ActiveRecord do
|
|
104
104
|
rel.last(2).should == users(:dev_7, :dev_8)
|
105
105
|
rel.page(3).last.should == users(:poor_jamis)
|
106
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
107
|
end
|
115
108
|
|
116
109
|
describe "counting" do
|
@@ -136,15 +129,6 @@ describe WillPaginate::ActiveRecord do
|
|
136
129
|
}.should run_queries(2)
|
137
130
|
end
|
138
131
|
|
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
132
|
it "supports empty? method" do
|
149
133
|
topics = Topic.paginate :page => 1, :per_page => 3
|
150
134
|
lambda {
|
@@ -250,10 +234,11 @@ describe WillPaginate::ActiveRecord do
|
|
250
234
|
end
|
251
235
|
|
252
236
|
it "should strip the order when counting" do
|
237
|
+
expected = topics(:ar)
|
253
238
|
lambda {
|
254
239
|
sql = "select id, title, content from topics order by topics.title"
|
255
240
|
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
|
256
|
-
topics.first.should ==
|
241
|
+
topics.first.should == expected
|
257
242
|
}.should run_queries(2)
|
258
243
|
|
259
244
|
$query_sql.last.should include('COUNT')
|
@@ -295,78 +280,7 @@ describe WillPaginate::ActiveRecord do
|
|
295
280
|
}.should run_queries(2)
|
296
281
|
end
|
297
282
|
|
298
|
-
it "should paginate with :order" do
|
299
|
-
result = Topic.paginate :page => 1, :order => 'created_at DESC'
|
300
|
-
result.should == topics(:futurama, :harvey_birdman, :rails, :ar).reverse
|
301
|
-
result.total_pages.should == 1
|
302
|
-
end
|
303
|
-
|
304
|
-
it "should paginate with :conditions" do
|
305
|
-
result = Topic.paginate :page => 1, :order => 'id ASC',
|
306
|
-
:conditions => ["created_at > ?", 30.minutes.ago]
|
307
|
-
result.should == topics(:rails, :ar)
|
308
|
-
result.total_pages.should == 1
|
309
|
-
end
|
310
|
-
|
311
|
-
it "should paginate with :include and :conditions" do
|
312
|
-
klass = Topic
|
313
|
-
klass = klass.references(:replies) if klass.respond_to?(:references)
|
314
|
-
|
315
|
-
result = klass.paginate \
|
316
|
-
:page => 1,
|
317
|
-
:include => :replies,
|
318
|
-
:conditions => "replies.content LIKE 'Bird%' ",
|
319
|
-
:per_page => 10
|
320
|
-
|
321
|
-
expected = klass.find :all,
|
322
|
-
:include => 'replies',
|
323
|
-
:conditions => "replies.content LIKE 'Bird%' ",
|
324
|
-
:limit => 10
|
325
|
-
|
326
|
-
result.should == expected
|
327
|
-
result.total_entries.should == 1
|
328
|
-
end
|
329
|
-
|
330
|
-
it "should paginate with :include and :order" do
|
331
|
-
result = nil
|
332
|
-
lambda {
|
333
|
-
result = Topic.paginate(:page => 1, :include => :replies, :per_page => 10,
|
334
|
-
:order => 'replies.created_at asc, topics.created_at asc').to_a
|
335
|
-
}.should run_queries(2)
|
336
|
-
|
337
|
-
expected = Topic.find :all,
|
338
|
-
:include => 'replies',
|
339
|
-
:order => 'replies.created_at asc, topics.created_at asc',
|
340
|
-
:limit => 10
|
341
|
-
|
342
|
-
result.should == expected
|
343
|
-
result.total_entries.should == 4
|
344
|
-
end
|
345
|
-
|
346
283
|
describe "associations" do
|
347
|
-
it "should paginate with include" do
|
348
|
-
project = projects(:active_record)
|
349
|
-
|
350
|
-
topics = project.topics
|
351
|
-
topics = topics.references(:replies) if topics.respond_to?(:references)
|
352
|
-
|
353
|
-
result = topics.paginate \
|
354
|
-
:page => 1,
|
355
|
-
:include => :replies,
|
356
|
-
:conditions => ["replies.content LIKE ?", 'Nice%'],
|
357
|
-
:per_page => 10
|
358
|
-
|
359
|
-
topics = Topic
|
360
|
-
topics = topics.references(:replies) if topics.respond_to?(:references)
|
361
|
-
|
362
|
-
expected = topics.find :all,
|
363
|
-
:include => 'replies',
|
364
|
-
:conditions => ["project_id = ? AND replies.content LIKE ?", project.id, 'Nice%'],
|
365
|
-
:limit => 10
|
366
|
-
|
367
|
-
result.should == expected
|
368
|
-
end
|
369
|
-
|
370
284
|
it "should paginate" do
|
371
285
|
dhh = users(:david)
|
372
286
|
expected_name_ordered = projects(:action_controller, :active_record)
|
@@ -375,7 +289,7 @@ describe WillPaginate::ActiveRecord do
|
|
375
289
|
lambda {
|
376
290
|
# with association-specified order
|
377
291
|
result = ignore_deprecation {
|
378
|
-
dhh.projects.includes(:topics).paginate(:page => 1
|
292
|
+
dhh.projects.includes(:topics).order('projects.name').paginate(:page => 1)
|
379
293
|
}
|
380
294
|
result.to_a.should == expected_name_ordered
|
381
295
|
result.total_entries.should == 2
|
@@ -387,7 +301,7 @@ describe WillPaginate::ActiveRecord do
|
|
387
301
|
result.total_entries.should == 2
|
388
302
|
|
389
303
|
lambda {
|
390
|
-
dhh.projects.
|
304
|
+
dhh.projects.order('projects.id').limit(4).to_a
|
391
305
|
}.should_not raise_error
|
392
306
|
|
393
307
|
result = dhh.projects.paginate(:page => 1, :per_page => 4).reorder('projects.id')
|
@@ -417,7 +331,7 @@ describe WillPaginate::ActiveRecord do
|
|
417
331
|
join_sql = 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id'
|
418
332
|
|
419
333
|
lambda {
|
420
|
-
result = Developer.
|
334
|
+
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
421
335
|
result.to_a # trigger loading of records
|
422
336
|
result.size.should == 2
|
423
337
|
developer_names = result.map(&:name)
|
@@ -427,8 +341,7 @@ describe WillPaginate::ActiveRecord do
|
|
427
341
|
|
428
342
|
lambda {
|
429
343
|
expected = result.to_a
|
430
|
-
result = Developer.paginate(:page => 1
|
431
|
-
:conditions => 'project_id = 1', :count => { :select => "users.id" }).to_a
|
344
|
+
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
432
345
|
result.should == expected
|
433
346
|
result.total_entries.should == 2
|
434
347
|
}.should run_queries(1)
|
@@ -437,8 +350,8 @@ describe WillPaginate::ActiveRecord do
|
|
437
350
|
it "should paginate with group" do
|
438
351
|
result = nil
|
439
352
|
lambda {
|
440
|
-
result = Developer.
|
441
|
-
:
|
353
|
+
result = Developer.select('salary').order('salary').group('salary').
|
354
|
+
paginate(:page => 1, :per_page => 10).to_a
|
442
355
|
}.should run_queries(1)
|
443
356
|
|
444
357
|
expected = users(:david, :jamis, :dev_10, :poor_jamis).map(&:salary).sort
|
@@ -451,12 +364,6 @@ describe WillPaginate::ActiveRecord do
|
|
451
364
|
}.should raise_error(NoMethodError)
|
452
365
|
end
|
453
366
|
|
454
|
-
it "should paginate with_scope" do
|
455
|
-
result = Developer.with_poor_ones { Developer.paginate :page => 1 }
|
456
|
-
result.size.should == 2
|
457
|
-
result.total_entries.should == 2
|
458
|
-
end
|
459
|
-
|
460
367
|
describe "scopes" do
|
461
368
|
it "should paginate" do
|
462
369
|
result = Developer.poor.paginate :page => 1, :per_page => 1
|
@@ -496,12 +403,6 @@ describe WillPaginate::ActiveRecord do
|
|
496
403
|
end
|
497
404
|
end
|
498
405
|
|
499
|
-
it "should paginate with :readonly option" do
|
500
|
-
lambda {
|
501
|
-
Developer.paginate :readonly => true, :page => 1
|
502
|
-
}.should_not raise_error
|
503
|
-
end
|
504
|
-
|
505
406
|
it "should not paginate an array of IDs" do
|
506
407
|
lambda {
|
507
408
|
Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
|
@@ -514,64 +415,4 @@ describe WillPaginate::ActiveRecord do
|
|
514
415
|
Project.page(307445734561825862)
|
515
416
|
}.should raise_error(WillPaginate::InvalidPage, "invalid offset: 9223372036854775830")
|
516
417
|
end
|
517
|
-
|
518
|
-
protected
|
519
|
-
|
520
|
-
def ignore_deprecation
|
521
|
-
ActiveSupport::Deprecation.silence { yield }
|
522
|
-
end
|
523
|
-
|
524
|
-
def run_queries(num)
|
525
|
-
QueryCountMatcher.new(num)
|
526
|
-
end
|
527
|
-
|
528
|
-
def show_queries(&block)
|
529
|
-
counter = QueryCountMatcher.new(nil)
|
530
|
-
counter.run block
|
531
|
-
ensure
|
532
|
-
queries = counter.performed_queries
|
533
|
-
if queries.any?
|
534
|
-
puts queries
|
535
|
-
else
|
536
|
-
puts "no queries"
|
537
|
-
end
|
538
|
-
end
|
539
|
-
|
540
|
-
end
|
541
|
-
|
542
|
-
class QueryCountMatcher
|
543
|
-
def initialize(num)
|
544
|
-
@expected_count = num
|
545
|
-
end
|
546
|
-
|
547
|
-
def matches?(block)
|
548
|
-
run(block)
|
549
|
-
|
550
|
-
if @expected_count.respond_to? :include?
|
551
|
-
@expected_count.include? @count
|
552
|
-
else
|
553
|
-
@count == @expected_count
|
554
|
-
end
|
555
|
-
end
|
556
|
-
|
557
|
-
def run(block)
|
558
|
-
$query_count = 0
|
559
|
-
$query_sql = []
|
560
|
-
block.call
|
561
|
-
ensure
|
562
|
-
@queries = $query_sql.dup
|
563
|
-
@count = $query_count
|
564
|
-
end
|
565
|
-
|
566
|
-
def performed_queries
|
567
|
-
@queries
|
568
|
-
end
|
569
|
-
|
570
|
-
def failure_message
|
571
|
-
"expected #{@expected_count} queries, got #{@count}\n#{@queries.join("\n")}"
|
572
|
-
end
|
573
|
-
|
574
|
-
def negative_failure_message
|
575
|
-
"expected query count not to be #{@expected_count}"
|
576
|
-
end
|
577
|
-
end
|
418
|
+
end
|
@@ -4,12 +4,23 @@ require 'active_support/multibyte' # needed for Ruby 1.9.1
|
|
4
4
|
require 'stringio'
|
5
5
|
require 'erb'
|
6
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
|
+
|
7
18
|
$query_count = 0
|
8
19
|
$query_sql = []
|
9
20
|
|
10
21
|
ignore_sql = /
|
11
22
|
^(
|
12
|
-
PRAGMA | SHOW\ max_identifier_length |
|
23
|
+
PRAGMA | SHOW\ (max_identifier_length|search_path) |
|
13
24
|
SELECT\ (currval|CAST|@@IDENTITY|@@ROWCOUNT) |
|
14
25
|
SHOW\ ((FULL\ )?FIELDS|TABLES)
|
15
26
|
)\b |
|
@@ -80,6 +80,19 @@ describe WillPaginate::DataMapper do
|
|
80
80
|
Animal.all(:conditions => ['1=2']).page(1).total_pages.should == 1
|
81
81
|
end
|
82
82
|
|
83
|
+
it "overrides total_entries count with a fixed value" do
|
84
|
+
lambda {
|
85
|
+
animals = Animal.paginate :page => 1, :per_page => 3, :total_entries => 999
|
86
|
+
animals.total_entries.should == 999
|
87
|
+
}.should run_queries(0)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "supports a non-int for total_entries" do
|
91
|
+
topics = Animal.paginate :page => 1, :per_page => 3, :total_entries => "999"
|
92
|
+
topics.total_entries.should == 999
|
93
|
+
end
|
94
|
+
|
95
|
+
|
83
96
|
it "can iterate and then call WP methods" do
|
84
97
|
animals = Animal.all(:limit => 2).page(1)
|
85
98
|
animals.each { |a| }
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'will_paginate/mongoid'
|
5
|
+
rescue LoadError => error
|
6
|
+
warn "Error running Mongoid specs: #{error.message}"
|
7
|
+
mongoid_loaded = false
|
8
|
+
else
|
9
|
+
Mongoid.connect_to 'will_paginate_test'
|
10
|
+
|
11
|
+
class MongoidModel
|
12
|
+
include Mongoid::Document
|
13
|
+
end
|
14
|
+
|
15
|
+
mongoid_loaded = true
|
16
|
+
end
|
17
|
+
|
18
|
+
describe WillPaginate::Mongoid do
|
19
|
+
before(:all) do
|
20
|
+
MongoidModel.delete_all
|
21
|
+
4.times { MongoidModel.create! }
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:criteria) { MongoidModel.criteria }
|
25
|
+
|
26
|
+
describe "#page" do
|
27
|
+
it "should forward to the paginate method" do
|
28
|
+
criteria.expects(:paginate).with(:page => 2).returns("itself")
|
29
|
+
criteria.page(2).should == "itself"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should not override per_page if set earlier in the chain" do
|
33
|
+
criteria.paginate(:per_page => 10).page(1).per_page.should == 10
|
34
|
+
criteria.paginate(:per_page => 20).page(1).per_page.should == 20
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#per_page" do
|
39
|
+
it "should set the limit if given an argument" do
|
40
|
+
criteria.per_page(10).options[:limit].should == 10
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return the current limit if no argument is given" do
|
44
|
+
criteria.per_page.should == nil
|
45
|
+
criteria.per_page(10).per_page.should == 10
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should be interchangable with limit" do
|
49
|
+
criteria.limit(15).per_page.should == 15
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should be nil'able" do
|
53
|
+
criteria.per_page(nil).per_page.should be_nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#paginate" do
|
58
|
+
it "should use criteria" do
|
59
|
+
criteria.paginate.should be_instance_of(::Mongoid::Criteria)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not override page number if set earlier in the chain" do
|
63
|
+
criteria.page(3).paginate.current_page.should == 3
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should limit according to per_page parameter" do
|
67
|
+
criteria.paginate(:per_page => 10).options.should include(:limit => 10)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should skip according to page and per_page parameters" do
|
71
|
+
criteria.paginate(:page => 2, :per_page => 5).options.should include(:skip => 5)
|
72
|
+
end
|
73
|
+
|
74
|
+
specify "first fallback value for per_page option is the current limit" do
|
75
|
+
criteria.limit(12).paginate.options.should include(:limit => 12)
|
76
|
+
end
|
77
|
+
|
78
|
+
specify "second fallback value for per_page option is WillPaginate.per_page" do
|
79
|
+
criteria.paginate.options.should include(:limit => WillPaginate.per_page)
|
80
|
+
end
|
81
|
+
|
82
|
+
specify "page should default to 1" do
|
83
|
+
criteria.paginate.options.should include(:skip => 0)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should convert strings to integers" do
|
87
|
+
criteria.paginate(:page => "2", :per_page => "3").options.should include(:limit => 3)
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "collection compatibility" do
|
91
|
+
describe "#total_count" do
|
92
|
+
it "should be calculated correctly" do
|
93
|
+
criteria.paginate(:per_page => 1).total_entries.should == 4
|
94
|
+
criteria.paginate(:per_page => 3).total_entries.should == 4
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should be cached" do
|
98
|
+
criteria.expects(:count).once.returns(123)
|
99
|
+
criteria.paginate
|
100
|
+
2.times { criteria.total_entries.should == 123 }
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should calculate total_pages" do
|
105
|
+
criteria.paginate(:per_page => 1).total_pages.should == 4
|
106
|
+
criteria.paginate(:per_page => 3).total_pages.should == 2
|
107
|
+
criteria.paginate(:per_page => 10).total_pages.should == 1
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should return per_page" do
|
111
|
+
criteria.paginate(:per_page => 1).per_page.should == 1
|
112
|
+
criteria.paginate(:per_page => 5).per_page.should == 5
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "#current_page" do
|
116
|
+
it "should return current_page" do
|
117
|
+
criteria.paginate(:page => 1).current_page.should == 1
|
118
|
+
criteria.paginate(:page => 3).current_page.should == 3
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should be casted to PageNumber" do
|
122
|
+
page = criteria.paginate(:page => 1).current_page
|
123
|
+
(page.instance_of? WillPaginate::PageNumber).should be
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should return offset" do
|
128
|
+
criteria.paginate(:page => 1).offset.should == 0
|
129
|
+
criteria.paginate(:page => 2, :per_page => 5).offset.should == 5
|
130
|
+
criteria.paginate(:page => 3, :per_page => 10).offset.should == 20
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should not pollute plain mongoid criterias" do
|
134
|
+
%w(total_entries total_pages current_page).each do |method|
|
135
|
+
criteria.should_not respond_to(method)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end if mongoid_loaded
|
data/spec/fixtures/admin.rb
CHANGED
data/spec/fixtures/developer.rb
CHANGED
@@ -1,12 +1,5 @@
|
|
1
1
|
class Developer < User
|
2
|
-
has_and_belongs_to_many :projects, :
|
3
|
-
|
4
|
-
def self.with_poor_ones(&block)
|
5
|
-
options = { :conditions => ['salary <= ?', 80000], :order => 'salary' }
|
6
|
-
with_scope({ :find => options }, :overwrite) do
|
7
|
-
yield
|
8
|
-
end
|
9
|
-
end
|
2
|
+
has_and_belongs_to_many :projects, :join_table => 'developers_projects'
|
10
3
|
|
11
4
|
scope :poor, lambda {
|
12
5
|
where(['salary <= ?', 80000]).order('salary')
|
data/spec/fixtures/project.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Project < ActiveRecord::Base
|
2
|
-
has_and_belongs_to_many :developers, :
|
2
|
+
has_and_belongs_to_many :developers, :join_table => 'developers_projects'
|
3
3
|
|
4
4
|
has_many :topics
|
5
5
|
# :finder_sql => 'SELECT * FROM topics WHERE (topics.project_id = #{id})',
|
data/spec/page_number_spec.rb
CHANGED
@@ -3,23 +3,41 @@ require 'will_paginate/page_number'
|
|
3
3
|
|
4
4
|
describe WillPaginate::PageNumber do
|
5
5
|
describe "valid" do
|
6
|
-
|
6
|
+
def num
|
7
|
+
WillPaginate::PageNumber.new('12', 'page')
|
8
|
+
end
|
9
|
+
|
10
|
+
it "== 12" do
|
11
|
+
num.should eq(12)
|
12
|
+
end
|
7
13
|
|
8
|
-
it
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
it
|
13
|
-
|
14
|
-
|
14
|
+
it "inspects to 'page 12'" do
|
15
|
+
num.inspect.should eq('page 12')
|
16
|
+
end
|
17
|
+
|
18
|
+
it "is a PageNumber" do
|
19
|
+
(num.instance_of? WillPaginate::PageNumber).should be
|
20
|
+
end
|
21
|
+
|
22
|
+
it "is a kind of Numeric" do
|
23
|
+
(num.is_a? Numeric).should be
|
24
|
+
end
|
25
|
+
|
26
|
+
it "is a kind of Fixnum" do
|
27
|
+
(num.is_a? Fixnum).should be
|
28
|
+
end
|
29
|
+
|
30
|
+
it "isn't directly a Fixnum" do
|
31
|
+
(num.instance_of? Fixnum).should_not be
|
32
|
+
end
|
15
33
|
|
16
34
|
it "passes the PageNumber=== type check" do |variable|
|
17
|
-
(WillPaginate::PageNumber ===
|
35
|
+
(WillPaginate::PageNumber === num).should be
|
18
36
|
end
|
19
37
|
|
20
38
|
it "passes the Numeric=== type check" do |variable|
|
21
|
-
(Numeric ===
|
22
|
-
(Fixnum ===
|
39
|
+
(Numeric === num).should be
|
40
|
+
(Fixnum === num).should be
|
23
41
|
end
|
24
42
|
end
|
25
43
|
|
data/spec/spec_helper.rb
CHANGED
@@ -19,8 +19,28 @@ RSpec.configure do |config|
|
|
19
19
|
def have_deprecation(msg)
|
20
20
|
DeprecationMatcher.new(msg)
|
21
21
|
end
|
22
|
+
|
23
|
+
def run_queries(num)
|
24
|
+
QueryCountMatcher.new(num)
|
25
|
+
end
|
26
|
+
|
27
|
+
def ignore_deprecation
|
28
|
+
ActiveSupport::Deprecation.silence { yield }
|
29
|
+
end
|
30
|
+
|
31
|
+
def show_queries(&block)
|
32
|
+
counter = QueryCountMatcher.new(nil)
|
33
|
+
counter.run block
|
34
|
+
ensure
|
35
|
+
queries = counter.performed_queries
|
36
|
+
if queries.any?
|
37
|
+
puts queries
|
38
|
+
else
|
39
|
+
puts "no queries"
|
40
|
+
end
|
41
|
+
end
|
22
42
|
}
|
23
|
-
|
43
|
+
|
24
44
|
config.mock_with :mocha
|
25
45
|
config.backtrace_clean_patterns << /view_example_group/
|
26
46
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'spec_helper'
|
2
3
|
require 'active_support/rescuable' # needed for Ruby 1.9.1
|
3
4
|
require 'action_controller'
|
@@ -20,6 +21,7 @@ describe WillPaginate::ActionView do
|
|
20
21
|
|
21
22
|
before(:all) do
|
22
23
|
I18n.load_path.concat WillPaginate::I18n.load_path
|
24
|
+
I18n.enforce_available_locales = false
|
23
25
|
end
|
24
26
|
|
25
27
|
before(:each) do
|
@@ -48,12 +50,14 @@ describe WillPaginate::ActionView do
|
|
48
50
|
paginate do |pagination|
|
49
51
|
assert_select 'a[href]', 3 do |elements|
|
50
52
|
validate_page_numbers [2,3,2], elements
|
51
|
-
|
53
|
+
text(elements[2]).should == 'Next →'
|
54
|
+
end
|
55
|
+
assert_select 'span', 1 do |spans|
|
56
|
+
spans[0]['class'].should == 'previous_page disabled'
|
57
|
+
text(spans[0]).should == '← Previous'
|
52
58
|
end
|
53
|
-
assert_select 'span', 1
|
54
|
-
assert_select 'span.disabled:first-child', '← Previous'
|
55
59
|
assert_select 'em.current', '1'
|
56
|
-
pagination.
|
60
|
+
text(pagination[0]).should == '← Previous 1 2 3 Next →'
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
@@ -75,15 +79,12 @@ describe WillPaginate::ActionView do
|
|
75
79
|
assert_select 'a[href]', 4 do |elements|
|
76
80
|
validate_page_numbers [1,1,3,3], elements
|
77
81
|
# test rel attribute values:
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
assert_select elements.last, 'a', "Next" do |link|
|
85
|
-
link.first['rel'].should == 'next'
|
86
|
-
end
|
82
|
+
text(elements[0]).should == 'Prev'
|
83
|
+
elements[0]['rel'].should == 'prev start'
|
84
|
+
text(elements[1]).should == '1'
|
85
|
+
elements[1]['rel'].should == 'prev start'
|
86
|
+
text(elements[3]).should == 'Next'
|
87
|
+
elements[3]['rel'].should == 'next'
|
87
88
|
end
|
88
89
|
assert_select '.current', '2'
|
89
90
|
end
|
@@ -126,8 +127,8 @@ describe WillPaginate::ActionView do
|
|
126
127
|
<a href="/foo/bar?page=2" class="next_page" rel="next">Next →</a></div>
|
127
128
|
HTML
|
128
129
|
expected.strip!.gsub!(/\s{2,}/, ' ')
|
129
|
-
expected_dom =
|
130
|
-
|
130
|
+
expected_dom = parse_html_document(expected).root
|
131
|
+
|
131
132
|
html_document.root.should == expected_dom
|
132
133
|
end
|
133
134
|
|
@@ -137,7 +138,8 @@ describe WillPaginate::ActionView do
|
|
137
138
|
|
138
139
|
assert_select 'a[href]', 1 do |links|
|
139
140
|
query = links.first['href'].split('?', 2)[1]
|
140
|
-
query.
|
141
|
+
parts = query.gsub('&', '&').split('&').sort
|
142
|
+
parts.should == %w(page=2 tag=%3Cbr%3E)
|
141
143
|
end
|
142
144
|
end
|
143
145
|
|
@@ -355,6 +357,11 @@ describe WillPaginate::ActionView do
|
|
355
357
|
I18n.available_locales # triggers loading existing translations
|
356
358
|
I18n.backend.store_translations(:en, data)
|
357
359
|
end
|
360
|
+
|
361
|
+
# Normalizes differences between HTML::Document and Nokogiri::HTML
|
362
|
+
def text(node)
|
363
|
+
node.inner_text.gsub('→', '→').gsub('←', '←')
|
364
|
+
end
|
358
365
|
end
|
359
366
|
|
360
367
|
class AdditionalLinkAttributesRenderer < WillPaginate::ActionView::LinkRenderer
|
@@ -376,7 +383,7 @@ class DummyController
|
|
376
383
|
include Routes.url_helpers
|
377
384
|
|
378
385
|
def initialize
|
379
|
-
@request = DummyRequest.new
|
386
|
+
@request = DummyRequest.new(self)
|
380
387
|
end
|
381
388
|
|
382
389
|
def params
|
@@ -399,12 +406,17 @@ class DummyRequest
|
|
399
406
|
attr_accessor :symbolized_path_parameters
|
400
407
|
alias :path_parameters :symbolized_path_parameters
|
401
408
|
|
402
|
-
def initialize
|
409
|
+
def initialize(controller)
|
410
|
+
@controller = controller
|
403
411
|
@get = true
|
404
412
|
@params = {}
|
405
413
|
@symbolized_path_parameters = { :controller => 'foo', :action => 'bar' }
|
406
414
|
end
|
407
|
-
|
415
|
+
|
416
|
+
def routes
|
417
|
+
@controller._routes
|
418
|
+
end
|
419
|
+
|
408
420
|
def get?
|
409
421
|
@get
|
410
422
|
end
|
@@ -9,16 +9,24 @@ rescue LoadError
|
|
9
9
|
ensure
|
10
10
|
$stderr = STDERR
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
|
+
begin
|
14
|
+
require 'rails/dom/testing/assertions'
|
15
|
+
rescue LoadError
|
16
|
+
require 'action_dispatch/testing/assertions'
|
17
|
+
end
|
13
18
|
require 'will_paginate/array'
|
14
19
|
|
15
20
|
module ViewExampleGroup
|
16
21
|
|
17
|
-
|
22
|
+
if defined?(Rails::Dom::Testing::Assertions)
|
23
|
+
include Rails::Dom::Testing::Assertions::SelectorAssertions
|
24
|
+
else
|
25
|
+
include ActionDispatch::Assertions::SelectorAssertions
|
26
|
+
end
|
18
27
|
include MiniTest::Assertions if defined? MiniTest
|
19
28
|
|
20
29
|
def assert(value, message)
|
21
|
-
message = message.call if message.respond_to?(:call)
|
22
30
|
raise message unless value
|
23
31
|
end
|
24
32
|
|
@@ -40,11 +48,23 @@ module ViewExampleGroup
|
|
40
48
|
|
41
49
|
@render_output
|
42
50
|
end
|
43
|
-
|
51
|
+
|
52
|
+
def parse_html_document(html)
|
53
|
+
@html_document ||= if defined?(Rails::Dom::Testing::Assertions)
|
54
|
+
Nokogiri::HTML::Document.parse(html)
|
55
|
+
else
|
56
|
+
HTML::Document.new(html, true, false)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
44
60
|
def html_document
|
45
|
-
@html_document ||=
|
61
|
+
@html_document ||= parse_html_document(@render_output)
|
46
62
|
end
|
47
|
-
|
63
|
+
|
64
|
+
def document_root_element
|
65
|
+
html_document.root
|
66
|
+
end
|
67
|
+
|
48
68
|
def response_from_page_or_rjs
|
49
69
|
html_document.root
|
50
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: will_paginate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mislav Marohnić
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: will_paginate provides a simple API for performing paginated queries
|
14
14
|
with Active Record, DataMapper and Sequel, and includes helpers for rendering pagination
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- lib/will_paginate/deprecation.rb
|
32
32
|
- lib/will_paginate/i18n.rb
|
33
33
|
- lib/will_paginate/locale/en.yml
|
34
|
+
- lib/will_paginate/mongoid.rb
|
34
35
|
- lib/will_paginate/page_number.rb
|
35
36
|
- lib/will_paginate/per_page.rb
|
36
37
|
- lib/will_paginate/railtie.rb
|
@@ -51,6 +52,7 @@ files:
|
|
51
52
|
- spec/finders/activerecord_test_connector.rb
|
52
53
|
- spec/finders/data_mapper_spec.rb
|
53
54
|
- spec/finders/data_mapper_test_connector.rb
|
55
|
+
- spec/finders/mongoid_spec.rb
|
54
56
|
- spec/finders/sequel_spec.rb
|
55
57
|
- spec/finders/sequel_test_connector.rb
|
56
58
|
- spec/fixtures/admin.rb
|
@@ -98,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
100
|
version: '0'
|
99
101
|
requirements: []
|
100
102
|
rubyforge_project:
|
101
|
-
rubygems_version: 2.
|
103
|
+
rubygems_version: 2.5.1
|
102
104
|
signing_key:
|
103
105
|
specification_version: 4
|
104
106
|
summary: Pagination plugin for web frameworks and other apps
|