will_paginate 3.1.8 → 4.0.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 +3 -5
- data/lib/will_paginate/active_record.rb +3 -3
- 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 +14 -8
- 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 -417
- data/spec/finders/activerecord_test_connector.rb +0 -140
- data/spec/finders/data_mapper_spec.rb +0 -114
- data/spec/finders/data_mapper_test_connector.rb +0 -54
- data/spec/finders/mongoid_spec.rb +0 -145
- data/spec/finders/sequel_spec.rb +0 -65
- 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: root
|
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,417 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'will_paginate/active_record'
|
3
|
-
require File.expand_path('../activerecord_test_connector', __FILE__)
|
4
|
-
|
5
|
-
ActiverecordTestConnector.setup
|
6
|
-
|
7
|
-
describe WillPaginate::ActiveRecord do
|
8
|
-
|
9
|
-
extend ActiverecordTestConnector::FixtureSetup
|
10
|
-
|
11
|
-
fixtures :topics, :replies, :users, :projects, :developers_projects
|
12
|
-
|
13
|
-
it "should integrate with ActiveRecord::Base" do
|
14
|
-
ActiveRecord::Base.should respond_to(:paginate)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should paginate" do
|
18
|
-
lambda {
|
19
|
-
users = User.paginate(:page => 1, :per_page => 5).to_a
|
20
|
-
users.length.should == 5
|
21
|
-
}.should run_queries(2)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should fail when encountering unknown params" do
|
25
|
-
lambda {
|
26
|
-
User.paginate :foo => 'bar', :page => 1, :per_page => 4
|
27
|
-
}.should raise_error(ArgumentError)
|
28
|
-
end
|
29
|
-
|
30
|
-
describe "relation" do
|
31
|
-
it "should return a relation" do
|
32
|
-
rel = nil
|
33
|
-
lambda {
|
34
|
-
rel = Developer.paginate(:page => 1)
|
35
|
-
rel.per_page.should == 10
|
36
|
-
rel.current_page.should == 1
|
37
|
-
}.should run_queries(0)
|
38
|
-
|
39
|
-
lambda {
|
40
|
-
rel.total_pages.should == 2
|
41
|
-
}.should run_queries(1)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should keep per-class per_page number" do
|
45
|
-
rel = Developer.order('id').paginate(:page => 1)
|
46
|
-
rel.per_page.should == 10
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should be able to change per_page number" do
|
50
|
-
rel = Developer.order('id').paginate(:page => 1).limit(5)
|
51
|
-
rel.per_page.should == 5
|
52
|
-
end
|
53
|
-
|
54
|
-
it "remembers pagination in sub-relations" do
|
55
|
-
rel = Topic.paginate(:page => 2, :per_page => 3)
|
56
|
-
lambda {
|
57
|
-
rel.total_entries.should == 4
|
58
|
-
}.should run_queries(1)
|
59
|
-
rel = rel.mentions_activerecord
|
60
|
-
rel.current_page.should == 2
|
61
|
-
rel.per_page.should == 3
|
62
|
-
lambda {
|
63
|
-
rel.total_entries.should == 1
|
64
|
-
}.should run_queries(1)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "supports the page() method" do
|
68
|
-
rel = Developer.page('1').order('id')
|
69
|
-
rel.current_page.should == 1
|
70
|
-
rel.per_page.should == 10
|
71
|
-
rel.offset.should == 0
|
72
|
-
|
73
|
-
rel = rel.limit(5).page(2)
|
74
|
-
rel.per_page.should == 5
|
75
|
-
rel.offset.should == 5
|
76
|
-
end
|
77
|
-
|
78
|
-
it "raises on invalid page number" do
|
79
|
-
lambda {
|
80
|
-
Developer.page('foo')
|
81
|
-
}.should raise_error(ArgumentError)
|
82
|
-
end
|
83
|
-
|
84
|
-
it "supports first limit() then page()" do
|
85
|
-
rel = Developer.limit(3).page(3)
|
86
|
-
rel.offset.should == 6
|
87
|
-
end
|
88
|
-
|
89
|
-
it "supports first page() then limit()" do
|
90
|
-
rel = Developer.page(3).limit(3)
|
91
|
-
rel.offset.should == 6
|
92
|
-
end
|
93
|
-
|
94
|
-
it "supports #first" do
|
95
|
-
rel = Developer.order('id').page(2).per_page(4)
|
96
|
-
rel.first.should == users(:dev_5)
|
97
|
-
rel.first(2).should == users(:dev_5, :dev_6)
|
98
|
-
end
|
99
|
-
|
100
|
-
it "supports #last" do
|
101
|
-
rel = Developer.order('id').page(2).per_page(4)
|
102
|
-
rel.last.should == users(:dev_8)
|
103
|
-
rel.last(2).should == users(:dev_7, :dev_8)
|
104
|
-
rel.page(3).last.should == users(:poor_jamis)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
describe "counting" do
|
109
|
-
it "should guess the total count" do
|
110
|
-
lambda {
|
111
|
-
topics = Topic.paginate :page => 2, :per_page => 3
|
112
|
-
topics.total_entries.should == 4
|
113
|
-
}.should run_queries(1)
|
114
|
-
end
|
115
|
-
|
116
|
-
it "should guess that there are no records" do
|
117
|
-
lambda {
|
118
|
-
topics = Topic.where(:project_id => 999).paginate :page => 1, :per_page => 3
|
119
|
-
topics.total_entries.should == 0
|
120
|
-
}.should run_queries(1)
|
121
|
-
end
|
122
|
-
|
123
|
-
it "forgets count in sub-relations" do
|
124
|
-
lambda {
|
125
|
-
topics = Topic.paginate :page => 1, :per_page => 3
|
126
|
-
topics.total_entries.should == 4
|
127
|
-
topics.where('1 = 1').total_entries.should == 4
|
128
|
-
}.should run_queries(2)
|
129
|
-
end
|
130
|
-
|
131
|
-
it "supports empty? method" do
|
132
|
-
topics = Topic.paginate :page => 1, :per_page => 3
|
133
|
-
lambda {
|
134
|
-
topics.should_not be_empty
|
135
|
-
}.should run_queries(1)
|
136
|
-
end
|
137
|
-
|
138
|
-
it "support empty? for grouped queries" do
|
139
|
-
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
140
|
-
lambda {
|
141
|
-
topics.should_not be_empty
|
142
|
-
}.should run_queries(1)
|
143
|
-
end
|
144
|
-
|
145
|
-
it "supports `size` for grouped queries" do
|
146
|
-
topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
|
147
|
-
lambda {
|
148
|
-
topics.size.should == {nil=>2, 1=>2}
|
149
|
-
}.should run_queries(1)
|
150
|
-
end
|
151
|
-
|
152
|
-
it "overrides total_entries count with a fixed value" do
|
153
|
-
lambda {
|
154
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => 999
|
155
|
-
topics.total_entries.should == 999
|
156
|
-
# value is kept even in sub-relations
|
157
|
-
topics.where('1 = 1').total_entries.should == 999
|
158
|
-
}.should run_queries(0)
|
159
|
-
end
|
160
|
-
|
161
|
-
it "supports a non-int for total_entries" do
|
162
|
-
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => "999"
|
163
|
-
topics.total_entries.should == 999
|
164
|
-
end
|
165
|
-
|
166
|
-
it "removes :include for count" do
|
167
|
-
lambda {
|
168
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
169
|
-
developers.total_entries.should == 11
|
170
|
-
$query_sql.last.should_not =~ /\bJOIN\b/
|
171
|
-
}.should run_queries(1)
|
172
|
-
end
|
173
|
-
|
174
|
-
it "keeps :include for count when they are referenced in :conditions" do
|
175
|
-
developers = Developer.paginate(:page => 1, :per_page => 1).includes(:projects)
|
176
|
-
with_condition = developers.where('projects.id > 1')
|
177
|
-
with_condition = with_condition.references(:projects) if with_condition.respond_to?(:references)
|
178
|
-
with_condition.total_entries.should == 1
|
179
|
-
|
180
|
-
$query_sql.last.should =~ /\bJOIN\b/
|
181
|
-
end
|
182
|
-
|
183
|
-
it "should count with group" do
|
184
|
-
Developer.group(:salary).page(1).total_entries.should == 4
|
185
|
-
end
|
186
|
-
|
187
|
-
it "should count with select" do
|
188
|
-
Topic.select('title, content').page(1).total_entries.should == 4
|
189
|
-
end
|
190
|
-
|
191
|
-
it "removes :reorder for count with group" do
|
192
|
-
Project.group(:id).reorder(:id).page(1).total_entries
|
193
|
-
$query_sql.last.should_not =~ /\ORDER\b/
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should not have zero total_pages when the result set is empty" do
|
197
|
-
Developer.where("1 = 2").page(1).total_pages.should == 1
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
it "should not ignore :select parameter when it says DISTINCT" do
|
202
|
-
users = User.select('DISTINCT salary').paginate :page => 2
|
203
|
-
users.total_entries.should == 5
|
204
|
-
end
|
205
|
-
|
206
|
-
describe "paginate_by_sql" do
|
207
|
-
it "should respond" do
|
208
|
-
User.should respond_to(:paginate_by_sql)
|
209
|
-
end
|
210
|
-
|
211
|
-
it "should paginate" do
|
212
|
-
lambda {
|
213
|
-
sql = "select content from topics where content like '%futurama%'"
|
214
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 1
|
215
|
-
topics.total_entries.should == 1
|
216
|
-
topics.first.attributes.has_key?('title').should be_false
|
217
|
-
}.should run_queries(2)
|
218
|
-
end
|
219
|
-
|
220
|
-
it "should respect total_entries setting" do
|
221
|
-
lambda {
|
222
|
-
sql = "select content from topics"
|
223
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 1, :total_entries => 999
|
224
|
-
topics.total_entries.should == 999
|
225
|
-
}.should run_queries(1)
|
226
|
-
end
|
227
|
-
|
228
|
-
it "defaults to page 1" do
|
229
|
-
sql = "select content from topics"
|
230
|
-
topics = Topic.paginate_by_sql sql, :page => nil, :per_page => 1
|
231
|
-
topics.current_page.should == 1
|
232
|
-
topics.size.should == 1
|
233
|
-
end
|
234
|
-
|
235
|
-
it "should strip the order when counting" do
|
236
|
-
expected = topics(:ar)
|
237
|
-
lambda {
|
238
|
-
sql = "select id, title, content from topics order by topics.title"
|
239
|
-
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
|
240
|
-
topics.first.should == expected
|
241
|
-
}.should run_queries(2)
|
242
|
-
|
243
|
-
$query_sql.last.should include('COUNT')
|
244
|
-
$query_sql.last.should_not include('order by topics.title')
|
245
|
-
end
|
246
|
-
|
247
|
-
it "shouldn't change the original query string" do
|
248
|
-
query = 'select * from topics where 1 = 2'
|
249
|
-
original_query = query.dup
|
250
|
-
Topic.paginate_by_sql(query, :page => 1)
|
251
|
-
query.should == original_query
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
it "doesn't mangle options" do
|
256
|
-
options = { :page => 1 }
|
257
|
-
options.expects(:delete).never
|
258
|
-
options_before = options.dup
|
259
|
-
|
260
|
-
Topic.paginate(options)
|
261
|
-
options.should == options_before
|
262
|
-
end
|
263
|
-
|
264
|
-
it "should get first page of Topics with a single query" do
|
265
|
-
lambda {
|
266
|
-
result = Topic.paginate :page => nil
|
267
|
-
result.to_a # trigger loading of records
|
268
|
-
result.current_page.should == 1
|
269
|
-
result.total_pages.should == 1
|
270
|
-
result.size.should == 4
|
271
|
-
}.should run_queries(1)
|
272
|
-
end
|
273
|
-
|
274
|
-
it "should get second (inexistent) page of Topics, requiring 2 queries" do
|
275
|
-
lambda {
|
276
|
-
result = Topic.paginate :page => 2
|
277
|
-
result.total_pages.should == 1
|
278
|
-
result.should be_empty
|
279
|
-
}.should run_queries(2)
|
280
|
-
end
|
281
|
-
|
282
|
-
describe "associations" do
|
283
|
-
it "should paginate" do
|
284
|
-
dhh = users(:david)
|
285
|
-
expected_name_ordered = projects(:action_controller, :active_record)
|
286
|
-
expected_id_ordered = projects(:active_record, :action_controller)
|
287
|
-
|
288
|
-
lambda {
|
289
|
-
# with association-specified order
|
290
|
-
result = ignore_deprecation {
|
291
|
-
dhh.projects.includes(:topics).order('projects.name').paginate(:page => 1)
|
292
|
-
}
|
293
|
-
result.to_a.should == expected_name_ordered
|
294
|
-
result.total_entries.should == 2
|
295
|
-
}.should run_queries(2)
|
296
|
-
|
297
|
-
# with explicit order
|
298
|
-
result = dhh.projects.paginate(:page => 1).reorder('projects.id')
|
299
|
-
result.should == expected_id_ordered
|
300
|
-
result.total_entries.should == 2
|
301
|
-
|
302
|
-
lambda {
|
303
|
-
dhh.projects.order('projects.id').limit(4).to_a
|
304
|
-
}.should_not raise_error
|
305
|
-
|
306
|
-
result = dhh.projects.paginate(:page => 1, :per_page => 4).reorder('projects.id')
|
307
|
-
result.should == expected_id_ordered
|
308
|
-
|
309
|
-
# has_many with implicit order
|
310
|
-
topic = Topic.find(1)
|
311
|
-
expected = replies(:spam, :witty_retort)
|
312
|
-
# FIXME: wow, this is ugly
|
313
|
-
topic.replies.paginate(:page => 1).map(&:id).sort.should == expected.map(&:id).sort
|
314
|
-
topic.replies.paginate(:page => 1).reorder('replies.id ASC').should == expected.reverse
|
315
|
-
end
|
316
|
-
|
317
|
-
it "should paginate through association extension" do
|
318
|
-
project = Project.order('id').first
|
319
|
-
expected = [replies(:brave)]
|
320
|
-
|
321
|
-
lambda {
|
322
|
-
result = project.replies.only_recent.paginate(:page => 1)
|
323
|
-
result.should == expected
|
324
|
-
}.should run_queries(1)
|
325
|
-
end
|
326
|
-
end
|
327
|
-
|
328
|
-
it "should paginate with joins" do
|
329
|
-
result = nil
|
330
|
-
join_sql = 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id'
|
331
|
-
|
332
|
-
lambda {
|
333
|
-
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
334
|
-
result.to_a # trigger loading of records
|
335
|
-
result.size.should == 2
|
336
|
-
developer_names = result.map(&:name)
|
337
|
-
developer_names.should include('David')
|
338
|
-
developer_names.should include('Jamis')
|
339
|
-
}.should run_queries(1)
|
340
|
-
|
341
|
-
lambda {
|
342
|
-
expected = result.to_a
|
343
|
-
result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
|
344
|
-
result.should == expected
|
345
|
-
result.total_entries.should == 2
|
346
|
-
}.should run_queries(1)
|
347
|
-
end
|
348
|
-
|
349
|
-
it "should paginate with group" do
|
350
|
-
result = nil
|
351
|
-
lambda {
|
352
|
-
result = Developer.select('salary').order('salary').group('salary').
|
353
|
-
paginate(:page => 1, :per_page => 10).to_a
|
354
|
-
}.should run_queries(1)
|
355
|
-
|
356
|
-
expected = users(:david, :jamis, :dev_10, :poor_jamis).map(&:salary).sort
|
357
|
-
result.map(&:salary).should == expected
|
358
|
-
end
|
359
|
-
|
360
|
-
it "should not paginate with dynamic finder" do
|
361
|
-
lambda {
|
362
|
-
Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
|
363
|
-
}.should raise_error(NoMethodError)
|
364
|
-
end
|
365
|
-
|
366
|
-
describe "scopes" do
|
367
|
-
it "should paginate" do
|
368
|
-
result = Developer.poor.paginate :page => 1, :per_page => 1
|
369
|
-
result.size.should == 1
|
370
|
-
result.total_entries.should == 2
|
371
|
-
end
|
372
|
-
|
373
|
-
it "should paginate on habtm association" do
|
374
|
-
project = projects(:active_record)
|
375
|
-
lambda {
|
376
|
-
result = ignore_deprecation { project.developers.poor.paginate :page => 1, :per_page => 1 }
|
377
|
-
result.size.should == 1
|
378
|
-
result.total_entries.should == 1
|
379
|
-
}.should run_queries(2)
|
380
|
-
end
|
381
|
-
|
382
|
-
it "should paginate on hmt association" do
|
383
|
-
project = projects(:active_record)
|
384
|
-
expected = [replies(:brave)]
|
385
|
-
|
386
|
-
lambda {
|
387
|
-
result = project.replies.recent.paginate :page => 1, :per_page => 1
|
388
|
-
result.should == expected
|
389
|
-
result.total_entries.should == 1
|
390
|
-
}.should run_queries(2)
|
391
|
-
end
|
392
|
-
|
393
|
-
it "should paginate on has_many association" do
|
394
|
-
project = projects(:active_record)
|
395
|
-
expected = [topics(:ar)]
|
396
|
-
|
397
|
-
lambda {
|
398
|
-
result = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
|
399
|
-
result.should == expected
|
400
|
-
result.total_entries.should == 1
|
401
|
-
}.should run_queries(2)
|
402
|
-
end
|
403
|
-
end
|
404
|
-
|
405
|
-
it "should not paginate an array of IDs" do
|
406
|
-
lambda {
|
407
|
-
Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
|
408
|
-
}.should raise_error(ArgumentError)
|
409
|
-
end
|
410
|
-
|
411
|
-
it "errors out for invalid values" do |variable|
|
412
|
-
lambda {
|
413
|
-
# page that results in an offset larger than BIGINT
|
414
|
-
Project.page(307445734561825862)
|
415
|
-
}.should raise_error(WillPaginate::InvalidPage, "invalid offset: 9223372036854775830")
|
416
|
-
end
|
417
|
-
end
|