[DRE-commits] [ruby-will-paginate] 01/02: Imported Upstream version 3.1.0

Abhijith PA abhijithpa-guest at moszumanska.debian.org
Mon Mar 7 17:08:50 UTC 2016


This is an automated email from the git hooks/post-receive script.

abhijithpa-guest pushed a commit to branch master
in repository ruby-will-paginate.

commit 4d0b0cf927749b37b9d41834b7527195c5be79ec
Author: Abhijith PA <abhijith at openmailbox.org>
Date:   Mon Mar 7 22:36:55 2016 +0530

    Imported Upstream version 3.1.0
---
 README.md                                   |   2 +-
 checksums.yaml.gz                           | Bin 268 -> 0 bytes
 lib/will_paginate/active_record.rb          |  19 +--
 lib/will_paginate/data_mapper.rb            |   7 +-
 lib/will_paginate/deprecation.rb            |   2 +-
 lib/will_paginate/mongoid.rb                |  46 ++++++++
 lib/will_paginate/railtie.rb                |   7 +-
 lib/will_paginate/version.rb                |   4 +-
 metadata.yml                                |   8 +-
 spec/finders/active_record_spec.rb          | 177 ++--------------------------
 spec/finders/activerecord_test_connector.rb |  13 +-
 spec/finders/data_mapper_spec.rb            |  13 ++
 spec/finders/mongoid_spec.rb                | 140 ++++++++++++++++++++++
 spec/fixtures/admin.rb                      |   2 +-
 spec/fixtures/developer.rb                  |   9 +-
 spec/fixtures/project.rb                    |   2 +-
 spec/page_number_spec.rb                    |  40 +++++--
 spec/spec_helper.rb                         |  22 +++-
 spec/view_helpers/action_view_spec.rb       |  50 +++++---
 spec/view_helpers/base_spec.rb              |   1 +
 spec/view_helpers/view_example_group.rb     |  32 ++++-
 21 files changed, 356 insertions(+), 240 deletions(-)

diff --git a/README.md b/README.md
index 9a9baa6..13a0972 100644
--- a/README.md
+++ b/README.md
@@ -58,4 +58,4 @@ Happy paginating.
 [install]: https://github.com/mislav/will_paginate/wiki/Installation "will_paginate installation"
 [group]: http://groups.google.com/group/will_paginate "will_paginate discussion and support group"
 [issues]: https://github.com/mislav/will_paginate/issues
-[css]: http://mislav.uniqpath.com/will_paginate/
+[css]: http://mislav.github.io/will_paginate/
diff --git a/checksums.yaml.gz b/checksums.yaml.gz
deleted file mode 100644
index 2619a55..0000000
Binary files a/checksums.yaml.gz and /dev/null differ
diff --git a/lib/will_paginate/active_record.rb b/lib/will_paginate/active_record.rb
index 5fb2ece..384d306 100644
--- a/lib/will_paginate/active_record.rb
+++ b/lib/will_paginate/active_record.rb
@@ -2,11 +2,6 @@ require 'will_paginate/per_page'
 require 'will_paginate/page_number'
 require 'will_paginate/collection'
 require 'active_record'
-begin
-  require 'active_record/deprecated_finders'
-rescue LoadError
-  # only for Rails 4.1
-end
 
 module WillPaginate
   # = Paginating finders for ActiveRecord models
@@ -26,7 +21,7 @@ module WillPaginate
       include WillPaginate::CollectionMethods
 
       attr_accessor :current_page
-      attr_writer :total_entries, :wp_count_options
+      attr_writer :total_entries
 
       def per_page(value = nil)
         if value.nil? then limit_value
@@ -88,9 +83,6 @@ module WillPaginate
           excluded = [:order, :limit, :offset, :reorder]
           excluded << :includes unless eager_loading?
           rel = self.except(*excluded)
-          # TODO: hack. decide whether to keep
-          rel = rel.apply_finder_options(@wp_count_options) if defined? @wp_count_options
-          
           column_name = (select_for_count(rel) || :all)
           rel.count(column_name)
         else
@@ -142,7 +134,6 @@ module WillPaginate
       def copy_will_paginate_data(other)
         other.current_page = current_page unless other.current_page
         other.total_entries = nil if defined? @total_entries_queried
-        other.wp_count_options = @wp_count_options if defined? @wp_count_options
         other
       end
       
@@ -158,15 +149,15 @@ module WillPaginate
       def paginate(options)
         options  = options.dup
         pagenum  = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
+        options.delete(:page)
         per_page = options.delete(:per_page) || self.per_page
         total    = options.delete(:total_entries)
 
-        count_options = options.delete(:count)
-        options.delete(:page)
+        if options.any?
+          raise ArgumentError, "unsupported parameters: %p" % options.keys
+        end
 
         rel = limit(per_page.to_i).page(pagenum)
-        rel = rel.apply_finder_options(options) if options.any?
-        rel.wp_count_options = count_options    if count_options
         rel.total_entries = total.to_i          unless total.blank?
         rel
       end
diff --git a/lib/will_paginate/data_mapper.rb b/lib/will_paginate/data_mapper.rb
index 13f4364..6156214 100644
--- a/lib/will_paginate/data_mapper.rb
+++ b/lib/will_paginate/data_mapper.rb
@@ -21,11 +21,15 @@ module WillPaginate
         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 WillPaginate
       include WillPaginate::CollectionMethods
 
       attr_accessor :current_page
+      attr_writer :total_entries
 
       def paginated?
         !current_page.nil?
diff --git a/lib/will_paginate/deprecation.rb b/lib/will_paginate/deprecation.rb
index aa66e6a..848ecf8 100644
--- a/lib/will_paginate/deprecation.rb
+++ b/lib/will_paginate/deprecation.rb
@@ -10,7 +10,7 @@ module WillPaginate::Deprecation
     private
 
     def rails_logger
-      defined?(Rails) && Rails.logger
+      defined?(Rails.logger) && Rails.logger
     end
 
     def origin_of_call(stack)
diff --git a/lib/will_paginate/mongoid.rb b/lib/will_paginate/mongoid.rb
new file mode 100644
index 0000000..5792412
--- /dev/null
+++ b/lib/will_paginate/mongoid.rb
@@ -0,0 +1,46 @@
+require 'mongoid'
+require 'will_paginate/collection'
+
+module WillPaginate
+  module Mongoid
+    module CriteriaMethods
+      def paginate(options = {})
+        extend CollectionMethods
+        @current_page = WillPaginate::PageNumber(options[:page] || @current_page || 1)
+        @page_multiplier = current_page - 1
+        pp = (options[:per_page] || per_page || WillPaginate.per_page).to_i
+        limit(pp).skip(@page_multiplier * pp)
+      end
+
+      def per_page(value = :non_given)
+        if value == :non_given
+          options[:limit] == 0 ? nil : options[:limit] # in new Mongoid versions a nil limit is saved as 0
+        else
+          limit(value)
+        end
+      end
+
+      def page(page)
+        paginate(:page => page)
+      end
+    end
+
+    module CollectionMethods
+      attr_reader :current_page
+
+      def total_entries
+        @total_entries ||= count
+      end
+
+      def total_pages
+        (total_entries / per_page.to_f).ceil
+      end
+
+      def offset
+        @page_multiplier * per_page
+      end
+    end
+
+    ::Mongoid::Criteria.send(:include, CriteriaMethods)
+  end
+end
diff --git a/lib/will_paginate/railtie.rb b/lib/will_paginate/railtie.rb
index 72137fd..bed794f 100644
--- a/lib/will_paginate/railtie.rb
+++ b/lib/will_paginate/railtie.rb
@@ -32,14 +32,17 @@ module WillPaginate
     end
 
     def self.add_locale_path(config)
-      config.i18n.railties_load_path.unshift(*WillPaginate::I18n.load_path)
+      config.i18n.load_path.unshift(*WillPaginate::I18n.load_path)
     end
 
     # Extending the exception handler middleware so it properly detects
     # WillPaginate::InvalidPage regardless of it being a tag module.
     module ShowExceptionsPatch
       extend ActiveSupport::Concern
-      included { alias_method_chain :status_code, :paginate }
+      included do
+        alias_method :status_code_without_paginate, :status_code
+        alias_method :status_code, :status_code_with_paginate
+      end
       def status_code_with_paginate(exception = @exception)
         if exception.is_a?(WillPaginate::InvalidPage) or
             (exception.respond_to?(:original_exception) &&
diff --git a/lib/will_paginate/version.rb b/lib/will_paginate/version.rb
index 4d6770f..4149c6b 100644
--- a/lib/will_paginate/version.rb
+++ b/lib/will_paginate/version.rb
@@ -1,8 +1,8 @@
 module WillPaginate #:nodoc:
   module VERSION #:nodoc:
     MAJOR = 3
-    MINOR = 0
-    TINY  = 7
+    MINOR = 1
+    TINY  = 0
 
     STRING = [MAJOR, MINOR, TINY].join('.')
   end
diff --git a/metadata.yml b/metadata.yml
index f734e2e..4aa4923 100644
--- a/metadata.yml
+++ b/metadata.yml
@@ -1,14 +1,14 @@
 --- !ruby/object:Gem::Specification
 name: will_paginate
 version: !ruby/object:Gem::Version
-  version: 3.0.7
+  version: 3.1.0
 platform: ruby
 authors:
 - Mislav Marohnić
 autorequire: 
 bindir: bin
 cert_chain: []
-date: 2014-07-04 00:00:00.000000000 Z
+date: 2016-01-03 00:00:00.000000000 Z
 dependencies: []
 description: will_paginate provides a simple API for performing paginated queries
   with Active Record, DataMapper and Sequel, and includes helpers for rendering pagination
@@ -31,6 +31,7 @@ files:
 - lib/will_paginate/deprecation.rb
 - lib/will_paginate/i18n.rb
 - lib/will_paginate/locale/en.yml
+- lib/will_paginate/mongoid.rb
 - lib/will_paginate/page_number.rb
 - lib/will_paginate/per_page.rb
 - lib/will_paginate/railtie.rb
@@ -51,6 +52,7 @@ files:
 - spec/finders/activerecord_test_connector.rb
 - spec/finders/data_mapper_spec.rb
 - spec/finders/data_mapper_test_connector.rb
+- spec/finders/mongoid_spec.rb
 - spec/finders/sequel_spec.rb
 - spec/finders/sequel_test_connector.rb
 - spec/fixtures/admin.rb
@@ -98,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
       version: '0'
 requirements: []
 rubyforge_project: 
-rubygems_version: 2.2.2
+rubygems_version: 2.5.1
 signing_key: 
 specification_version: 4
 summary: Pagination plugin for web frameworks and other apps
diff --git a/spec/finders/active_record_spec.rb b/spec/finders/active_record_spec.rb
index 5631841..c6d74c5 100644
--- a/spec/finders/active_record_spec.rb
+++ b/spec/finders/active_record_spec.rb
@@ -104,13 +104,6 @@ describe WillPaginate::ActiveRecord do
       rel.last(2).should == users(:dev_7, :dev_8)
       rel.page(3).last.should == users(:poor_jamis)
     end
-
-    it "keeps pagination data after 'scoped'" do
-      rel = Developer.page(2).scoped
-      rel.per_page.should == 10
-      rel.offset.should == 10
-      rel.current_page.should == 2
-    end
   end
 
   describe "counting" do
@@ -136,15 +129,6 @@ describe WillPaginate::ActiveRecord do
       }.should run_queries(2)
     end
 
-    it "remembers custom count options in sub-relations" do
-      topics = Topic.paginate :page => 1, :per_page => 3, :count => {:conditions => "title LIKE '%futurama%'"}
-      topics.total_entries.should == 1
-      topics.length.should == 3
-      lambda {
-        topics.order('id').total_entries.should == 1
-      }.should run_queries(1)
-    end
-
     it "supports empty? method" do
       topics = Topic.paginate :page => 1, :per_page => 3
       lambda {
@@ -250,10 +234,11 @@ describe WillPaginate::ActiveRecord do
     end
 
     it "should strip the order when counting" do
+      expected = topics(:ar)
       lambda {
         sql = "select id, title, content from topics order by topics.title"
         topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
-        topics.first.should == topics(:ar)
+        topics.first.should == expected
       }.should run_queries(2)
 
       $query_sql.last.should include('COUNT')
@@ -295,78 +280,7 @@ describe WillPaginate::ActiveRecord do
     }.should run_queries(2)
   end
   
-  it "should paginate with :order" do
-    result = Topic.paginate :page => 1, :order => 'created_at DESC'
-    result.should == topics(:futurama, :harvey_birdman, :rails, :ar).reverse
-    result.total_pages.should == 1
-  end
-  
-  it "should paginate with :conditions" do
-    result = Topic.paginate :page => 1, :order => 'id ASC',
-      :conditions => ["created_at > ?", 30.minutes.ago]
-    result.should == topics(:rails, :ar)
-    result.total_pages.should == 1
-  end
-
-  it "should paginate with :include and :conditions" do
-    klass = Topic
-    klass = klass.references(:replies) if klass.respond_to?(:references)
-
-    result = klass.paginate \
-      :page     => 1, 
-      :include  => :replies,  
-      :conditions => "replies.content LIKE 'Bird%' ", 
-      :per_page => 10
-
-    expected = klass.find :all,
-      :include => 'replies', 
-      :conditions => "replies.content LIKE 'Bird%' ", 
-      :limit   => 10
-
-    result.should == expected
-    result.total_entries.should == 1
-  end
-
-  it "should paginate with :include and :order" do
-    result = nil
-    lambda {
-      result = Topic.paginate(:page => 1, :include => :replies, :per_page => 10,
-        :order => 'replies.created_at asc, topics.created_at asc').to_a
-    }.should run_queries(2)
-
-    expected = Topic.find :all, 
-      :include => 'replies', 
-      :order   => 'replies.created_at asc, topics.created_at asc', 
-      :limit   => 10
-
-    result.should == expected
-    result.total_entries.should == 4
-  end
-  
   describe "associations" do
-    it "should paginate with include" do
-      project = projects(:active_record)
-
-      topics = project.topics
-      topics = topics.references(:replies) if topics.respond_to?(:references)
-
-      result = topics.paginate \
-        :page       => 1, 
-        :include    => :replies,  
-        :conditions => ["replies.content LIKE ?", 'Nice%'],
-        :per_page   => 10
-
-      topics = Topic
-      topics = topics.references(:replies) if topics.respond_to?(:references)
-
-      expected = topics.find :all,
-        :include    => 'replies', 
-        :conditions => ["project_id = ? AND replies.content LIKE ?", project.id, 'Nice%'],
-        :limit      => 10
-
-      result.should == expected
-    end
-
     it "should paginate" do
       dhh = users(:david)
       expected_name_ordered = projects(:action_controller, :active_record)
@@ -375,7 +289,7 @@ describe WillPaginate::ActiveRecord do
       lambda {
         # with association-specified order
         result = ignore_deprecation {
-          dhh.projects.includes(:topics).paginate(:page => 1, :order => 'projects.name')
+          dhh.projects.includes(:topics).order('projects.name').paginate(:page => 1)
         }
         result.to_a.should == expected_name_ordered
         result.total_entries.should == 2
@@ -387,7 +301,7 @@ describe WillPaginate::ActiveRecord do
       result.total_entries.should == 2
 
       lambda {
-        dhh.projects.find(:all, :order => 'projects.id', :limit => 4)
+        dhh.projects.order('projects.id').limit(4).to_a
       }.should_not raise_error
       
       result = dhh.projects.paginate(:page => 1, :per_page => 4).reorder('projects.id')
@@ -417,7 +331,7 @@ describe WillPaginate::ActiveRecord do
     join_sql = 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id'
 
     lambda {
-      result = Developer.paginate(:page => 1, :joins => join_sql, :conditions => 'project_id = 1')
+      result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
       result.to_a # trigger loading of records
       result.size.should == 2
       developer_names = result.map(&:name)
@@ -427,8 +341,7 @@ describe WillPaginate::ActiveRecord do
 
     lambda {
       expected = result.to_a
-      result = Developer.paginate(:page => 1, :joins => join_sql,
-        :conditions => 'project_id = 1', :count => { :select => "users.id" }).to_a
+      result = Developer.where('developers_projects.project_id = 1').joins(join_sql).paginate(:page => 1)
       result.should == expected
       result.total_entries.should == 2
     }.should run_queries(1)
@@ -437,8 +350,8 @@ describe WillPaginate::ActiveRecord do
   it "should paginate with group" do
     result = nil
     lambda {
-      result = Developer.paginate(:page => 1, :per_page => 10,
-        :group => 'salary', :select => 'salary', :order => 'salary').to_a
+      result = Developer.select('salary').order('salary').group('salary').
+        paginate(:page => 1, :per_page => 10).to_a
     }.should run_queries(1)
 
     expected = users(:david, :jamis, :dev_10, :poor_jamis).map(&:salary).sort
@@ -451,12 +364,6 @@ describe WillPaginate::ActiveRecord do
     }.should raise_error(NoMethodError)
   end
 
-  it "should paginate with_scope" do
-    result = Developer.with_poor_ones { Developer.paginate :page => 1 }
-    result.size.should == 2
-    result.total_entries.should == 2
-  end
-
   describe "scopes" do
     it "should paginate" do
       result = Developer.poor.paginate :page => 1, :per_page => 1
@@ -496,12 +403,6 @@ describe WillPaginate::ActiveRecord do
     end
   end
 
-  it "should paginate with :readonly option" do
-    lambda {
-      Developer.paginate :readonly => true, :page => 1
-    }.should_not raise_error
-  end
-  
   it "should not paginate an array of IDs" do
     lambda {
       Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
@@ -514,64 +415,4 @@ describe WillPaginate::ActiveRecord do
       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
diff --git a/spec/finders/activerecord_test_connector.rb b/spec/finders/activerecord_test_connector.rb
index 867575f..eeedc11 100644
--- a/spec/finders/activerecord_test_connector.rb
+++ b/spec/finders/activerecord_test_connector.rb
@@ -4,12 +4,23 @@ require 'active_support/multibyte' # needed for Ruby 1.9.1
 require 'stringio'
 require 'erb'
 
+# https://travis-ci.org/mislav/will_paginate/jobs/99999001
+require 'active_support/core_ext/string/conversions'
+class String
+  alias to_datetime_without_patch to_datetime
+  def to_datetime
+    to_datetime_without_patch
+  rescue ArgumentError
+    return nil
+  end
+end
+
 $query_count = 0
 $query_sql = []
 
 ignore_sql = /
     ^(
-      PRAGMA | SHOW\ max_identifier_length |
+      PRAGMA | SHOW\ (max_identifier_length|search_path) |
       SELECT\ (currval|CAST|@@IDENTITY|@@ROWCOUNT) |
       SHOW\ ((FULL\ )?FIELDS|TABLES)
     )\b |
diff --git a/spec/finders/data_mapper_spec.rb b/spec/finders/data_mapper_spec.rb
index c04074f..f3aad6c 100644
--- a/spec/finders/data_mapper_spec.rb
+++ b/spec/finders/data_mapper_spec.rb
@@ -80,6 +80,19 @@ describe WillPaginate::DataMapper do
     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| }
diff --git a/spec/finders/mongoid_spec.rb b/spec/finders/mongoid_spec.rb
new file mode 100644
index 0000000..5562052
--- /dev/null
+++ b/spec/finders/mongoid_spec.rb
@@ -0,0 +1,140 @@
+require 'spec_helper'
+
+begin
+  require 'will_paginate/mongoid'
+rescue LoadError => error
+  warn "Error running Mongoid specs: #{error.message}"
+  mongoid_loaded = false
+else
+  Mongoid.connect_to 'will_paginate_test'
+
+  class MongoidModel
+    include Mongoid::Document
+  end
+
+  mongoid_loaded = true
+end
+
+describe WillPaginate::Mongoid do
+  before(:all) do
+    MongoidModel.delete_all
+    4.times { MongoidModel.create! }
+  end
+
+  let(:criteria) { MongoidModel.criteria }
+
+  describe "#page" do
+    it "should forward to the paginate method" do
+      criteria.expects(:paginate).with(:page => 2).returns("itself")
+      criteria.page(2).should == "itself"
+    end
+
+    it "should not override per_page if set earlier in the chain" do
+      criteria.paginate(:per_page => 10).page(1).per_page.should == 10
+      criteria.paginate(:per_page => 20).page(1).per_page.should == 20
+    end
+  end
+
+  describe "#per_page" do
+    it "should set the limit if given an argument" do
+      criteria.per_page(10).options[:limit].should == 10
+    end
+
+    it "should return the current limit if no argument is given" do
+      criteria.per_page.should == nil
+      criteria.per_page(10).per_page.should == 10
+    end
+
+    it "should be interchangable with limit" do
+      criteria.limit(15).per_page.should == 15
+    end
+
+    it "should be nil'able" do
+      criteria.per_page(nil).per_page.should be_nil
+    end
+  end
+
+  describe "#paginate" do
+    it "should use criteria" do
+      criteria.paginate.should be_instance_of(::Mongoid::Criteria)
+    end
+
+    it "should not override page number if set earlier in the chain" do
+      criteria.page(3).paginate.current_page.should == 3
+    end
+
+    it "should limit according to per_page parameter" do
+      criteria.paginate(:per_page => 10).options.should include(:limit => 10)
+    end
+
+    it "should skip according to page and per_page parameters" do
+      criteria.paginate(:page => 2, :per_page => 5).options.should include(:skip => 5)
+    end
+
+    specify "first fallback value for per_page option is the current limit" do
+      criteria.limit(12).paginate.options.should include(:limit => 12)
+    end
+
+    specify "second fallback value for per_page option is WillPaginate.per_page" do
+      criteria.paginate.options.should include(:limit => WillPaginate.per_page)
+    end
+
+    specify "page should default to 1" do
+      criteria.paginate.options.should include(:skip => 0)
+    end
+
+    it "should convert strings to integers" do
+      criteria.paginate(:page => "2", :per_page => "3").options.should include(:limit => 3)
+    end
+
+    describe "collection compatibility" do
+      describe "#total_count" do
+        it "should be calculated correctly" do
+          criteria.paginate(:per_page => 1).total_entries.should == 4
+          criteria.paginate(:per_page => 3).total_entries.should == 4
+        end
+
+        it "should be cached" do
+          criteria.expects(:count).once.returns(123)
+          criteria.paginate
+          2.times { criteria.total_entries.should == 123 }
+        end
+      end
+
+      it "should calculate total_pages" do
+        criteria.paginate(:per_page => 1).total_pages.should == 4
+        criteria.paginate(:per_page => 3).total_pages.should == 2
+        criteria.paginate(:per_page => 10).total_pages.should == 1
+      end
+
+      it "should return per_page" do
+        criteria.paginate(:per_page => 1).per_page.should == 1
+        criteria.paginate(:per_page => 5).per_page.should == 5
+      end
+
+      describe "#current_page" do
+        it "should return current_page" do
+          criteria.paginate(:page => 1).current_page.should == 1
+          criteria.paginate(:page => 3).current_page.should == 3
+        end
+
+        it "should be casted to PageNumber" do
+          page = criteria.paginate(:page => 1).current_page
+          (page.instance_of? WillPaginate::PageNumber).should be
+        end
+      end
+
+      it "should return offset" do
+        criteria.paginate(:page => 1).offset.should == 0
+        criteria.paginate(:page => 2, :per_page => 5).offset.should == 5
+        criteria.paginate(:page => 3, :per_page => 10).offset.should == 20
+      end
+
+      it "should not pollute plain mongoid criterias" do
+        %w(total_entries total_pages current_page).each do |method|
+          criteria.should_not respond_to(method)
+        end
+      end
+    end
+  end
+end if mongoid_loaded
diff --git a/spec/fixtures/admin.rb b/spec/fixtures/admin.rb
index 1d5e7f3..07311c0 100644
--- a/spec/fixtures/admin.rb
+++ b/spec/fixtures/admin.rb
@@ -1,3 +1,3 @@
 class Admin < User
-  has_many :companies, :finder_sql => 'SELECT * FROM companies'
+  has_many :companies
 end
diff --git a/spec/fixtures/developer.rb b/spec/fixtures/developer.rb
index e495447..d71601f 100644
--- a/spec/fixtures/developer.rb
+++ b/spec/fixtures/developer.rb
@@ -1,12 +1,5 @@
 class Developer < User
-  has_and_belongs_to_many :projects, :order => 'projects.name', :join_table => 'developers_projects'
-
-  def self.with_poor_ones(&block)
-    options = { :conditions => ['salary <= ?', 80000], :order => 'salary' }
-    with_scope({ :find => options }, :overwrite) do
-      yield
-    end
-  end
+  has_and_belongs_to_many :projects, :join_table => 'developers_projects'
 
   scope :poor, lambda {
     where(['salary <= ?', 80000]).order('salary')
diff --git a/spec/fixtures/project.rb b/spec/fixtures/project.rb
index 8c65a52..e51561f 100644
--- a/spec/fixtures/project.rb
+++ b/spec/fixtures/project.rb
@@ -1,5 +1,5 @@
 class Project < ActiveRecord::Base
-  has_and_belongs_to_many :developers, :uniq => true, :join_table => 'developers_projects'
+  has_and_belongs_to_many :developers, :join_table => 'developers_projects'
   
   has_many :topics
     # :finder_sql  => 'SELECT * FROM topics WHERE (topics.project_id = #{id})',
diff --git a/spec/page_number_spec.rb b/spec/page_number_spec.rb
index 1287116..5661e3e 100644
--- a/spec/page_number_spec.rb
+++ b/spec/page_number_spec.rb
@@ -3,23 +3,41 @@ require 'will_paginate/page_number'
 
 describe WillPaginate::PageNumber do
   describe "valid" do
-    subject { described_class.new('12', 'page') }
+    def num
+      WillPaginate::PageNumber.new('12', 'page')
+    end
+
+    it "== 12" do
+      num.should eq(12)
+    end
 
-    it { should eq(12) }
-    its(:inspect) { should eq('page 12') }
-    it { should be_a(WillPaginate::PageNumber) }
-    it { should be_instance_of(WillPaginate::PageNumber) }
-    it { should be_a(Numeric) }
-    it { should be_a(Fixnum) }
-    it { should_not be_instance_of(Fixnum) }
+    it "inspects to 'page 12'" do
+      num.inspect.should eq('page 12')
+    end
+
+    it "is a PageNumber" do
+      (num.instance_of? WillPaginate::PageNumber).should be
+    end
+
+    it "is a kind of Numeric" do
+      (num.is_a? Numeric).should be
+    end
+
+    it "is a kind of Fixnum" do
+      (num.is_a? Fixnum).should be
+    end
+
+    it "isn't directly a Fixnum" do
+      (num.instance_of? Fixnum).should_not be
+    end
 
     it "passes the PageNumber=== type check" do |variable|
-      (WillPaginate::PageNumber === subject).should be
+      (WillPaginate::PageNumber === num).should be
     end
 
     it "passes the Numeric=== type check" do |variable|
-      (Numeric === subject).should be
-      (Fixnum === subject).should be
+      (Numeric === num).should be
+      (Fixnum === num).should be
     end
   end
 
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index af8a66a..13b1418 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -19,8 +19,28 @@ RSpec.configure do |config|
     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 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
   config.backtrace_clean_patterns << /view_example_group/
 end
diff --git a/spec/view_helpers/action_view_spec.rb b/spec/view_helpers/action_view_spec.rb
index 2f63119..a22e131 100644
--- a/spec/view_helpers/action_view_spec.rb
+++ b/spec/view_helpers/action_view_spec.rb
@@ -1,3 +1,4 @@
+# encoding: utf-8
 require 'spec_helper'
 require 'active_support/rescuable' # needed for Ruby 1.9.1
 require 'action_controller'
@@ -20,6 +21,7 @@ describe WillPaginate::ActionView do
 
   before(:all) do
     I18n.load_path.concat WillPaginate::I18n.load_path
+    I18n.enforce_available_locales = false
   end
 
   before(:each) do
@@ -48,12 +50,14 @@ describe WillPaginate::ActionView do
     paginate do |pagination|
       assert_select 'a[href]', 3 do |elements|
         validate_page_numbers [2,3,2], elements
-        assert_select elements.last, ':last-child', "Next →"
+        text(elements[2]).should == 'Next →'
+      end
+      assert_select 'span', 1 do |spans|
+        spans[0]['class'].should == 'previous_page disabled'
+        text(spans[0]).should == '← Previous'
       end
-      assert_select 'span', 1
-      assert_select 'span.disabled:first-child', '← Previous'
       assert_select 'em.current', '1'
-      pagination.first.inner_text.should == '← Previous 1 2 3 Next →'
+      text(pagination[0]).should == '← Previous 1 2 3 Next →'
     end
   end
 
@@ -75,15 +79,12 @@ describe WillPaginate::ActionView do
       assert_select 'a[href]', 4 do |elements|
         validate_page_numbers [1,1,3,3], elements
         # test rel attribute values:
-        assert_select elements[1], 'a', '1' do |link|
-          link.first['rel'].should == 'prev start'
-        end
-        assert_select elements.first, 'a', "Prev" do |link|
-          link.first['rel'].should == 'prev start'
-        end
-        assert_select elements.last, 'a', "Next" do |link|
-          link.first['rel'].should == 'next'
-        end
+        text(elements[0]).should == 'Prev'
+        elements[0]['rel'].should == 'prev start'
+        text(elements[1]).should == '1'
+        elements[1]['rel'].should == 'prev start'
+        text(elements[3]).should == 'Next'
+        elements[3]['rel'].should == 'next'
       end
       assert_select '.current', '2'
     end
@@ -126,8 +127,8 @@ describe WillPaginate::ActionView do
       <a href="/foo/bar?page=2" class="next_page" rel="next">Next →</a></div>
     HTML
     expected.strip!.gsub!(/\s{2,}/, ' ')
-    expected_dom = HTML::Document.new(expected).root
-    
+    expected_dom = parse_html_document(expected).root
+
     html_document.root.should == expected_dom
   end
   
@@ -137,7 +138,8 @@ describe WillPaginate::ActionView do
     
     assert_select 'a[href]', 1 do |links|
       query = links.first['href'].split('?', 2)[1]
-      query.split('&').sort.should == %w(page=2 tag=%3Cbr%3E)
+      parts = query.gsub('&', '&').split('&').sort
+      parts.should == %w(page=2 tag=%3Cbr%3E)
     end
   end
   
@@ -355,6 +357,11 @@ describe WillPaginate::ActionView do
     I18n.available_locales # triggers loading existing translations
     I18n.backend.store_translations(:en, data)
   end
+
+  # Normalizes differences between HTML::Document and Nokogiri::HTML
+  def text(node)
+    node.inner_text.gsub('→', '→').gsub('←', '←')
+  end
 end
 
 class AdditionalLinkAttributesRenderer < WillPaginate::ActionView::LinkRenderer
@@ -376,7 +383,7 @@ class DummyController
   include Routes.url_helpers
   
   def initialize
-    @request = DummyRequest.new
+    @request = DummyRequest.new(self)
   end
 
   def params
@@ -399,12 +406,17 @@ class DummyRequest
   attr_accessor :symbolized_path_parameters
   alias :path_parameters :symbolized_path_parameters
   
-  def initialize
+  def initialize(controller)
+    @controller = controller
     @get = true
     @params = {}
     @symbolized_path_parameters = { :controller => 'foo', :action => 'bar' }
   end
-  
+
+  def routes
+    @controller._routes
+  end
+
   def get?
     @get
   end
diff --git a/spec/view_helpers/base_spec.rb b/spec/view_helpers/base_spec.rb
index 5fd97f1..1d6e86b 100644
--- a/spec/view_helpers/base_spec.rb
+++ b/spec/view_helpers/base_spec.rb
@@ -10,6 +10,7 @@ describe WillPaginate::ViewHelpers do
   before(:all) do
     # make sure default translations aren't loaded
     I18n.load_path.clear
+    I18n.enforce_available_locales = false
   end
 
   before(:each) do
diff --git a/spec/view_helpers/view_example_group.rb b/spec/view_helpers/view_example_group.rb
index 27994c0..b482266 100644
--- a/spec/view_helpers/view_example_group.rb
+++ b/spec/view_helpers/view_example_group.rb
@@ -9,16 +9,24 @@ rescue LoadError
 ensure
   $stderr = STDERR
 end
-require 'action_dispatch/testing/assertions'
+
+begin
+  require 'rails/dom/testing/assertions'
+rescue LoadError
+  require 'action_dispatch/testing/assertions'
+end
 require 'will_paginate/array'
 
 module ViewExampleGroup
   
-  include ActionDispatch::Assertions::SelectorAssertions
+  if defined?(Rails::Dom::Testing::Assertions)
+    include Rails::Dom::Testing::Assertions::SelectorAssertions
+  else
+    include ActionDispatch::Assertions::SelectorAssertions
+  end
   include MiniTest::Assertions if defined? MiniTest
 
   def assert(value, message)
-    message = message.call if message.respond_to?(:call)
     raise message unless value
   end
   
@@ -40,11 +48,23 @@ module ViewExampleGroup
     
     @render_output
   end
-  
+
+  def parse_html_document(html)
+    @html_document ||= if defined?(Rails::Dom::Testing::Assertions)
+      Nokogiri::HTML::Document.parse(html)
+    else
+      HTML::Document.new(html, true, false)
+    end
+  end
+
   def html_document
-    @html_document ||= HTML::Document.new(@render_output, true, false)
+    @html_document ||= parse_html_document(@render_output)
   end
-  
+
+  def document_root_element
+    html_document.root
+  end
+
   def response_from_page_or_rjs
     html_document.root
   end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-will-paginate.git



More information about the Pkg-ruby-extras-commits mailing list