will_paginate 3.0.12 → 3.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70f44a0574db4ec78958041e6299452b35c6e4bf
4
- data.tar.gz: 165a02112371349602d70cca5ce0d5c583e802bb
3
+ metadata.gz: b3445d24047dd419909e314bc5f1b9eb3fdb7f73
4
+ data.tar.gz: 2e6ce5705bc95b523bdd8a7ffa9d8e0b35acc36f
5
5
  SHA512:
6
- metadata.gz: 3e994750be209050697215a4f92b952760ec1be7e1d1bb05afb5fed986e3a3adf6e7417804bc77eeb730a67958d0bb8e02409a072b2b59438bf6e86d4f07d16c
7
- data.tar.gz: f7730e8cdf54c6e679cd74ef71c909768927be6ea5e38be32396e5df13fb954787b22b3faa323ef634b857dca1d7bdaa6ae7bab835736019457450cd7d694d78
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.uniqpath.com/will_paginate/
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, :wp_count_options
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
- count_options = options.delete(:count)
165
- options.delete(:page)
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
- all(options).page(pagenum)
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
@@ -18,6 +18,8 @@ module WillPaginate
18
18
  require 'will_paginate/view_helpers/action_view'
19
19
  end
20
20
 
21
+ self.class.add_locale_path config
22
+
21
23
  # early access to ViewHelpers.pagination_options
22
24
  require 'will_paginate/view_helpers'
23
25
  end
@@ -29,6 +31,10 @@ module WillPaginate
29
31
  ActionController::Base.extend ControllerRescuePatch
30
32
  end
31
33
 
34
+ def self.add_locale_path(config)
35
+ config.i18n.load_path.unshift(*WillPaginate::I18n.load_path)
36
+ end
37
+
32
38
  # Extending the exception handler middleware so it properly detects
33
39
  # WillPaginate::InvalidPage regardless of it being a tag module.
34
40
  module ShowExceptionsPatch
@@ -38,15 +44,9 @@ module WillPaginate
38
44
  alias_method :status_code, :status_code_with_paginate
39
45
  end
40
46
  def status_code_with_paginate(exception = @exception)
41
- actual_exception = if exception.respond_to?(:cause)
42
- exception.cause
43
- elsif exception.respond_to?(:original_exception)
44
- exception.original_exception
45
- else
46
- exception
47
- end
48
-
49
- if actual_exception.is_a?(WillPaginate::InvalidPage)
47
+ if exception.is_a?(WillPaginate::InvalidPage) or
48
+ (exception.respond_to?(:original_exception) &&
49
+ exception.original_exception.is_a?(WillPaginate::InvalidPage))
50
50
  Rack::Utils.status_code(:not_found)
51
51
  else
52
52
  original_method = method(:status_code_without_paginate)
@@ -69,7 +69,3 @@ module WillPaginate
69
69
  end
70
70
  end
71
71
  end
72
-
73
- ActiveSupport.on_load :i18n do
74
- I18n.load_path.concat(WillPaginate::I18n.load_path)
75
- end
@@ -1,8 +1,8 @@
1
1
  module WillPaginate #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
- MINOR = 0
5
- TINY = 12
4
+ MINOR = 1
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -99,8 +99,6 @@ module WillPaginate
99
99
  class LinkRenderer < ViewHelpers::LinkRenderer
100
100
  protected
101
101
 
102
- GET_PARAMS_BLACKLIST = [:script_name, :original_script_name]
103
-
104
102
  def default_url_params
105
103
  {}
106
104
  end
@@ -120,7 +118,7 @@ module WillPaginate
120
118
 
121
119
  def merge_get_params(url_params)
122
120
  if @template.respond_to? :request and @template.request and @template.request.get?
123
- symbolized_update(url_params, @template.params, GET_PARAMS_BLACKLIST)
121
+ symbolized_update(url_params, @template.params)
124
122
  end
125
123
  url_params
126
124
  end
@@ -108,17 +108,17 @@ module WillPaginate
108
108
 
109
109
  def rel_value(page)
110
110
  case page
111
- when @collection.current_page - 1; 'prev'
111
+ when @collection.current_page - 1; 'prev' + (page == 1 ? ' start' : '')
112
112
  when @collection.current_page + 1; 'next'
113
+ when 1; 'start'
113
114
  end
114
115
  end
115
116
 
116
- def symbolized_update(target, other, blacklist = nil)
117
+ def symbolized_update(target, other)
117
118
  other.each do |key, value|
118
119
  key = key.to_sym
119
120
  existing = target[key]
120
- next if blacklist && blacklist.include?(key)
121
-
121
+
122
122
  if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?)
123
123
  symbolized_update(existing || (target[key] = {}), value)
124
124
  else
@@ -30,7 +30,7 @@ module WillPaginate
30
30
  window_from = current_page - inner_window
31
31
  window_to = current_page + inner_window
32
32
 
33
- # adjust lower or upper limit if either is out of bounds
33
+ # adjust lower or upper limit if other is out of bounds
34
34
  if window_to > total_pages
35
35
  window_from -= window_to - total_pages
36
36
  window_to = total_pages
@@ -3,27 +3,23 @@ sqlite3:
3
3
  adapter: sqlite3
4
4
  timeout: 500
5
5
 
6
- mysql: &mysql
6
+ mysql:
7
7
  adapter: mysql
8
8
  database: will_paginate
9
9
  username:
10
10
  encoding: utf8
11
- <% if File.exist?("/var/run/mysql5/mysqld.sock") %>
12
- host: localhost
13
- socket: /var/run/mysql5/mysqld.sock
14
- <% elsif File.exist? "/tmp/mysql.sock" %>
15
- host: localhost
16
- socket: /tmp/mysql.sock
17
- <% else %>
18
- host: 127.0.0.1
19
- <% end %>
11
+ socket: <%= ENV["BOXEN_MYSQL_SOCKET"] %>
20
12
 
21
13
  mysql2:
22
- <<: *mysql
23
14
  adapter: mysql2
15
+ database: will_paginate
16
+ username:
17
+ encoding: utf8
18
+ socket: <%= ENV["BOXEN_MYSQL_SOCKET"] %>
24
19
 
25
20
  postgres:
26
21
  adapter: postgresql
27
22
  database: will_paginate
28
23
  username: <%= "postgres" if ENV["TRAVIS"] %>
29
24
  min_messages: warning
25
+ port: <%= ENV["BOXEN_POSTGRESQL_PORT"] %>
@@ -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 == topics(:ar)
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, :order => 'projects.name')
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
@@ -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.paginate(:page => 1, :joins => join_sql, :conditions => 'project_id = 1')
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, :joins => join_sql,
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.paginate(:page => 1, :per_page => 10,
441
- :group => 'salary', :select => 'salary', :order => 'salary').to_a
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
@@ -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
@@ -1,13 +1,6 @@
1
1
  class Developer < User
2
2
  has_and_belongs_to_many :projects, :join_table => 'developers_projects'
3
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
10
-
11
4
  scope :poor, lambda {
12
5
  where(['salary <= ?', 80000]).order('salary')
13
6
  }
@@ -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
@@ -80,9 +80,9 @@ describe WillPaginate::ActionView do
80
80
  validate_page_numbers [1,1,3,3], elements
81
81
  # test rel attribute values:
82
82
  text(elements[0]).should == 'Prev'
83
- elements[0]['rel'].should == 'prev'
83
+ elements[0]['rel'].should == 'prev start'
84
84
  text(elements[1]).should == '1'
85
- elements[1]['rel'].should == 'prev'
85
+ elements[1]['rel'].should == 'prev start'
86
86
  text(elements[3]).should == 'Next'
87
87
  elements[3]['rel'].should == 'next'
88
88
  end
@@ -201,13 +201,6 @@ describe WillPaginate::ActionView do
201
201
  assert_no_links_match /99/
202
202
  assert_no_links_match /ftp/
203
203
  end
204
-
205
- it "doesn't allow tampering with script_name" do
206
- request.params :script_name => 'p0wned', :original_script_name => 'p0wned'
207
- paginate
208
- assert_links_match %r{^/foo/bar}
209
- assert_no_links_match /p0wned/
210
- end
211
204
 
212
205
  it "should not preserve parameters on POST" do
213
206
  request.post
@@ -257,7 +250,7 @@ describe WillPaginate::ActionView do
257
250
  end
258
251
 
259
252
  it "should paginate with custom route page parameter" do
260
- request.symbolized_path_parameters.update :controller => 'dummy', :action => 'index'
253
+ request.symbolized_path_parameters.update :controller => 'dummy', :action => nil
261
254
  paginate :per_page => 2 do
262
255
  assert_select 'a[href]', 6 do |links|
263
256
  assert_links_match %r{/page/(\d+)$}, links, [2, 3, 4, 5, 6, 2]
@@ -275,7 +268,7 @@ describe WillPaginate::ActionView do
275
268
  end
276
269
 
277
270
  it "should paginate with custom route and first page number implicit" do
278
- request.symbolized_path_parameters.update :controller => 'ibocorp', :action => 'index'
271
+ request.symbolized_path_parameters.update :controller => 'ibocorp', :action => nil
279
272
  paginate :page => 2, :per_page => 2 do
280
273
  assert_select 'a[href]', 7 do |links|
281
274
  assert_links_match %r{/ibocorp(?:/(\d+))?$}, links, [nil, nil, 3, 4, 5, 6, 3]
@@ -27,7 +27,6 @@ module ViewExampleGroup
27
27
  include MiniTest::Assertions if defined? MiniTest
28
28
 
29
29
  def assert(value, message)
30
- message = message.call if message.respond_to?(:call)
31
30
  raise message unless value
32
31
  end
33
32
 
@@ -144,4 +143,4 @@ module HTML
144
143
  childless?? '' : super
145
144
  end
146
145
  end
147
- end if defined?(HTML)
146
+ 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.12
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: 2016-10-15 00:00:00.000000000 Z
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