will_paginate 3.0.4 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +3 -3
- data/lib/will_paginate/active_record.rb +17 -12
- 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/lib/will_paginate/view_helpers/action_view.rb +2 -1
- data/lib/will_paginate/view_helpers.rb +3 -1
- data/spec/database.yml +4 -1
- data/spec/fake_rubygems.rb +18 -0
- data/spec/finders/active_record_spec.rb +22 -160
- data/spec/finders/activerecord_test_connector.rb +23 -6
- data/spec/finders/data_mapper_spec.rb +14 -1
- data/spec/finders/mongoid_spec.rb +140 -0
- data/spec/fixtures/admin.rb +1 -1
- data/spec/fixtures/developer.rb +4 -8
- data/spec/fixtures/project.rb +2 -4
- data/spec/fixtures/reply.rb +4 -5
- data/spec/fixtures/topic.rb +4 -3
- data/spec/matchers/deprecation_matcher.rb +27 -0
- data/spec/matchers/phrase_matcher.rb +19 -0
- data/spec/matchers/query_count_matcher.rb +36 -0
- data/spec/page_number_spec.rb +29 -11
- data/spec/spec_helper.rb +23 -48
- data/spec/view_helpers/action_view_spec.rb +59 -29
- data/spec/view_helpers/base_spec.rb +13 -0
- data/spec/view_helpers/view_example_group.rb +30 -5
- metadata +21 -19
- data/Rakefile +0 -25
- data/spec/ci.rb +0 -29
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b3445d24047dd419909e314bc5f1b9eb3fdb7f73
|
4
|
+
data.tar.gz: 2e6ce5705bc95b523bdd8a7ffa9d8e0b35acc36f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bdc9b0d1f20379dc1df3dc503796834b329976c90a63f4e0f14b77152aef8721bea240f850d0abc0c0171a3e1ad10d5c88a354136e47d4bb933b29a205311d7d
|
7
|
+
data.tar.gz: c35edfea8e81e70fef045d9f1c3c32bea89723a1bd74a6c2edb438c62fc54c3356eb4704329c42b35760178c71a505ee0d86c0109a6f7cd3fdacd5276af4eb74
|
data/README.md
CHANGED
@@ -5,8 +5,8 @@ will_paginate is a pagination library that integrates with Ruby on Rails, Sinatr
|
|
5
5
|
Installation:
|
6
6
|
|
7
7
|
``` ruby
|
8
|
-
## Gemfile for Rails 3
|
9
|
-
gem 'will_paginate', '~> 3.0'
|
8
|
+
## Gemfile for Rails 3+, Sinatra, and Merb
|
9
|
+
gem 'will_paginate', '~> 3.0.6'
|
10
10
|
```
|
11
11
|
|
12
12
|
See [installation instructions][install] on the wiki for more info.
|
@@ -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/
|
@@ -21,7 +21,7 @@ module WillPaginate
|
|
21
21
|
include WillPaginate::CollectionMethods
|
22
22
|
|
23
23
|
attr_accessor :current_page
|
24
|
-
attr_writer :total_entries
|
24
|
+
attr_writer :total_entries
|
25
25
|
|
26
26
|
def per_page(value = nil)
|
27
27
|
if value.nil? then limit_value
|
@@ -78,16 +78,15 @@ module WillPaginate
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
-
def count
|
81
|
+
def count(*args)
|
82
82
|
if limit_value
|
83
|
-
excluded = [:order, :limit, :offset]
|
83
|
+
excluded = [:order, :limit, :offset, :reorder]
|
84
84
|
excluded << :includes unless eager_loading?
|
85
85
|
rel = self.except(*excluded)
|
86
|
-
|
87
|
-
rel
|
88
|
-
rel.count
|
86
|
+
column_name = (select_for_count(rel) || :all)
|
87
|
+
rel.count(column_name)
|
89
88
|
else
|
90
|
-
super
|
89
|
+
super(*args)
|
91
90
|
end
|
92
91
|
end
|
93
92
|
|
@@ -135,24 +134,30 @@ module WillPaginate
|
|
135
134
|
def copy_will_paginate_data(other)
|
136
135
|
other.current_page = current_page unless other.current_page
|
137
136
|
other.total_entries = nil if defined? @total_entries_queried
|
138
|
-
other.wp_count_options = @wp_count_options if defined? @wp_count_options
|
139
137
|
other
|
140
138
|
end
|
139
|
+
|
140
|
+
def select_for_count(rel)
|
141
|
+
if rel.select_values.present?
|
142
|
+
select = rel.select_values.join(", ")
|
143
|
+
select if select !~ /[,*]/
|
144
|
+
end
|
145
|
+
end
|
141
146
|
end
|
142
147
|
|
143
148
|
module Pagination
|
144
149
|
def paginate(options)
|
145
150
|
options = options.dup
|
146
151
|
pagenum = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
|
152
|
+
options.delete(:page)
|
147
153
|
per_page = options.delete(:per_page) || self.per_page
|
148
154
|
total = options.delete(:total_entries)
|
149
155
|
|
150
|
-
|
151
|
-
|
156
|
+
if options.any?
|
157
|
+
raise ArgumentError, "unsupported parameters: %p" % options.keys
|
158
|
+
end
|
152
159
|
|
153
160
|
rel = limit(per_page.to_i).page(pagenum)
|
154
|
-
rel = rel.apply_finder_options(options) if options.any?
|
155
|
-
rel.wp_count_options = count_options if count_options
|
156
161
|
rel.total_entries = total.to_i unless total.blank?
|
157
162
|
rel
|
158
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) &&
|
@@ -30,7 +30,7 @@ module WillPaginate
|
|
30
30
|
options = options.symbolize_keys
|
31
31
|
options[:renderer] ||= LinkRenderer
|
32
32
|
|
33
|
-
super(collection, options)
|
33
|
+
super(collection, options)
|
34
34
|
end
|
35
35
|
|
36
36
|
def page_entries_info(collection = nil, options = {}) #:nodoc:
|
@@ -106,6 +106,7 @@ module WillPaginate
|
|
106
106
|
def url(page)
|
107
107
|
@base_url_params ||= begin
|
108
108
|
url_params = merge_get_params(default_url_params)
|
109
|
+
url_params[:only_path] = true
|
109
110
|
merge_optional_params(url_params)
|
110
111
|
end
|
111
112
|
|
@@ -91,7 +91,9 @@ module WillPaginate
|
|
91
91
|
end
|
92
92
|
# render HTML for pagination
|
93
93
|
renderer.prepare collection, options, self
|
94
|
-
renderer.to_html
|
94
|
+
output = renderer.to_html
|
95
|
+
output = output.html_safe if output.respond_to?(:html_safe)
|
96
|
+
output
|
95
97
|
end
|
96
98
|
|
97
99
|
# Renders a message containing number of displayed vs. total entries.
|
data/spec/database.yml
CHANGED
@@ -8,15 +8,18 @@ mysql:
|
|
8
8
|
database: will_paginate
|
9
9
|
username:
|
10
10
|
encoding: utf8
|
11
|
+
socket: <%= ENV["BOXEN_MYSQL_SOCKET"] %>
|
11
12
|
|
12
13
|
mysql2:
|
13
14
|
adapter: mysql2
|
14
15
|
database: will_paginate
|
15
16
|
username:
|
16
17
|
encoding: utf8
|
18
|
+
socket: <%= ENV["BOXEN_MYSQL_SOCKET"] %>
|
17
19
|
|
18
20
|
postgres:
|
19
21
|
adapter: postgresql
|
20
22
|
database: will_paginate
|
21
|
-
username: postgres
|
23
|
+
username: <%= "postgres" if ENV["TRAVIS"] %>
|
22
24
|
min_messages: warning
|
25
|
+
port: <%= ENV["BOXEN_POSTGRESQL_PORT"] %>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Makes the test suite compatible with Bundler standalone mode (used in CI)
|
2
|
+
# because Active Record uses `gem` for loading adapters.
|
3
|
+
Kernel.module_eval do
|
4
|
+
|
5
|
+
remove_method :gem if 'method' == defined? gem
|
6
|
+
|
7
|
+
def gem(*args)
|
8
|
+
return if $VERBOSE.nil?
|
9
|
+
$stderr << "warning: gem(#{args.map {|o| o.inspect }.join(', ')}) ignored"
|
10
|
+
$stderr << "; called from:\n " << caller[0,5].join("\n ") if $DEBUG
|
11
|
+
$stderr << "\n"
|
12
|
+
end
|
13
|
+
|
14
|
+
private :gem
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
$" << "rubygems.rb"
|
@@ -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 {
|
@@ -191,6 +175,7 @@ describe WillPaginate::ActiveRecord do
|
|
191
175
|
it "keeps :include for count when they are referenced in :conditions" do
|
192
176
|
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
193
177
|
with_condition = developers.where('projects.id > 1')
|
178
|
+
with_condition = with_condition.references(:projects) if with_condition.respond_to?(:references)
|
194
179
|
with_condition.total_entries.should == 1
|
195
180
|
|
196
181
|
$query_sql.last.should =~ /\bJOIN\b/
|
@@ -199,6 +184,15 @@ describe WillPaginate::ActiveRecord do
|
|
199
184
|
it "should count with group" do
|
200
185
|
Developer.group(:salary).page(1).total_entries.should == 4
|
201
186
|
end
|
187
|
+
|
188
|
+
it "should count with select" do
|
189
|
+
Topic.select('title, content').page(1).total_entries.should == 4
|
190
|
+
end
|
191
|
+
|
192
|
+
it "removes :reorder for count with group" do
|
193
|
+
Project.group(:id).reorder(:id).page(1).total_entries
|
194
|
+
$query_sql.last.should_not =~ /\ORDER\b/
|
195
|
+
end
|
202
196
|
|
203
197
|
it "should not have zero total_pages when the result set is empty" do
|
204
198
|
Developer.where("1 = 2").page(1).total_pages.should == 1
|
@@ -240,10 +234,11 @@ describe WillPaginate::ActiveRecord do
|
|
240
234
|
end
|
241
235
|
|
242
236
|
it "should strip the order when counting" do
|
237
|
+
expected = topics(:ar)
|
243
238
|
lambda {
|
244
239
|
sql = "select id, title, content from topics order by topics.title"
|
245
240
|
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
|
246
|
-
topics.first.should ==
|
241
|
+
topics.first.should == expected
|
247
242
|
}.should run_queries(2)
|
248
243
|
|
249
244
|
$query_sql.last.should include('COUNT')
|
@@ -285,69 +280,7 @@ describe WillPaginate::ActiveRecord do
|
|
285
280
|
}.should run_queries(2)
|
286
281
|
end
|
287
282
|
|
288
|
-
it "should paginate with :order" do
|
289
|
-
result = Topic.paginate :page => 1, :order => 'created_at DESC'
|
290
|
-
result.should == topics(:futurama, :harvey_birdman, :rails, :ar).reverse
|
291
|
-
result.total_pages.should == 1
|
292
|
-
end
|
293
|
-
|
294
|
-
it "should paginate with :conditions" do
|
295
|
-
result = Topic.paginate :page => 1, :order => 'id ASC',
|
296
|
-
:conditions => ["created_at > ?", 30.minutes.ago]
|
297
|
-
result.should == topics(:rails, :ar)
|
298
|
-
result.total_pages.should == 1
|
299
|
-
end
|
300
|
-
|
301
|
-
it "should paginate with :include and :conditions" do
|
302
|
-
result = Topic.paginate \
|
303
|
-
:page => 1,
|
304
|
-
:include => :replies,
|
305
|
-
:conditions => "replies.content LIKE 'Bird%' ",
|
306
|
-
:per_page => 10
|
307
|
-
|
308
|
-
expected = Topic.find :all,
|
309
|
-
:include => 'replies',
|
310
|
-
:conditions => "replies.content LIKE 'Bird%' ",
|
311
|
-
:limit => 10
|
312
|
-
|
313
|
-
result.should == expected
|
314
|
-
result.total_entries.should == 1
|
315
|
-
end
|
316
|
-
|
317
|
-
it "should paginate with :include and :order" do
|
318
|
-
result = nil
|
319
|
-
lambda {
|
320
|
-
result = Topic.paginate(:page => 1, :include => :replies, :per_page => 10,
|
321
|
-
:order => 'replies.created_at asc, topics.created_at asc').to_a
|
322
|
-
}.should run_queries(2)
|
323
|
-
|
324
|
-
expected = Topic.find :all,
|
325
|
-
:include => 'replies',
|
326
|
-
:order => 'replies.created_at asc, topics.created_at asc',
|
327
|
-
:limit => 10
|
328
|
-
|
329
|
-
result.should == expected
|
330
|
-
result.total_entries.should == 4
|
331
|
-
end
|
332
|
-
|
333
283
|
describe "associations" do
|
334
|
-
it "should paginate with include" do
|
335
|
-
project = projects(:active_record)
|
336
|
-
|
337
|
-
result = project.topics.paginate \
|
338
|
-
:page => 1,
|
339
|
-
:include => :replies,
|
340
|
-
:conditions => ["replies.content LIKE ?", 'Nice%'],
|
341
|
-
:per_page => 10
|
342
|
-
|
343
|
-
expected = Topic.find :all,
|
344
|
-
:include => 'replies',
|
345
|
-
:conditions => ["project_id = ? AND replies.content LIKE ?", project.id, 'Nice%'],
|
346
|
-
:limit => 10
|
347
|
-
|
348
|
-
result.should == expected
|
349
|
-
end
|
350
|
-
|
351
284
|
it "should paginate" do
|
352
285
|
dhh = users(:david)
|
353
286
|
expected_name_ordered = projects(:action_controller, :active_record)
|
@@ -355,8 +288,10 @@ describe WillPaginate::ActiveRecord do
|
|
355
288
|
|
356
289
|
lambda {
|
357
290
|
# with association-specified order
|
358
|
-
result = ignore_deprecation {
|
359
|
-
|
291
|
+
result = ignore_deprecation {
|
292
|
+
dhh.projects.includes(:topics).order('projects.name').paginate(:page => 1)
|
293
|
+
}
|
294
|
+
result.to_a.should == expected_name_ordered
|
360
295
|
result.total_entries.should == 2
|
361
296
|
}.should run_queries(2)
|
362
297
|
|
@@ -366,7 +301,7 @@ describe WillPaginate::ActiveRecord do
|
|
366
301
|
result.total_entries.should == 2
|
367
302
|
|
368
303
|
lambda {
|
369
|
-
dhh.projects.
|
304
|
+
dhh.projects.order('projects.id').limit(4).to_a
|
370
305
|
}.should_not raise_error
|
371
306
|
|
372
307
|
result = dhh.projects.paginate(:page => 1, :per_page => 4).reorder('projects.id')
|
@@ -396,7 +331,7 @@ describe WillPaginate::ActiveRecord do
|
|
396
331
|
join_sql = 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id'
|
397
332
|
|
398
333
|
lambda {
|
399
|
-
result = Developer.
|
334
|
+
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
400
335
|
result.to_a # trigger loading of records
|
401
336
|
result.size.should == 2
|
402
337
|
developer_names = result.map(&:name)
|
@@ -406,8 +341,7 @@ describe WillPaginate::ActiveRecord do
|
|
406
341
|
|
407
342
|
lambda {
|
408
343
|
expected = result.to_a
|
409
|
-
result = Developer.paginate(:page => 1
|
410
|
-
: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)
|
411
345
|
result.should == expected
|
412
346
|
result.total_entries.should == 2
|
413
347
|
}.should run_queries(1)
|
@@ -416,8 +350,8 @@ describe WillPaginate::ActiveRecord do
|
|
416
350
|
it "should paginate with group" do
|
417
351
|
result = nil
|
418
352
|
lambda {
|
419
|
-
result = Developer.
|
420
|
-
:
|
353
|
+
result = Developer.select('salary').order('salary').group('salary').
|
354
|
+
paginate(:page => 1, :per_page => 10).to_a
|
421
355
|
}.should run_queries(1)
|
422
356
|
|
423
357
|
expected = users(:david, :jamis, :dev_10, :poor_jamis).map(&:salary).sort
|
@@ -430,12 +364,6 @@ describe WillPaginate::ActiveRecord do
|
|
430
364
|
}.should raise_error(NoMethodError)
|
431
365
|
end
|
432
366
|
|
433
|
-
it "should paginate with_scope" do
|
434
|
-
result = Developer.with_poor_ones { Developer.paginate :page => 1 }
|
435
|
-
result.size.should == 2
|
436
|
-
result.total_entries.should == 2
|
437
|
-
end
|
438
|
-
|
439
367
|
describe "scopes" do
|
440
368
|
it "should paginate" do
|
441
369
|
result = Developer.poor.paginate :page => 1, :per_page => 1
|
@@ -475,12 +403,6 @@ describe WillPaginate::ActiveRecord do
|
|
475
403
|
end
|
476
404
|
end
|
477
405
|
|
478
|
-
it "should paginate with :readonly option" do
|
479
|
-
lambda {
|
480
|
-
Developer.paginate :readonly => true, :page => 1
|
481
|
-
}.should_not raise_error
|
482
|
-
end
|
483
|
-
|
484
406
|
it "should not paginate an array of IDs" do
|
485
407
|
lambda {
|
486
408
|
Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
|
@@ -493,64 +415,4 @@ describe WillPaginate::ActiveRecord do
|
|
493
415
|
Project.page(307445734561825862)
|
494
416
|
}.should raise_error(WillPaginate::InvalidPage, "invalid offset: 9223372036854775830")
|
495
417
|
end
|
496
|
-
|
497
|
-
protected
|
498
|
-
|
499
|
-
def ignore_deprecation
|
500
|
-
ActiveSupport::Deprecation.silence { yield }
|
501
|
-
end
|
502
|
-
|
503
|
-
def run_queries(num)
|
504
|
-
QueryCountMatcher.new(num)
|
505
|
-
end
|
506
|
-
|
507
|
-
def show_queries(&block)
|
508
|
-
counter = QueryCountMatcher.new(nil)
|
509
|
-
counter.run block
|
510
|
-
ensure
|
511
|
-
queries = counter.performed_queries
|
512
|
-
if queries.any?
|
513
|
-
puts queries
|
514
|
-
else
|
515
|
-
puts "no queries"
|
516
|
-
end
|
517
|
-
end
|
518
|
-
|
519
|
-
end
|
520
|
-
|
521
|
-
class QueryCountMatcher
|
522
|
-
def initialize(num)
|
523
|
-
@expected_count = num
|
524
|
-
end
|
525
|
-
|
526
|
-
def matches?(block)
|
527
|
-
run(block)
|
528
|
-
|
529
|
-
if @expected_count.respond_to? :include?
|
530
|
-
@expected_count.include? @count
|
531
|
-
else
|
532
|
-
@count == @expected_count
|
533
|
-
end
|
534
|
-
end
|
535
|
-
|
536
|
-
def run(block)
|
537
|
-
$query_count = 0
|
538
|
-
$query_sql = []
|
539
|
-
block.call
|
540
|
-
ensure
|
541
|
-
@queries = $query_sql.dup
|
542
|
-
@count = $query_count
|
543
|
-
end
|
544
|
-
|
545
|
-
def performed_queries
|
546
|
-
@queries
|
547
|
-
end
|
548
|
-
|
549
|
-
def failure_message
|
550
|
-
"expected #{@expected_count} queries, got #{@count}\n#{@queries.join("\n")}"
|
551
|
-
end
|
552
|
-
|
553
|
-
def negative_failure_message
|
554
|
-
"expected query count not to be #{@expected_count}"
|
555
|
-
end
|
556
|
-
end
|
418
|
+
end
|
@@ -1,15 +1,28 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'active_record/fixtures'
|
3
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
|
4
17
|
|
5
18
|
$query_count = 0
|
6
19
|
$query_sql = []
|
7
20
|
|
8
21
|
ignore_sql = /
|
9
22
|
^(
|
10
|
-
PRAGMA | SHOW\ max_identifier_length |
|
23
|
+
PRAGMA | SHOW\ (max_identifier_length|search_path) |
|
11
24
|
SELECT\ (currval|CAST|@@IDENTITY|@@ROWCOUNT) |
|
12
|
-
SHOW\ (FIELDS|TABLES)
|
25
|
+
SHOW\ ((FULL\ )?FIELDS|TABLES)
|
13
26
|
)\b |
|
14
27
|
\bFROM\ (sqlite_master|pg_tables|pg_attribute)\b
|
15
28
|
/x
|
@@ -59,8 +72,9 @@ module ActiverecordTestConnector
|
|
59
72
|
|
60
73
|
def setup_connection
|
61
74
|
db = ENV['DB'].blank?? 'sqlite3' : ENV['DB']
|
62
|
-
|
63
|
-
|
75
|
+
|
76
|
+
erb = ERB.new(File.read(File.expand_path('../../database.yml', __FILE__)))
|
77
|
+
configurations = YAML.load(erb.result)
|
64
78
|
raise "no configuration for '#{db}'" unless configurations.key? db
|
65
79
|
configuration = configurations[db]
|
66
80
|
|
@@ -68,14 +82,17 @@ module ActiverecordTestConnector
|
|
68
82
|
puts "using #{configuration['adapter']} adapter"
|
69
83
|
|
70
84
|
ActiveRecord::Base.configurations = { db => configuration }
|
71
|
-
ActiveRecord::Base.establish_connection(db)
|
85
|
+
ActiveRecord::Base.establish_connection(db.to_sym)
|
72
86
|
ActiveRecord::Base.default_timezone = :utc
|
73
87
|
end
|
74
88
|
|
75
89
|
def load_schema
|
76
|
-
|
90
|
+
begin
|
91
|
+
$stdout = StringIO.new
|
77
92
|
ActiveRecord::Migration.verbose = false
|
78
93
|
load File.join(FIXTURES_PATH, 'schema.rb')
|
94
|
+
ensure
|
95
|
+
$stdout = STDOUT
|
79
96
|
end
|
80
97
|
end
|
81
98
|
|
@@ -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| }
|
@@ -90,7 +103,7 @@ describe WillPaginate::DataMapper do
|
|
90
103
|
animals = Animal.all(:limit => 2).page(1)
|
91
104
|
array = animals.to_a
|
92
105
|
array.size.should == 2
|
93
|
-
array.
|
106
|
+
array.should be_kind_of(WillPaginate::Collection)
|
94
107
|
array.current_page.should == 1
|
95
108
|
array.per_page.should == 2
|
96
109
|
end
|