will_paginate 3.1.5 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +8 -11
- data/lib/will_paginate/active_record.rb +7 -11
- data/lib/will_paginate/deprecation.rb +2 -2
- data/lib/will_paginate/i18n.rb +3 -3
- data/lib/will_paginate/locale/en.yml +4 -0
- data/lib/will_paginate/mongoid.rb +2 -0
- data/lib/will_paginate/page_number.rb +7 -11
- data/lib/will_paginate/railtie.rb +3 -4
- data/lib/will_paginate/version.rb +3 -3
- data/lib/will_paginate/view_helpers/action_view.rb +8 -4
- data/lib/will_paginate/view_helpers/hanami.rb +41 -0
- data/lib/will_paginate/view_helpers/link_renderer.rb +19 -14
- data/lib/will_paginate.rb +0 -12
- metadata +17 -48
- data/lib/will_paginate/data_mapper.rb +0 -100
- data/lib/will_paginate/view_helpers/merb.rb +0 -26
- data/spec/collection_spec.rb +0 -139
- data/spec/console +0 -12
- data/spec/console_fixtures.rb +0 -28
- data/spec/database.yml +0 -29
- data/spec/fake_rubygems.rb +0 -18
- data/spec/finders/active_record_spec.rb +0 -418
- data/spec/finders/activerecord_test_connector.rb +0 -132
- data/spec/finders/data_mapper_spec.rb +0 -116
- data/spec/finders/data_mapper_test_connector.rb +0 -54
- data/spec/finders/mongoid_spec.rb +0 -140
- data/spec/finders/sequel_spec.rb +0 -67
- data/spec/finders/sequel_test_connector.rb +0 -15
- data/spec/fixtures/admin.rb +0 -3
- data/spec/fixtures/developer.rb +0 -9
- data/spec/fixtures/developers_projects.yml +0 -13
- data/spec/fixtures/project.rb +0 -13
- data/spec/fixtures/projects.yml +0 -6
- data/spec/fixtures/replies.yml +0 -29
- data/spec/fixtures/reply.rb +0 -8
- data/spec/fixtures/schema.rb +0 -38
- data/spec/fixtures/topic.rb +0 -8
- data/spec/fixtures/topics.yml +0 -30
- data/spec/fixtures/user.rb +0 -2
- data/spec/fixtures/users.yml +0 -35
- data/spec/matchers/deprecation_matcher.rb +0 -27
- data/spec/matchers/phrase_matcher.rb +0 -19
- data/spec/matchers/query_count_matcher.rb +0 -36
- data/spec/page_number_spec.rb +0 -83
- data/spec/per_page_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -46
- data/spec/view_helpers/action_view_spec.rb +0 -468
- data/spec/view_helpers/base_spec.rb +0 -143
- data/spec/view_helpers/link_renderer_base_spec.rb +0 -87
- data/spec/view_helpers/view_example_group.rb +0 -146
data/spec/collection_spec.rb
DELETED
@@ -1,139 +0,0 @@
|
|
1
|
-
require 'will_paginate/array'
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
describe WillPaginate::Collection do
|
5
|
-
|
6
|
-
before :all do
|
7
|
-
@simple = ('a'..'e').to_a
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should be a subset of original collection" do
|
11
|
-
@simple.paginate(:page => 1, :per_page => 3).should == %w( a b c )
|
12
|
-
end
|
13
|
-
|
14
|
-
it "can be shorter than per_page if on last page" do
|
15
|
-
@simple.paginate(:page => 2, :per_page => 3).should == %w( d e )
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should include whole collection if per_page permits" do
|
19
|
-
@simple.paginate(:page => 1, :per_page => 5).should == @simple
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should be empty if out of bounds" do
|
23
|
-
@simple.paginate(:page => 2, :per_page => 5).should be_empty
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should default to 1 as current page and 30 per-page" do
|
27
|
-
result = (1..50).to_a.paginate
|
28
|
-
result.current_page.should == 1
|
29
|
-
result.size.should == 30
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should give total_entries precedence over actual size" do
|
33
|
-
%w(a b c).paginate(:total_entries => 5).total_entries.should == 5
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should be an augmented Array" do
|
37
|
-
entries = %w(a b c)
|
38
|
-
collection = create(2, 3, 10) do |pager|
|
39
|
-
pager.replace(entries).should == entries
|
40
|
-
end
|
41
|
-
|
42
|
-
collection.should == entries
|
43
|
-
for method in %w(total_pages each offset size current_page per_page total_entries)
|
44
|
-
collection.should respond_to(method)
|
45
|
-
end
|
46
|
-
collection.should be_kind_of(Array)
|
47
|
-
collection.entries.should be_instance_of(Array)
|
48
|
-
# TODO: move to another expectation:
|
49
|
-
collection.offset.should == 3
|
50
|
-
collection.total_pages.should == 4
|
51
|
-
collection.should_not be_out_of_bounds
|
52
|
-
end
|
53
|
-
|
54
|
-
describe "previous/next pages" do
|
55
|
-
it "should have previous_page nil when on first page" do
|
56
|
-
collection = create(1, 1, 3)
|
57
|
-
collection.previous_page.should be_nil
|
58
|
-
collection.next_page.should == 2
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should have both prev/next pages" do
|
62
|
-
collection = create(2, 1, 3)
|
63
|
-
collection.previous_page.should == 1
|
64
|
-
collection.next_page.should == 3
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should have next_page nil when on last page" do
|
68
|
-
collection = create(3, 1, 3)
|
69
|
-
collection.previous_page.should == 2
|
70
|
-
collection.next_page.should be_nil
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe "out of bounds" do
|
75
|
-
it "is out of bounds when page number is too high" do
|
76
|
-
create(2, 3, 2).should be_out_of_bounds
|
77
|
-
end
|
78
|
-
|
79
|
-
it "isn't out of bounds when inside collection" do
|
80
|
-
create(1, 3, 2).should_not be_out_of_bounds
|
81
|
-
end
|
82
|
-
|
83
|
-
it "isn't out of bounds when the collection is empty" do
|
84
|
-
collection = create(1, 3, 0)
|
85
|
-
collection.should_not be_out_of_bounds
|
86
|
-
collection.total_pages.should == 1
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "guessing total count" do
|
91
|
-
it "can guess when collection is shorter than limit" do
|
92
|
-
collection = create { |p| p.replace array }
|
93
|
-
collection.total_entries.should == 8
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should allow explicit total count to override guessed" do
|
97
|
-
collection = create(2, 5, 10) { |p| p.replace array }
|
98
|
-
collection.total_entries.should == 10
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should not be able to guess when collection is same as limit" do
|
102
|
-
collection = create { |p| p.replace array(5) }
|
103
|
-
collection.total_entries.should be_nil
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should not be able to guess when collection is empty" do
|
107
|
-
collection = create { |p| p.replace array(0) }
|
108
|
-
collection.total_entries.should be_nil
|
109
|
-
end
|
110
|
-
|
111
|
-
it "should be able to guess when collection is empty and this is the first page" do
|
112
|
-
collection = create(1) { |p| p.replace array(0) }
|
113
|
-
collection.total_entries.should == 0
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should not respond to page_count anymore" do
|
118
|
-
Proc.new { create.page_count }.should raise_error(NoMethodError)
|
119
|
-
end
|
120
|
-
|
121
|
-
it "inherits per_page from global value" do
|
122
|
-
collection = described_class.new(1)
|
123
|
-
collection.per_page.should == 30
|
124
|
-
end
|
125
|
-
|
126
|
-
private
|
127
|
-
|
128
|
-
def create(page = 2, limit = 5, total = nil, &block)
|
129
|
-
if block_given?
|
130
|
-
described_class.create(page, limit, total, &block)
|
131
|
-
else
|
132
|
-
described_class.new(page, limit, total)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def array(size = 3)
|
137
|
-
Array.new(size)
|
138
|
-
end
|
139
|
-
end
|
data/spec/console
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
3
|
-
opts = %w[ --simple-prompt -rirb/completion ]
|
4
|
-
if ARGV.include? '-dm'
|
5
|
-
opts << '-rwill_paginate/data_mapper' << '-rfinders/data_mapper_test_connector'
|
6
|
-
elsif ARGV.include? '-seq'
|
7
|
-
opts << '-rwill_paginate/sequel' << '-rfinders/sequel_test_connector'
|
8
|
-
else
|
9
|
-
opts << '-rconsole_fixtures'
|
10
|
-
end
|
11
|
-
|
12
|
-
exec 'bundle', 'exec', irb, '-Ilib:spec', *opts
|
data/spec/console_fixtures.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
Bundler.setup
|
3
|
-
|
4
|
-
require 'will_paginate/active_record'
|
5
|
-
require 'finders/activerecord_test_connector'
|
6
|
-
|
7
|
-
ActiverecordTestConnector.setup
|
8
|
-
|
9
|
-
windows = RUBY_PLATFORM =~ /(:?mswin|mingw)/
|
10
|
-
# used just for the `color` method
|
11
|
-
log_subscriber = ActiveSupport::LogSubscriber.log_subscribers.first
|
12
|
-
|
13
|
-
IGNORE_SQL = /\b(sqlite_master|sqlite_version)\b|^(CREATE TABLE|PRAGMA)\b/
|
14
|
-
|
15
|
-
ActiveSupport::Notifications.subscribe(/^sql\./) do |*args|
|
16
|
-
data = args.last
|
17
|
-
unless data[:name] =~ /^Fixture/ or data[:sql] =~ IGNORE_SQL
|
18
|
-
if windows
|
19
|
-
puts data[:sql]
|
20
|
-
else
|
21
|
-
puts log_subscriber.send(:color, data[:sql], :cyan)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# load all fixtures
|
27
|
-
ActiverecordTestConnector::Fixtures.create_fixtures \
|
28
|
-
ActiverecordTestConnector::FIXTURES_PATH, ActiveRecord::Base.connection.tables
|
data/spec/database.yml
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
sqlite3:
|
2
|
-
database: ":memory:"
|
3
|
-
adapter: sqlite3
|
4
|
-
timeout: 500
|
5
|
-
|
6
|
-
mysql: &mysql
|
7
|
-
adapter: mysql
|
8
|
-
database: will_paginate
|
9
|
-
username:
|
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 %>
|
20
|
-
|
21
|
-
mysql2:
|
22
|
-
<<: *mysql
|
23
|
-
adapter: mysql2
|
24
|
-
|
25
|
-
postgres:
|
26
|
-
adapter: postgresql
|
27
|
-
database: will_paginate
|
28
|
-
username: <%= "postgres" if ENV["TRAVIS"] %>
|
29
|
-
min_messages: warning
|
data/spec/fake_rubygems.rb
DELETED
@@ -1,18 +0,0 @@
|
|
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"
|
@@ -1,418 +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
|
-
abort unless ActiverecordTestConnector.able_to_connect
|
7
|
-
|
8
|
-
describe WillPaginate::ActiveRecord do
|
9
|
-
|
10
|
-
extend ActiverecordTestConnector::FixtureSetup
|
11
|
-
|
12
|
-
fixtures :topics, :replies, :users, :projects, :developers_projects
|
13
|
-
|
14
|
-
it "should integrate with ActiveRecord::Base" do
|
15
|
-
ActiveRecord::Base.should respond_to(:paginate)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should paginate" do
|
19
|
-
lambda {
|
20
|
-
users = User.paginate(:page => 1, :per_page => 5).to_a
|
21
|
-
users.length.should == 5
|
22
|
-
}.should run_queries(2)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should fail when encountering unknown params" do
|
26
|
-
lambda {
|
27
|
-
User.paginate :foo => 'bar', :page => 1, :per_page => 4
|
28
|
-
}.should raise_error(ArgumentError)
|
29
|
-
end
|
30
|
-
|
31
|
-
describe "relation" do
|
32
|
-
it "should return a relation" do
|
33
|
-
rel = nil
|
34
|
-
lambda {
|
35
|
-
rel = Developer.paginate(:page => 1)
|
36
|
-
rel.per_page.should == 10
|
37
|
-
rel.current_page.should == 1
|
38
|
-
}.should run_queries(0)
|
39
|
-
|
40
|
-
lambda {
|
41
|
-
rel.total_pages.should == 2
|
42
|
-
}.should run_queries(1)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should keep per-class per_page number" do
|
46
|
-
rel = Developer.order('id').paginate(:page => 1)
|
47
|
-
rel.per_page.should == 10
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should be able to change per_page number" do
|
51
|
-
rel = Developer.order('id').paginate(:page => 1).limit(5)
|
52
|
-
rel.per_page.should == 5
|
53
|
-
end
|
54
|
-
|
55
|
-
it "remembers pagination in sub-relations" do
|
56
|
-
rel = Topic.paginate(:page => 2, :per_page => 3)
|
57
|
-
lambda {
|
58
|
-
rel.total_entries.should == 4
|
59
|
-
}.should run_queries(1)
|
60
|
-
rel = rel.mentions_activerecord
|
61
|
-
rel.current_page.should == 2
|
62
|
-
rel.per_page.should == 3
|
63
|
-
lambda {
|
64
|
-
rel.total_entries.should == 1
|
65
|
-
}.should run_queries(1)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "supports the page() method" do
|
69
|
-
rel = Developer.page('1').order('id')
|
70
|
-
rel.current_page.should == 1
|
71
|
-
rel.per_page.should == 10
|
72
|
-
rel.offset.should == 0
|
73
|
-
|
74
|
-
rel = rel.limit(5).page(2)
|
75
|
-
rel.per_page.should == 5
|
76
|
-
rel.offset.should == 5
|
77
|
-
end
|
78
|
-
|
79
|
-
it "raises on invalid page number" do
|
80
|
-
lambda {
|
81
|
-
Developer.page('foo')
|
82
|
-
}.should raise_error(ArgumentError)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "supports first limit() then page()" do
|
86
|
-
rel = Developer.limit(3).page(3)
|
87
|
-
rel.offset.should == 6
|
88
|
-
end
|
89
|
-
|
90
|
-
it "supports first page() then limit()" do
|
91
|
-
rel = Developer.page(3).limit(3)
|
92
|
-
rel.offset.should == 6
|
93
|
-
end
|
94
|
-
|
95
|
-
it "supports #first" do
|
96
|
-
rel = Developer.order('id').page(2).per_page(4)
|
97
|
-
rel.first.should == users(:dev_5)
|
98
|
-
rel.first(2).should == users(:dev_5, :dev_6)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "supports #last" do
|
102
|
-
rel = Developer.order('id').page(2).per_page(4)
|
103
|
-
rel.last.should == users(:dev_8)
|
104
|
-
rel.last(2).should == users(:dev_7, :dev_8)
|
105
|
-
rel.page(3).last.should == users(:poor_jamis)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
describe "counting" do
|
110
|
-
it "should guess the total count" do
|
111
|
-
lambda {
|
112
|
-
topics = Topic.paginate :page => 2, :per_page => 3
|
113
|
-
topics.total_entries.should == 4
|
114
|
-
}.should run_queries(1)
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should guess that there are no records" do
|
118
|
-
lambda {
|
119
|
-
topics = Topic.where(:project_id => 999).paginate :page => 1, :per_page => 3
|
120
|
-
topics.total_entries.should == 0
|
121
|
-
}.should run_queries(1)
|
122
|
-
end
|
123
|
-
|
124
|
-
it "forgets count in sub-relations" do
|
125
|
-
lambda {
|
126
|
-
topics = Topic.paginate :page => 1, :per_page => 3
|
127
|
-
topics.total_entries.should == 4
|
128
|
-
topics.where('1 = 1').total_entries.should == 4
|
129
|
-
}.should run_queries(2)
|
130
|
-
end
|
131
|
-
|
132
|
-
it "supports empty? method" do
|
133
|
-
topics = Topic.paginate :page => 1, :per_page => 3
|
134
|
-
lambda {
|
135
|
-
topics.should_not be_empty
|
136
|
-
}.should run_queries(1)
|
137
|
-
end
|
138
|
-
|
139
|
-
it "support empty? for grouped queries" do
|
140
|
-
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
141
|
-
lambda {
|
142
|
-
topics.should_not be_empty
|
143
|
-
}.should run_queries(1)
|
144
|
-
end
|
145
|
-
|
146
|
-
it "supports `size` for grouped queries" do
|
147
|
-
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
148
|
-
lambda {
|
149
|
-
topics.size.should == {nil=>2, 1=>2}
|
150
|
-
}.should run_queries(1)
|
151
|
-
end
|
152
|
-
|
153
|
-
it "overrides total_entries count with a fixed value" do
|
154
|
-
lambda {
|
155
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => 999
|
156
|
-
topics.total_entries.should == 999
|
157
|
-
# value is kept even in sub-relations
|
158
|
-
topics.where('1 = 1').total_entries.should == 999
|
159
|
-
}.should run_queries(0)
|
160
|
-
end
|
161
|
-
|
162
|
-
it "supports a non-int for total_entries" do
|
163
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => "999"
|
164
|
-
topics.total_entries.should == 999
|
165
|
-
end
|
166
|
-
|
167
|
-
it "removes :include for count" do
|
168
|
-
lambda {
|
169
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
170
|
-
developers.total_entries.should == 11
|
171
|
-
$query_sql.last.should_not =~ /\bJOIN\b/
|
172
|
-
}.should run_queries(1)
|
173
|
-
end
|
174
|
-
|
175
|
-
it "keeps :include for count when they are referenced in :conditions" do
|
176
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
177
|
-
with_condition = developers.where('projects.id > 1')
|
178
|
-
with_condition = with_condition.references(:projects) if with_condition.respond_to?(:references)
|
179
|
-
with_condition.total_entries.should == 1
|
180
|
-
|
181
|
-
$query_sql.last.should =~ /\bJOIN\b/
|
182
|
-
end
|
183
|
-
|
184
|
-
it "should count with group" do
|
185
|
-
Developer.group(:salary).page(1).total_entries.should == 4
|
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
|
196
|
-
|
197
|
-
it "should not have zero total_pages when the result set is empty" do
|
198
|
-
Developer.where("1 = 2").page(1).total_pages.should == 1
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
it "should not ignore :select parameter when it says DISTINCT" do
|
203
|
-
users = User.select('DISTINCT salary').paginate :page => 2
|
204
|
-
users.total_entries.should == 5
|
205
|
-
end
|
206
|
-
|
207
|
-
describe "paginate_by_sql" do
|
208
|
-
it "should respond" do
|
209
|
-
User.should respond_to(:paginate_by_sql)
|
210
|
-
end
|
211
|
-
|
212
|
-
it "should paginate" do
|
213
|
-
lambda {
|
214
|
-
sql = "select content from topics where content like '%futurama%'"
|
215
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 1
|
216
|
-
topics.total_entries.should == 1
|
217
|
-
topics.first.attributes.has_key?('title').should be_false
|
218
|
-
}.should run_queries(2)
|
219
|
-
end
|
220
|
-
|
221
|
-
it "should respect total_entries setting" do
|
222
|
-
lambda {
|
223
|
-
sql = "select content from topics"
|
224
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 1, :total_entries => 999
|
225
|
-
topics.total_entries.should == 999
|
226
|
-
}.should run_queries(1)
|
227
|
-
end
|
228
|
-
|
229
|
-
it "defaults to page 1" do
|
230
|
-
sql = "select content from topics"
|
231
|
-
topics = Topic.paginate_by_sql sql, :page => nil, :per_page => 1
|
232
|
-
topics.current_page.should == 1
|
233
|
-
topics.size.should == 1
|
234
|
-
end
|
235
|
-
|
236
|
-
it "should strip the order when counting" do
|
237
|
-
expected = topics(:ar)
|
238
|
-
lambda {
|
239
|
-
sql = "select id, title, content from topics order by topics.title"
|
240
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
|
241
|
-
topics.first.should == expected
|
242
|
-
}.should run_queries(2)
|
243
|
-
|
244
|
-
$query_sql.last.should include('COUNT')
|
245
|
-
$query_sql.last.should_not include('order by topics.title')
|
246
|
-
end
|
247
|
-
|
248
|
-
it "shouldn't change the original query string" do
|
249
|
-
query = 'select * from topics where 1 = 2'
|
250
|
-
original_query = query.dup
|
251
|
-
Topic.paginate_by_sql(query, :page => 1)
|
252
|
-
query.should == original_query
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
it "doesn't mangle options" do
|
257
|
-
options = { :page => 1 }
|
258
|
-
options.expects(:delete).never
|
259
|
-
options_before = options.dup
|
260
|
-
|
261
|
-
Topic.paginate(options)
|
262
|
-
options.should == options_before
|
263
|
-
end
|
264
|
-
|
265
|
-
it "should get first page of Topics with a single query" do
|
266
|
-
lambda {
|
267
|
-
result = Topic.paginate :page => nil
|
268
|
-
result.to_a # trigger loading of records
|
269
|
-
result.current_page.should == 1
|
270
|
-
result.total_pages.should == 1
|
271
|
-
result.size.should == 4
|
272
|
-
}.should run_queries(1)
|
273
|
-
end
|
274
|
-
|
275
|
-
it "should get second (inexistent) page of Topics, requiring 2 queries" do
|
276
|
-
lambda {
|
277
|
-
result = Topic.paginate :page => 2
|
278
|
-
result.total_pages.should == 1
|
279
|
-
result.should be_empty
|
280
|
-
}.should run_queries(2)
|
281
|
-
end
|
282
|
-
|
283
|
-
describe "associations" do
|
284
|
-
it "should paginate" do
|
285
|
-
dhh = users(:david)
|
286
|
-
expected_name_ordered = projects(:action_controller, :active_record)
|
287
|
-
expected_id_ordered = projects(:active_record, :action_controller)
|
288
|
-
|
289
|
-
lambda {
|
290
|
-
# with association-specified order
|
291
|
-
result = ignore_deprecation {
|
292
|
-
dhh.projects.includes(:topics).order('projects.name').paginate(:page => 1)
|
293
|
-
}
|
294
|
-
result.to_a.should == expected_name_ordered
|
295
|
-
result.total_entries.should == 2
|
296
|
-
}.should run_queries(2)
|
297
|
-
|
298
|
-
# with explicit order
|
299
|
-
result = dhh.projects.paginate(:page => 1).reorder('projects.id')
|
300
|
-
result.should == expected_id_ordered
|
301
|
-
result.total_entries.should == 2
|
302
|
-
|
303
|
-
lambda {
|
304
|
-
dhh.projects.order('projects.id').limit(4).to_a
|
305
|
-
}.should_not raise_error
|
306
|
-
|
307
|
-
result = dhh.projects.paginate(:page => 1, :per_page => 4).reorder('projects.id')
|
308
|
-
result.should == expected_id_ordered
|
309
|
-
|
310
|
-
# has_many with implicit order
|
311
|
-
topic = Topic.find(1)
|
312
|
-
expected = replies(:spam, :witty_retort)
|
313
|
-
# FIXME: wow, this is ugly
|
314
|
-
topic.replies.paginate(:page => 1).map(&:id).sort.should == expected.map(&:id).sort
|
315
|
-
topic.replies.paginate(:page => 1).reorder('replies.id ASC').should == expected.reverse
|
316
|
-
end
|
317
|
-
|
318
|
-
it "should paginate through association extension" do
|
319
|
-
project = Project.order('id').first
|
320
|
-
expected = [replies(:brave)]
|
321
|
-
|
322
|
-
lambda {
|
323
|
-
result = project.replies.only_recent.paginate(:page => 1)
|
324
|
-
result.should == expected
|
325
|
-
}.should run_queries(1)
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
it "should paginate with joins" do
|
330
|
-
result = nil
|
331
|
-
join_sql = 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id'
|
332
|
-
|
333
|
-
lambda {
|
334
|
-
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
335
|
-
result.to_a # trigger loading of records
|
336
|
-
result.size.should == 2
|
337
|
-
developer_names = result.map(&:name)
|
338
|
-
developer_names.should include('David')
|
339
|
-
developer_names.should include('Jamis')
|
340
|
-
}.should run_queries(1)
|
341
|
-
|
342
|
-
lambda {
|
343
|
-
expected = result.to_a
|
344
|
-
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
345
|
-
result.should == expected
|
346
|
-
result.total_entries.should == 2
|
347
|
-
}.should run_queries(1)
|
348
|
-
end
|
349
|
-
|
350
|
-
it "should paginate with group" do
|
351
|
-
result = nil
|
352
|
-
lambda {
|
353
|
-
result = Developer.select('salary').order('salary').group('salary').
|
354
|
-
paginate(:page => 1, :per_page => 10).to_a
|
355
|
-
}.should run_queries(1)
|
356
|
-
|
357
|
-
expected = users(:david, :jamis, :dev_10, :poor_jamis).map(&:salary).sort
|
358
|
-
result.map(&:salary).should == expected
|
359
|
-
end
|
360
|
-
|
361
|
-
it "should not paginate with dynamic finder" do
|
362
|
-
lambda {
|
363
|
-
Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
|
364
|
-
}.should raise_error(NoMethodError)
|
365
|
-
end
|
366
|
-
|
367
|
-
describe "scopes" do
|
368
|
-
it "should paginate" do
|
369
|
-
result = Developer.poor.paginate :page => 1, :per_page => 1
|
370
|
-
result.size.should == 1
|
371
|
-
result.total_entries.should == 2
|
372
|
-
end
|
373
|
-
|
374
|
-
it "should paginate on habtm association" do
|
375
|
-
project = projects(:active_record)
|
376
|
-
lambda {
|
377
|
-
result = ignore_deprecation { project.developers.poor.paginate :page => 1, :per_page => 1 }
|
378
|
-
result.size.should == 1
|
379
|
-
result.total_entries.should == 1
|
380
|
-
}.should run_queries(2)
|
381
|
-
end
|
382
|
-
|
383
|
-
it "should paginate on hmt association" do
|
384
|
-
project = projects(:active_record)
|
385
|
-
expected = [replies(:brave)]
|
386
|
-
|
387
|
-
lambda {
|
388
|
-
result = project.replies.recent.paginate :page => 1, :per_page => 1
|
389
|
-
result.should == expected
|
390
|
-
result.total_entries.should == 1
|
391
|
-
}.should run_queries(2)
|
392
|
-
end
|
393
|
-
|
394
|
-
it "should paginate on has_many association" do
|
395
|
-
project = projects(:active_record)
|
396
|
-
expected = [topics(:ar)]
|
397
|
-
|
398
|
-
lambda {
|
399
|
-
result = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
|
400
|
-
result.should == expected
|
401
|
-
result.total_entries.should == 1
|
402
|
-
}.should run_queries(2)
|
403
|
-
end
|
404
|
-
end
|
405
|
-
|
406
|
-
it "should not paginate an array of IDs" do
|
407
|
-
lambda {
|
408
|
-
Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
|
409
|
-
}.should raise_error(ArgumentError)
|
410
|
-
end
|
411
|
-
|
412
|
-
it "errors out for invalid values" do |variable|
|
413
|
-
lambda {
|
414
|
-
# page that results in an offset larger than BIGINT
|
415
|
-
Project.page(307445734561825862)
|
416
|
-
}.should raise_error(WillPaginate::InvalidPage, "invalid offset: 9223372036854775830")
|
417
|
-
end
|
418
|
-
end
|