Skip to content
Browse files

Support paginate(:total_entries => n) for dm

  • Loading branch information...
1 parent 217b1a0 commit d14eccc5782eeed8a8f6b1a205c53d562c6c123e @phuesler phuesler committed
Showing with 82 additions and 63 deletions.
  1. +6 −1 lib/will_paginate/data_mapper.rb
  2. +1 −61 spec/finders/active_record_spec.rb
  3. +13 −0 spec/finders/data_mapper_spec.rb
  4. +62 −1 spec/spec_helper.rb
View
7 lib/will_paginate/data_mapper.rb
@@ -21,11 +21,15 @@ def paginate(options)
options = options.dup
pagenum = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
per_page = options.delete(:per_page) || self.per_page
+ total = options.delete(:total_entries)
options.delete(:page)
options[:limit] = per_page.to_i
- all(options).page(pagenum)
+
+ col = all(options).page(pagenum)
+ col.total_entries = total.to_i unless total.nil? || (total.kind_of?(String) && total.strip.empty?)
+ col
end
end
@@ -33,6 +37,7 @@ module CollectionMethods
include WillPaginate::CollectionMethods
attr_accessor :current_page
+ attr_writer :total_entries
def paginated?
!current_page.nil?
View
62 spec/finders/active_record_spec.rb
@@ -498,64 +498,4 @@
Project.page(307445734561825862)
}.should raise_error(WillPaginate::InvalidPage, "invalid offset: 9223372036854775830")
end
-
- protected
-
- def ignore_deprecation
- ActiveSupport::Deprecation.silence { yield }
- end
-
- def run_queries(num)
- QueryCountMatcher.new(num)
- end
-
- def show_queries(&block)
- counter = QueryCountMatcher.new(nil)
- counter.run block
- ensure
- queries = counter.performed_queries
- if queries.any?
- puts queries
- else
- puts "no queries"
- end
- end
-
-end
-
-class QueryCountMatcher
- def initialize(num)
- @expected_count = num
- end
-
- def matches?(block)
- run(block)
-
- if @expected_count.respond_to? :include?
- @expected_count.include? @count
- else
- @count == @expected_count
- end
- end
-
- def run(block)
- $query_count = 0
- $query_sql = []
- block.call
- ensure
- @queries = $query_sql.dup
- @count = $query_count
- end
-
- def performed_queries
- @queries
- end
-
- def failure_message
- "expected #{@expected_count} queries, got #{@count}\n#{@queries.join("\n")}"
- end
-
- def negative_failure_message
- "expected query count not to be #{@expected_count}"
- end
-end
+ end
View
13 spec/finders/data_mapper_spec.rb
@@ -80,6 +80,19 @@
Animal.all(:conditions => ['1=2']).page(1).total_pages.should == 1
end
+ it "overrides total_entries count with a fixed value" do
+ lambda {
+ animals = Animal.paginate :page => 1, :per_page => 3, :total_entries => 999
+ animals.total_entries.should == 999
+ }.should run_queries(0)
+ end
+
+ it "supports a non-int for total_entries" do
+ topics = Animal.paginate :page => 1, :per_page => 3, :total_entries => "999"
+ topics.total_entries.should == 999
+ end
+
+
it "can iterate and then call WP methods" do
animals = Animal.all(:limit => 2).page(1)
animals.each { |a| }
View
63 spec/spec_helper.rb
@@ -17,8 +17,32 @@ def include_phrase(string)
def have_deprecation(msg)
DeprecationMatcher.new(msg)
end
+
+ def run_queries(num)
+ QueryCountMatcher.new(num)
+ end
+
+ def ignore_deprecation
+ ActiveSupport::Deprecation.silence { yield }
+ end
+
+ def run_queries(num)
+ QueryCountMatcher.new(num)
+ end
+
+ def show_queries(&block)
+ counter = QueryCountMatcher.new(nil)
+ counter.run block
+ ensure
+ queries = counter.performed_queries
+ if queries.any?
+ puts queries
+ else
+ puts "no queries"
+ end
+ end
}
-
+
config.mock_with :mocha
end
@@ -69,3 +93,40 @@ def hijack_stderr
$stderr = err
end
end
+
+class QueryCountMatcher
+ def initialize(num)
+ @expected_count = num
+ end
+
+ def matches?(block)
+ run(block)
+
+ if @expected_count.respond_to? :include?
+ @expected_count.include? @count
+ else
+ @count == @expected_count
+ end
+ end
+
+ def run(block)
+ $query_count = 0
+ $query_sql = []
+ block.call
+ ensure
+ @queries = $query_sql.dup
+ @count = $query_count
+ end
+
+ def performed_queries
+ @queries
+ end
+
+ def failure_message
+ "expected #{@expected_count} queries, got #{@count}\n#{@queries.join("\n")}"
+ end
+
+ def negative_failure_message
+ "expected query count not to be #{@expected_count}"
+ end
+end

0 comments on commit d14eccc

Please sign in to comment.
Something went wrong with that request. Please try again.