will_paginate 2.3.17 → 3.0.pre

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of will_paginate might be problematic. Click here for more details.

Files changed (83) hide show
  1. data/CHANGELOG.rdoc +24 -80
  2. data/LICENSE +1 -1
  3. data/README.rdoc +125 -0
  4. data/Rakefile +26 -22
  5. data/lib/will_paginate.rb +10 -84
  6. data/lib/will_paginate/array.rb +25 -8
  7. data/lib/will_paginate/collection.rb +15 -28
  8. data/lib/will_paginate/core_ext.rb +26 -0
  9. data/lib/will_paginate/deprecation.rb +50 -0
  10. data/lib/will_paginate/finders.rb +9 -0
  11. data/lib/will_paginate/finders/active_record.rb +158 -0
  12. data/lib/will_paginate/finders/active_resource.rb +51 -0
  13. data/lib/will_paginate/finders/base.rb +112 -0
  14. data/lib/will_paginate/finders/data_mapper.rb +30 -0
  15. data/lib/will_paginate/finders/sequel.rb +23 -0
  16. data/lib/will_paginate/railtie.rb +26 -0
  17. data/lib/will_paginate/version.rb +5 -5
  18. data/lib/will_paginate/view_helpers.rb +25 -436
  19. data/lib/will_paginate/view_helpers/action_view.rb +142 -0
  20. data/lib/will_paginate/view_helpers/base.rb +126 -0
  21. data/lib/will_paginate/view_helpers/link_renderer.rb +130 -0
  22. data/lib/will_paginate/view_helpers/link_renderer_base.rb +83 -0
  23. data/lib/will_paginate/view_helpers/merb.rb +13 -0
  24. data/spec/collection_spec.rb +147 -0
  25. data/spec/console +8 -0
  26. data/spec/console_fixtures.rb +8 -0
  27. data/spec/database.yml +22 -0
  28. data/spec/finders/active_record_spec.rb +377 -0
  29. data/spec/finders/active_resource_spec.rb +52 -0
  30. data/spec/finders/activerecord_test_connector.rb +114 -0
  31. data/spec/finders/data_mapper_spec.rb +62 -0
  32. data/spec/finders/data_mapper_test_connector.rb +20 -0
  33. data/spec/finders/sequel_spec.rb +53 -0
  34. data/spec/finders/sequel_test_connector.rb +9 -0
  35. data/spec/finders_spec.rb +76 -0
  36. data/{test → spec}/fixtures/admin.rb +0 -0
  37. data/{test → spec}/fixtures/developer.rb +2 -3
  38. data/{test → spec}/fixtures/developers_projects.yml +0 -0
  39. data/{test → spec}/fixtures/project.rb +2 -6
  40. data/{test → spec}/fixtures/projects.yml +1 -1
  41. data/{test → spec}/fixtures/replies.yml +0 -0
  42. data/{test → spec}/fixtures/reply.rb +1 -1
  43. data/{test → spec}/fixtures/schema.rb +0 -0
  44. data/spec/fixtures/topic.rb +7 -0
  45. data/{test → spec}/fixtures/topics.yml +0 -0
  46. data/{test → spec}/fixtures/user.rb +0 -0
  47. data/{test → spec}/fixtures/users.yml +0 -0
  48. data/spec/rcov.opts +2 -0
  49. data/spec/spec.opts +2 -0
  50. data/spec/spec_helper.rb +74 -0
  51. data/spec/tasks.rake +60 -0
  52. data/spec/view_helpers/action_view_spec.rb +345 -0
  53. data/spec/view_helpers/base_spec.rb +64 -0
  54. data/spec/view_helpers/link_renderer_base_spec.rb +84 -0
  55. data/spec/view_helpers/view_example_group.rb +103 -0
  56. metadata +60 -65
  57. data/README.md +0 -53
  58. data/lib/will_paginate/finder.rb +0 -269
  59. data/lib/will_paginate/i18n.rb +0 -29
  60. data/lib/will_paginate/locale/en.yml +0 -33
  61. data/lib/will_paginate/named_scope.rb +0 -170
  62. data/lib/will_paginate/named_scope_patch.rb +0 -37
  63. data/lib/will_paginate/per_page.rb +0 -27
  64. data/test/ci.rb +0 -60
  65. data/test/collection_test.rb +0 -160
  66. data/test/console +0 -8
  67. data/test/database.yml +0 -16
  68. data/test/finder_test.rb +0 -527
  69. data/test/fixtures/topic.rb +0 -12
  70. data/test/gemfiles/Gemfile.1.2 +0 -13
  71. data/test/gemfiles/Gemfile.1.2.lock +0 -39
  72. data/test/gemfiles/Gemfile.2.0 +0 -16
  73. data/test/gemfiles/Gemfile.2.0.lock +0 -28
  74. data/test/gemfiles/Gemfile.2.1 +0 -16
  75. data/test/gemfiles/Gemfile.2.1.lock +0 -28
  76. data/test/gemfiles/Gemfile.2.2 +0 -16
  77. data/test/gemfiles/Gemfile.2.2.lock +0 -28
  78. data/test/helper.rb +0 -34
  79. data/test/lib/activerecord_test_case.rb +0 -38
  80. data/test/lib/activerecord_test_connector.rb +0 -86
  81. data/test/lib/load_fixtures.rb +0 -12
  82. data/test/lib/view_test_process.rb +0 -186
  83. data/test/view_test.rb +0 -380
@@ -1,37 +0,0 @@
1
- ActiveRecord::Associations::AssociationProxy.class_eval do
2
- protected
3
- def with_scope(*args)
4
- @reflection.klass.send(:with_scope, *args) { |*a| yield(*a) if block_given? }
5
- end
6
- end
7
-
8
- [ ActiveRecord::Associations::AssociationCollection,
9
- ActiveRecord::Associations::HasManyThroughAssociation ].each do |klass|
10
- klass.class_eval do
11
- protected
12
- alias :method_missing_without_scopes :method_missing_without_paginate
13
- def method_missing_without_paginate(method, *args)
14
- if @reflection.klass.scopes.include?(method)
15
- @reflection.klass.scopes[method].call(self, *args) { |*a| yield(*a) if block_given? }
16
- else
17
- method_missing_without_scopes(method, *args) { |*a| yield(*a) if block_given? }
18
- end
19
- end
20
- end
21
- end
22
-
23
- # Rails 1.2.6
24
- ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
25
- protected
26
- def method_missing(method, *args)
27
- if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
28
- super
29
- elsif @reflection.klass.scopes.include?(method)
30
- @reflection.klass.scopes[method].call(self, *args)
31
- else
32
- @reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
33
- @reflection.klass.send(method, *args) { |*a| yield(*a) if block_given? }
34
- end
35
- end
36
- end
37
- end if ActiveRecord::Base.respond_to? :find_first
@@ -1,27 +0,0 @@
1
- module WillPaginate
2
- module PerPage
3
- def per_page
4
- defined?(@per_page) ? @per_page : WillPaginate.per_page
5
- end
6
-
7
- def per_page=(limit)
8
- @per_page = limit.to_i
9
- end
10
-
11
- def self.extended(base)
12
- base.extend Inheritance if base.is_a? Class
13
- end
14
-
15
- module Inheritance
16
- def inherited(subclass)
17
- super
18
- subclass.per_page = self.per_page
19
- end
20
- end
21
- end
22
-
23
- extend PerPage
24
-
25
- # default number of items per page
26
- self.per_page = 30
27
- end
data/test/ci.rb DELETED
@@ -1,60 +0,0 @@
1
- #!/usr/bin/env ruby
2
- databases = %w[ sqlite3 mysql postgres ]
3
- # skip mysql on 1.8.6 (doesn't work for unknown reason)
4
- run_mysql = !(ENV['TRAVIS'] && RUBY_VERSION == '1.8.6')
5
-
6
- def announce(name, msg)
7
- puts "\n\e[1;33m[#{name}] #{msg}\e[m\n"
8
- end
9
-
10
- def rails_version(gemfile)
11
- gemfile =~ /\d[\d.]*$/ ? $& : '2.3'
12
- end
13
-
14
- def system(*args)
15
- puts "$ #{args.join(' ')}"
16
- super
17
- end
18
-
19
- if ENV['TRAVIS']
20
- if run_mysql
21
- system "mysql -e 'create database will_paginate;' >/dev/null"
22
- abort "failed to create mysql database" unless $?.success?
23
- end
24
- system "psql -c 'create database will_paginate;' -U postgres >/dev/null"
25
- abort "failed to create postgres database" unless $?.success?
26
- end
27
-
28
- gemfiles = ['Gemfile']
29
- gemfiles.concat Dir['test/gemfiles/*'].reject { |f| f.include? '.lock' }.sort.reverse
30
-
31
- ruby19 = RUBY_VERSION > '1.9'
32
- ruby19_gemfiles = gemfiles.first
33
-
34
- bundler_options = ENV['TRAVIS'] ? "--path #{Dir.pwd}/vendor/bundle" : ''
35
-
36
- failed = false
37
-
38
- gemfiles.each do |gemfile|
39
- next if ruby19 and !ruby19_gemfiles.include? gemfile
40
- version = rails_version(gemfile)
41
- ENV['BUNDLE_GEMFILE'] = gemfile
42
- skip_install = gemfile == gemfiles.first
43
- if skip_install or system %(bundle install #{bundler_options})
44
- for db in databases
45
- next if 'mysql' == db and !run_mysql
46
- announce "Rails #{version}", "with #{db}"
47
- ENV['DB'] = db
48
- if db == 'postgres' && !ENV['TRAVIS']
49
- system "psql -c 'drop database will_paginate;' &>/dev/null"
50
- system "psql -c 'create database will_paginate;' >/dev/null"
51
- end
52
- failed = true unless system %(bundle exec rake)
53
- end
54
- else
55
- # bundle install failed
56
- failed = true
57
- end
58
- end
59
-
60
- exit 1 if failed
@@ -1,160 +0,0 @@
1
- require 'helper'
2
- require 'will_paginate/array'
3
-
4
- class ArrayPaginationTest < Test::Unit::TestCase
5
-
6
- def setup ; end
7
-
8
- def test_simple
9
- collection = ('a'..'e').to_a
10
-
11
- [{ :page => 1, :per_page => 3, :expected => %w( a b c ) },
12
- { :page => 2, :per_page => 3, :expected => %w( d e ) },
13
- { :page => 1, :per_page => 5, :expected => %w( a b c d e ) },
14
- { :page => 3, :per_page => 5, :expected => [] },
15
- ].
16
- each do |conditions|
17
- expected = conditions.delete :expected
18
- assert_equal expected, collection.paginate(conditions)
19
- end
20
- end
21
-
22
- def test_defaults
23
- result = (1..50).to_a.paginate
24
- assert_equal 1, result.current_page
25
- assert_equal 30, result.size
26
- end
27
-
28
- def test_deprecated_api
29
- assert_raise(ArgumentError) { [].paginate(2) }
30
- assert_raise(ArgumentError) { [].paginate(2, 10) }
31
- end
32
-
33
- def test_total_entries_has_precedence
34
- result = %w(a b c).paginate :total_entries => 5
35
- assert_equal 5, result.total_entries
36
- end
37
-
38
- def test_argument_error_with_params_and_another_argument
39
- assert_raise ArgumentError do
40
- [].paginate({}, 5)
41
- end
42
- end
43
-
44
- def test_paginated_collection
45
- entries = %w(a b c)
46
- collection = create(2, 3, 10) do |pager|
47
- assert_equal entries, pager.replace(entries)
48
- end
49
-
50
- assert_equal entries, collection
51
- assert_respond_to_all collection, %w(total_pages each offset size current_page per_page total_entries)
52
- assert_kind_of Array, collection
53
- assert_instance_of Array, collection.entries
54
- assert_equal 3, collection.offset
55
- assert_equal 4, collection.total_pages
56
- assert !collection.out_of_bounds?
57
- end
58
-
59
- def test_previous_next_pages
60
- collection = create(1, 1, 3)
61
- assert_nil collection.previous_page
62
- assert_equal 2, collection.next_page
63
-
64
- collection = create(2, 1, 3)
65
- assert_equal 1, collection.previous_page
66
- assert_equal 3, collection.next_page
67
-
68
- collection = create(3, 1, 3)
69
- assert_equal 2, collection.previous_page
70
- assert_nil collection.next_page
71
- end
72
-
73
- def test_out_of_bounds
74
- entries = create(2, 3, 2){}
75
- assert entries.out_of_bounds?
76
-
77
- entries = create(1, 3, 2){}
78
- assert !entries.out_of_bounds?
79
- end
80
-
81
- def test_guessing_total_count
82
- entries = create do |pager|
83
- # collection is shorter than limit
84
- pager.replace array
85
- end
86
- assert_equal 8, entries.total_entries
87
-
88
- entries = create(2, 5, 10) do |pager|
89
- # collection is shorter than limit, but we have an explicit count
90
- pager.replace array
91
- end
92
- assert_equal 10, entries.total_entries
93
-
94
- entries = create do |pager|
95
- # collection is the same as limit; we can't guess
96
- pager.replace array(5)
97
- end
98
- assert_equal nil, entries.total_entries
99
-
100
- entries = create do |pager|
101
- # collection is empty; we can't guess
102
- pager.replace array(0)
103
- end
104
- assert_equal nil, entries.total_entries
105
-
106
- entries = create(1) do |pager|
107
- # collection is empty and we're on page 1,
108
- # so the whole thing must be empty, too
109
- pager.replace array(0)
110
- end
111
- assert_equal 0, entries.total_entries
112
- end
113
-
114
- def test_invalid_page
115
- bad_inputs = [0, -1, nil, '', 'Schnitzel']
116
-
117
- bad_inputs.each do |bad|
118
- assert_raise(WillPaginate::InvalidPage) { create bad }
119
- end
120
- end
121
-
122
- def test_invalid_offset
123
- error = assert_raise(WillPaginate::InvalidPage) { create(307445734561825862, 30) }
124
- assert_equal "invalid offset: 9223372036854775830", error.message
125
- end
126
-
127
- def test_page_count_was_removed
128
- assert_raise(NoMethodError) { create.page_count }
129
- # It's `total_pages` now.
130
- end
131
-
132
- def test_inherits_global_per_page
133
- col = WillPaginate::Collection.new(1)
134
- assert_equal 30, col.per_page
135
-
136
- WillPaginate.per_page = 12
137
- begin
138
- col = WillPaginate::Collection.new(1)
139
- assert_equal 12, col.per_page
140
-
141
- col = ('a'..'z').to_a.paginate(:page => 1)
142
- assert_equal 12, col.per_page
143
- ensure
144
- WillPaginate.per_page = 30
145
- end
146
- end
147
-
148
- private
149
- def create(page = 2, limit = 5, total = nil, &block)
150
- if block_given?
151
- WillPaginate::Collection.create(page, limit, total, &block)
152
- else
153
- WillPaginate::Collection.new(page, limit, total)
154
- end
155
- end
156
-
157
- def array(size = 3)
158
- Array.new(size)
159
- end
160
- end
data/test/console DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env ruby
2
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
3
-
4
- opts = %w[ --simple-prompt ]
5
- opts << '-rirb/completion'
6
- opts << '-rlib/load_fixtures'
7
-
8
- exec irb, '-Ilib:test', *opts
data/test/database.yml DELETED
@@ -1,16 +0,0 @@
1
- sqlite3:
2
- database: ":memory:"
3
- adapter: sqlite3
4
- timeout: 500
5
-
6
- mysql:
7
- adapter: mysql
8
- database: will_paginate
9
- username:
10
- encoding: utf8
11
-
12
- postgres:
13
- adapter: postgresql
14
- database: will_paginate
15
- username:
16
- min_messages: warning
data/test/finder_test.rb DELETED
@@ -1,527 +0,0 @@
1
- require 'helper'
2
- require 'lib/activerecord_test_case'
3
-
4
- require 'will_paginate'
5
- WillPaginate.enable_activerecord
6
- WillPaginate.enable_named_scope
7
-
8
- class FinderTest < ActiveRecordTestCase
9
- fixtures :topics, :replies, :users, :projects, :developers_projects
10
-
11
- def test_new_methods_presence
12
- assert_respond_to_all Topic, %w(per_page paginate paginate_by_sql paginate_by_definition_in_class)
13
- end
14
-
15
- def test_simple_paginate
16
- assert_queries(1) do
17
- entries = Topic.paginate :page => nil
18
- assert_equal 1, entries.current_page
19
- assert_equal 1, entries.total_pages
20
- assert_equal 4, entries.size
21
- end
22
-
23
- assert_queries(2) do
24
- entries = Topic.paginate :page => 2
25
- assert_equal 1, entries.total_pages
26
- assert entries.empty?
27
- end
28
- end
29
-
30
- def test_per_page_setting
31
- assert_equal 30, Topic.per_page
32
- Topic.per_page = 12
33
- begin
34
- assert_equal 12, Topic.per_page
35
- ensure
36
- Topic.per_page = 30
37
- end
38
- assert_equal 10, Developer.per_page
39
- end
40
-
41
- def test_per_page_inheritance
42
- subclass = Class.new(Topic)
43
- assert_equal 30, subclass.per_page
44
- subclass.per_page = 12
45
- assert_equal 12, subclass.per_page
46
- assert_equal 30, Topic.per_page
47
- sub_subclass = Class.new(subclass)
48
- assert_equal 12, sub_subclass.per_page
49
- end
50
-
51
- def test_parameter_api
52
- # :page parameter in options is required!
53
- assert_raise(ArgumentError){ Topic.paginate }
54
- assert_raise(ArgumentError){ Topic.paginate({}) }
55
-
56
- # explicit :all should not break anything
57
- assert_equal Topic.paginate(:page => nil), Topic.paginate(:all, :page => 1)
58
-
59
- # :count could be nil and we should still not cry
60
- assert_nothing_raised { Topic.paginate :page => 1, :count => nil }
61
- end
62
-
63
- def test_counting_when_integer_has_length_method
64
- Integer.module_eval { def length; to_s.length; end }
65
- begin
66
- assert_equal 2, 11.length
67
- entries = Developer.paginate :page => 1, :per_page => 5
68
- assert_equal 11, entries.total_entries
69
- assert_equal 5, entries.size
70
- assert_equal 3, entries.total_pages
71
- ensure
72
- begin
73
- Integer.module_eval { remove_method :length }
74
- rescue
75
- end
76
- end
77
- end
78
-
79
- def test_paginate_with_per_page
80
- entries = Topic.paginate :page => 1, :per_page => 1
81
- assert_equal 1, entries.size
82
- assert_equal 4, entries.total_pages
83
-
84
- # Developer class has explicit per_page at 10
85
- entries = Developer.paginate :page => 1
86
- assert_equal 10, entries.size
87
- assert_equal 2, entries.total_pages
88
-
89
- entries = Developer.paginate :page => 1, :per_page => 5
90
- assert_equal 11, entries.total_entries
91
- assert_equal 5, entries.size
92
- assert_equal 3, entries.total_pages
93
- end
94
-
95
- def test_paginate_with_order
96
- entries = Topic.paginate :page => 1, :order => 'created_at desc'
97
- expected = [topics(:futurama), topics(:harvey_birdman), topics(:rails), topics(:ar)].reverse
98
- assert_equal expected, entries.to_a
99
- assert_equal 1, entries.total_pages
100
- end
101
-
102
- def test_paginate_with_conditions
103
- entries = Topic.paginate :page => 1, :conditions => ["created_at > ?", 30.minutes.ago], :order => :id
104
- expected = [topics(:rails), topics(:ar)]
105
- assert_equal expected, entries.to_a
106
- assert_equal 1, entries.total_pages
107
- end
108
-
109
- def test_paginate_with_include_and_conditions
110
- entries = Topic.paginate \
111
- :page => 1,
112
- :include => :replies,
113
- :conditions => "replies.content LIKE 'Bird%' ",
114
- :per_page => 10
115
-
116
- expected = Topic.find :all,
117
- :include => 'replies',
118
- :conditions => "replies.content LIKE 'Bird%' ",
119
- :limit => 10
120
-
121
- assert_equal expected, entries.to_a
122
- assert_equal 1, entries.total_entries
123
- end
124
-
125
- def test_paginate_with_include_and_order
126
- entries = nil
127
- assert_queries(2) do
128
- entries = Topic.paginate \
129
- :page => 1,
130
- :include => :replies,
131
- :order => 'replies.created_at asc, topics.created_at asc',
132
- :per_page => 10
133
- end
134
-
135
- expected = Topic.find :all,
136
- :include => 'replies',
137
- :order => 'replies.created_at asc, topics.created_at asc',
138
- :limit => 10
139
-
140
- assert_equal expected, entries.to_a
141
- assert_equal 4, entries.total_entries
142
- end
143
-
144
- def test_paginate_associations_with_include
145
- entries, project = nil, projects(:active_record)
146
-
147
- assert_nothing_raised "THIS IS A BUG in Rails 1.2.3 that was fixed in [7326]. " +
148
- "Please upgrade to a newer version of Rails." do
149
- entries = project.topics.paginate \
150
- :page => 1,
151
- :include => :replies,
152
- :conditions => "replies.content LIKE 'Nice%' ",
153
- :per_page => 10
154
- end
155
-
156
- expected = Topic.find :all,
157
- :include => 'replies',
158
- :conditions => "project_id = #{project.id} AND replies.content LIKE 'Nice%' ",
159
- :limit => 10
160
-
161
- assert_equal expected, entries.to_a
162
- end
163
-
164
- def test_paginate_associations
165
- dhh = users :david
166
- expected_name_ordered = [projects(:action_controller), projects(:active_record)]
167
- expected_id_ordered = [projects(:active_record), projects(:action_controller)]
168
-
169
- assert_queries(2) do
170
- # with association-specified order
171
- entries = dhh.projects.paginate(:page => 1)
172
- assert_equal expected_name_ordered, entries
173
- assert_equal 2, entries.total_entries
174
- end
175
-
176
- # with explicit order
177
- entries = dhh.projects.paginate(:page => 1, :order => 'projects.id')
178
- assert_equal expected_id_ordered, entries
179
- assert_equal 2, entries.total_entries
180
-
181
- assert_nothing_raised { dhh.projects.find(:all, :order => 'projects.id', :limit => 4) }
182
- entries = dhh.projects.paginate(:page => 1, :order => 'projects.id', :per_page => 4)
183
- assert_equal expected_id_ordered, entries
184
-
185
- # has_many with implicit order
186
- topic = Topic.find(1)
187
- expected = [replies(:spam), replies(:witty_retort)]
188
- assert_equal expected.map(&:id).sort, topic.replies.paginate(:page => 1).map(&:id).sort
189
- assert_equal expected.reverse, topic.replies.paginate(:page => 1, :order => 'replies.id ASC')
190
- end
191
-
192
- def test_paginate_association_extension
193
- project = Project.find(:first, :order => :id)
194
-
195
- assert_queries(2) do
196
- entries = project.replies.paginate_recent :page => 1
197
- assert_equal [replies(:brave)], entries
198
- end
199
- end
200
-
201
- def test_paginate_with_joins
202
- entries = nil
203
-
204
- assert_queries(1) do
205
- entries = Developer.paginate :page => 1,
206
- :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
207
- :conditions => 'project_id = 1'
208
- assert_equal 2, entries.size
209
- developer_names = entries.map &:name
210
- assert developer_names.include?('David')
211
- assert developer_names.include?('Jamis')
212
- end
213
-
214
- assert_queries(1) do
215
- expected = entries.to_a
216
- entries = Developer.paginate :page => 1,
217
- :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
218
- :conditions => 'project_id = 1', :count => { :select => "users.id" }
219
- assert_equal expected, entries.to_a
220
- assert_equal 2, entries.total_entries
221
- end
222
- end
223
-
224
- def test_paginate_with_group
225
- entries = nil
226
- assert_queries(1) do
227
- entries = Developer.paginate :page => 1, :per_page => 10,
228
- :group => 'salary', :select => 'salary', :order => 'salary'
229
- end
230
-
231
- expected = [ users(:david), users(:jamis), users(:dev_10), users(:poor_jamis) ].map(&:salary).sort
232
- assert_equal expected, entries.map(&:salary)
233
- end
234
-
235
- def test_paginate_with_dynamic_finder
236
- expected = [replies(:witty_retort), replies(:spam)]
237
- assert_equal expected, Reply.paginate_by_topic_id(1, :page => 1, :order => :created_at)
238
-
239
- entries = Developer.paginate :conditions => { :salary => 100000 }, :page => 1, :per_page => 5
240
- assert_equal 8, entries.total_entries
241
- assert_equal entries, Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
242
-
243
- # dynamic finder + conditions
244
- entries = Developer.paginate_by_salary(100000, :page => 1, :order => :id,
245
- :conditions => ['id > ?', 6])
246
- assert_equal 4, entries.total_entries
247
- assert_equal (7..10).to_a, entries.map(&:id)
248
-
249
- assert_raises NoMethodError do
250
- Developer.paginate_by_inexistent_attribute 100000, :page => 1
251
- end
252
- end
253
-
254
- def test_scoped_paginate
255
- entries = Developer.with_poor_ones { Developer.paginate :page => 1 }
256
-
257
- assert_equal 2, entries.size
258
- assert_equal 2, entries.total_entries
259
- end
260
-
261
- ## named_scope ##
262
-
263
- def test_paginate_in_named_scope
264
- entries = Developer.poor.paginate :page => 1, :per_page => 1
265
-
266
- assert_equal 1, entries.size
267
- assert_equal 2, entries.total_entries
268
- end
269
-
270
- # fat chance I'll ever get to debugging and fixing this
271
- unless '1.8.7' == RUBY_VERSION and ActiveRecord::VERSION::STRING < '2.2'
272
- def test_paginate_in_named_scope_on_habtm_association
273
- project = projects(:active_record)
274
- assert_queries(2) do
275
- entries = project.developers.poor.paginate :page => 1, :per_page => 1
276
-
277
- assert_equal 1, entries.size, 'one developer should be found'
278
- assert_equal 1, entries.total_entries, 'only one developer should be found'
279
- end
280
- end
281
- end
282
-
283
- def test_paginate_in_named_scope_on_hmt_association
284
- project = projects(:active_record)
285
- expected = [replies(:brave)]
286
-
287
- assert_queries(2) do
288
- entries = project.replies.recent.paginate :page => 1, :per_page => 1
289
- assert_equal expected, entries
290
- assert_equal 1, entries.total_entries, 'only one reply should be found'
291
- end
292
- end
293
-
294
- def test_paginate_in_named_scope_on_has_many_association
295
- project = projects(:active_record)
296
- expected = [topics(:ar)]
297
-
298
- assert_queries(2) do
299
- entries = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
300
- assert_equal expected, entries
301
- assert_equal 1, entries.total_entries, 'only one topic should be found'
302
- end
303
- end
304
-
305
- def test_named_scope_with_include
306
- project = projects(:active_record)
307
- entries = project.topics.with_replies_starting_with('AR ').paginate(:page => 1, :per_page => 1)
308
- assert_equal 1, entries.size
309
- end
310
-
311
- def test_paginated_each_with_scoped_order
312
- paginated_developers = []
313
- Developer.poor.paginated_each {|d| paginated_developers << d}
314
-
315
- assert_equal Developer.poor.find(:all), paginated_developers, 'should use scoped :order option'
316
- end
317
-
318
- ## misc ##
319
-
320
- def test_count_and_total_entries_options_are_mutually_exclusive
321
- e = assert_raise ArgumentError do
322
- Developer.paginate :page => 1, :count => {}, :total_entries => 1
323
- end
324
- assert_match /exclusive/, e.to_s
325
- end
326
-
327
- def test_readonly
328
- assert_nothing_raised { Developer.paginate :readonly => true, :page => 1 }
329
- end
330
-
331
- # this functionality is temporarily removed
332
- def xtest_pagination_defines_method
333
- pager = "paginate_by_created_at"
334
- assert !User.methods.include_method?(pager), "User methods should not include `#{pager}` method"
335
- # paginate!
336
- assert 0, User.send(pager, nil, :page => 1).total_entries
337
- # the paging finder should now be defined
338
- assert User.methods.include_method?(pager), "`#{pager}` method should be defined on User"
339
- end
340
-
341
- # Is this Rails 2.0? Find out by testing find_all which was removed in [6998]
342
- unless ActiveRecord::Base.respond_to? :find_all
343
- def test_paginate_array_of_ids
344
- # AR finders also accept arrays of IDs
345
- # (this was broken in Rails before [6912])
346
- assert_queries(1) do
347
- entries = Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
348
- assert_equal (4..6).to_a, entries.map(&:id)
349
- assert_equal 8, entries.total_entries
350
- end
351
- end
352
- end
353
-
354
- uses_mocha 'internals' do
355
- def test_implicit_all_with_dynamic_finders
356
- Topic.expects(:find_all_by_foo).returns([])
357
- Topic.expects(:count).returns(0)
358
- Topic.paginate_by_foo :page => 2
359
- end
360
-
361
- def test_guessing_the_total_count
362
- Topic.expects(:find).returns(Array.new(2))
363
- Topic.expects(:count).never
364
-
365
- entries = Topic.paginate :page => 2, :per_page => 4
366
- assert_equal 6, entries.total_entries
367
- end
368
-
369
- def test_guessing_that_there_are_no_records
370
- Topic.expects(:find).returns([])
371
- Topic.expects(:count).never
372
-
373
- entries = Topic.paginate :page => 1, :per_page => 4
374
- assert_equal 0, entries.total_entries
375
- end
376
-
377
- def test_extra_parameters_stay_untouched
378
- Topic.expects(:find).with(:all, {:foo => 'bar', :limit => 4, :offset => 0 }).returns(Array.new(5))
379
- Topic.expects(:count).with({:foo => 'bar'}).returns(1)
380
-
381
- Topic.paginate :foo => 'bar', :page => 1, :per_page => 4
382
- end
383
-
384
- def test_count_skips_select
385
- Developer.stubs(:find).returns([])
386
- Developer.expects(:count).with({}).returns(0)
387
- Developer.paginate :select => 'salary', :page => 2
388
- end
389
-
390
- def test_count_select_when_distinct
391
- Developer.stubs(:find).returns([])
392
- Developer.expects(:count).with(:select => 'DISTINCT salary').returns(0)
393
- Developer.paginate :select => 'DISTINCT salary', :page => 2
394
- end
395
-
396
- def test_count_with_scoped_select_when_distinct
397
- Developer.stubs(:find).returns([])
398
- Developer.expects(:count).with(:select => 'DISTINCT users.id').returns(0)
399
- Developer.distinct.paginate :page => 2
400
- end
401
-
402
- def test_should_use_scoped_finders_if_present
403
- # scope-out compatibility
404
- Topic.expects(:find_best).returns(Array.new(5))
405
- Topic.expects(:with_best).returns(1)
406
-
407
- Topic.paginate_best :page => 1, :per_page => 4
408
- end
409
-
410
- def test_paginate_by_sql
411
- sql = "SELECT * FROM users WHERE type = 'Developer' ORDER BY id"
412
- entries = Developer.paginate_by_sql(sql, :page => 2, :per_page => 3)
413
- assert_equal 11, entries.total_entries
414
- assert_equal [users(:dev_4), users(:dev_5), users(:dev_6)], entries
415
- end
416
-
417
- def test_paginate_by_sql_respects_total_entries_setting
418
- sql = "SELECT * FROM users"
419
- entries = Developer.paginate_by_sql(sql, :page => 1, :total_entries => 999)
420
- assert_equal 999, entries.total_entries
421
- end
422
-
423
- def test_paginate_by_sql_strips_order_by_when_counting
424
- Developer.expects(:find_by_sql).returns([])
425
- Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n ) AS count_table").returns(0)
426
-
427
- Developer.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 2
428
- end
429
-
430
- # TODO: counts are still wrong
431
- def test_ability_to_use_with_custom_finders
432
- # acts_as_taggable defines find_tagged_with(tag, options)
433
- Topic.expects(:find_tagged_with).with('will_paginate', :offset => 5, :limit => 5).returns([])
434
- Topic.expects(:count).with({}).returns(0)
435
-
436
- Topic.paginate_tagged_with 'will_paginate', :page => 2, :per_page => 5
437
- end
438
-
439
- def test_array_argument_doesnt_eliminate_count
440
- ids = (1..8).to_a
441
- Developer.expects(:find_all_by_id).returns([])
442
- Developer.expects(:count).returns(0)
443
-
444
- Developer.paginate_by_id(ids, :per_page => 3, :page => 2, :order => 'id')
445
- end
446
-
447
- def test_paginating_finder_doesnt_mangle_options
448
- Developer.expects(:find).returns([])
449
- options = { :page => 1, :per_page => 2, :foo => 'bar' }
450
- options_before = options.dup
451
-
452
- Developer.paginate(options)
453
- assert_equal options_before, options
454
- end
455
-
456
- def test_paginate_by_sql_doesnt_change_original_query
457
- query = 'SQL QUERY'
458
- original_query = query.dup
459
- Developer.expects(:find_by_sql).returns([])
460
-
461
- Developer.paginate_by_sql query, :page => 1
462
- assert_equal original_query, query
463
- end
464
-
465
- def test_paginated_each
466
- collection = stub('collection', :size => 5, :empty? => false, :per_page => 5)
467
- collection.expects(:each).times(2).returns(collection)
468
- last_collection = stub('collection', :size => 4, :empty? => false, :per_page => 5)
469
- last_collection.expects(:each).returns(last_collection)
470
-
471
- params = { :order => 'id', :total_entries => 0 }
472
-
473
- Developer.expects(:paginate).with(params.merge(:page => 2)).returns(collection)
474
- Developer.expects(:paginate).with(params.merge(:page => 3)).returns(collection)
475
- Developer.expects(:paginate).with(params.merge(:page => 4)).returns(last_collection)
476
-
477
- assert_equal 14, Developer.paginated_each(:page => '2') { }
478
- end
479
-
480
- def test_paginated_each_with_named_scope
481
- assert_equal 2, Developer.poor.paginated_each(:per_page => 1) {
482
- assert_equal 11, Developer.count
483
- }
484
- end
485
-
486
- # detect ActiveRecord 2.1
487
- if ActiveRecord::Base.private_methods.include_method?(:references_eager_loaded_tables?)
488
- def test_removes_irrelevant_includes_in_count
489
- Developer.expects(:find).returns([1])
490
- Developer.expects(:count).with({}).returns(0)
491
-
492
- Developer.paginate :page => 1, :per_page => 1, :include => :projects
493
- end
494
-
495
- def test_doesnt_remove_referenced_includes_in_count
496
- Developer.expects(:find).returns([1])
497
- Developer.expects(:count).with({ :include => :projects, :conditions => 'projects.id > 2' }).returns(0)
498
-
499
- Developer.paginate :page => 1, :per_page => 1,
500
- :include => :projects, :conditions => 'projects.id > 2'
501
- end
502
- end
503
-
504
- def test_paginate_from
505
- result = Developer.paginate(:from => 'users', :page => 1, :per_page => 1)
506
- assert_equal 1, result.size
507
- end
508
-
509
- def test_hmt_with_include
510
- # ticket #220
511
- reply = projects(:active_record).replies.find(:first, :order => 'replies.id')
512
- assert_equal replies(:decisive), reply
513
-
514
- # ticket #223
515
- Project.find(1, :include => :replies)
516
-
517
- # I cannot reproduce any of the failures from those reports :(
518
- end
519
-
520
- def test_hmt_with_uniq
521
- project = Project.find(1)
522
- result = project.unique_replies.paginate :page => 1, :per_page => 1,
523
- :order => 'replies.id'
524
- assert_equal replies(:decisive), result.first
525
- end
526
- end
527
- end