will_paginate 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


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

@@ -73,7 +73,7 @@ module WillPaginate
73
73
 
74
74
  # workaround for Active Record 3.0
75
75
  def size
76
- if !loaded? and limit_value
76
+ if !loaded? and limit_value and group_values.empty?
77
77
  [super, limit_value].min
78
78
  else
79
79
  super
@@ -83,12 +83,9 @@ module WillPaginate
83
83
  # overloaded to be pagination-aware
84
84
  def empty?
85
85
  if !loaded? and offset_value
86
- rel_count = count
87
- if rel_count.respond_to?(:size) and !rel_count.is_a?(Integer)
88
- rel_count.size <= offset_value
89
- else
90
- rel_count <= offset_value
91
- end
86
+ result = count
87
+ result = result.size if result.respond_to?(:size) and !result.is_a?(Integer)
88
+ result <= offset_value
92
89
  else
93
90
  super
94
91
  end
@@ -167,25 +164,34 @@ module WillPaginate
167
164
  # application.
168
165
  #
169
166
  def paginate_by_sql(sql, options)
170
- pagenum = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
167
+ pagenum = options.fetch(:page) { raise ArgumentError, ":page parameter required" } || 1
171
168
  per_page = options[:per_page] || self.per_page
172
169
  total = options[:total_entries]
173
170
 
174
171
  WillPaginate::Collection.create(pagenum, per_page, total) do |pager|
175
172
  query = sanitize_sql(sql.dup)
176
173
  original_query = query.dup
174
+ oracle = self.connection.adapter_name =~ /^(oracle|oci$)/i
175
+
177
176
  # add limit, offset
178
- query << " LIMIT #{pager.per_page} OFFSET #{pager.offset}"
177
+ if oracle
178
+ query = <<-SQL
179
+ SELECT * FROM (
180
+ SELECT rownum rnum, a.* FROM (#{query}) a
181
+ WHERE rownum <= #{pager.offset + pager.per_page}
182
+ ) WHERE rnum >= #{pager.offset}
183
+ SQL
184
+ else
185
+ query << " LIMIT #{pager.per_page} OFFSET #{pager.offset}"
186
+ end
187
+
179
188
  # perfom the find
180
189
  pager.replace find_by_sql(query)
181
190
 
182
191
  unless pager.total_entries
183
- count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s]+$/mi, ''
192
+ count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s.]+$/mi, ''
184
193
  count_query = "SELECT COUNT(*) FROM (#{count_query})"
185
-
186
- unless self.connection.adapter_name =~ /^(oracle|oci$)/i
187
- count_query << ' AS count_table'
188
- end
194
+ count_query << ' AS count_table' unless oracle
189
195
  # perform the count query
190
196
  pager.total_entries = count_by_sql(count_query)
191
197
  end
@@ -2,7 +2,7 @@ module WillPaginate #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -146,6 +146,13 @@ describe WillPaginate::ActiveRecord do
146
146
  }.should run_queries(1)
147
147
  end
148
148
 
149
+ it "supports `size` for grouped queries" do
150
+ topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
151
+ lambda {
152
+ topics.size.should == {nil=>2, 1=>2}
153
+ }.should run_queries(1)
154
+ end
155
+
149
156
  it "overrides total_entries count with a fixed value" do
150
157
  lambda {
151
158
  topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => 999
@@ -212,14 +219,24 @@ describe WillPaginate::ActiveRecord do
212
219
  }.should run_queries(1)
213
220
  end
214
221
 
222
+ it "defaults to page 1" do
223
+ sql = "select content from topics"
224
+ topics = Topic.paginate_by_sql sql, :page => nil, :per_page => 1
225
+ topics.current_page.should == 1
226
+ topics.size.should == 1
227
+ end
228
+
215
229
  it "should strip the order when counting" do
216
230
  lambda {
217
- sql = "select id, title, content from topics order by title"
231
+ sql = "select id, title, content from topics order by topics.title"
218
232
  topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
219
233
  topics.first.should == topics(:ar)
220
234
  }.should run_queries(2)
235
+
236
+ $query_sql.last.should include('COUNT')
237
+ $query_sql.last.should_not include('order by topics.title')
221
238
  end
222
-
239
+
223
240
  it "shouldn't change the original query string" do
224
241
  query = 'select * from topics where 1 = 2'
225
242
  original_query = query.dup
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: will_paginate
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.0.1
5
+ version: 3.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Mislav Marohni\xC4\x87"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-09-17 00:00:00 Z
13
+ date: 2011-09-27 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: will_paginate provides a simple API for performing paginated queries with Active Record, DataMapper and Sequel, and includes helpers for rendering pagination links in Rails, Sinatra and Merb web apps.