Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/kamal/will_paginate
Browse files Browse the repository at this point in the history
Conflicts:

	will_paginate.gemspec
  • Loading branch information
stilkov committed Apr 7, 2009
2 parents 234bf47 + 553e7b5 commit 9c2c65b
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 44 deletions.
6 changes: 1 addition & 5 deletions lib/will_paginate.rb
Expand Up @@ -68,11 +68,7 @@ module Deprecation # :nodoc:

def self.warn(message, callstack = caller)
message = 'WillPaginate: ' + message.strip.gsub(/\s+/, ' ')
behavior.call(message, callstack) if behavior && !silenced?
end

def self.silenced?
ActiveSupport::Deprecation.silenced?
ActiveSupport::Deprecation.warn(message, callstack)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/will_paginate/collection.rb
Expand Up @@ -82,7 +82,7 @@ def initialize(page, per_page, total = nil)
#
# The Array#paginate API has since then changed, but this still serves as a
# fine example of WillPaginate::Collection usage.
def self.create(page, per_page, total = nil, &block)
def self.create(page, per_page, total = nil)
pager = new(page, per_page, total)
yield pager
pager
Expand Down
18 changes: 11 additions & 7 deletions lib/will_paginate/finder.rb
Expand Up @@ -61,7 +61,7 @@ module ClassMethods
#
# All other options (+conditions+, +order+, ...) are forwarded to +find+
# and +count+ calls.
def paginate(*args, &block)
def paginate(*args)
options = args.pop
page, per_page, total_entries = wp_parse_options(options)
finder = (options[:finder] || 'find').to_s
Expand All @@ -79,7 +79,7 @@ def paginate(*args, &block)

args << find_options
# @options_from_last_find = nil
pager.replace send(finder, *args, &block)
pager.replace(send(finder, *args) { |*a| yield(*a) if block_given? })

# magic counting for user convenience:
pager.total_entries = wp_count(count_options, args, finder) unless pager.total_entries
Expand All @@ -96,7 +96,7 @@ def paginate(*args, &block)
#
# See {Faking Cursors in ActiveRecord}[http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord]
# where Jamis Buck describes this and a more efficient way for MySQL.
def paginated_each(options = {}, &block)
def paginated_each(options = {})
options = { :order => 'id', :page => 1 }.merge options
options[:page] = options[:page].to_i
options[:total_entries] = 0 # skip the individual count queries
Expand All @@ -106,7 +106,7 @@ def paginated_each(options = {}, &block)
collection = paginate(options)
with_exclusive_scope(:find => {}) do
# using exclusive scope so that the block is yielded in scope-free context
total += collection.each(&block).size
total += collection.each { |item| yield item }.size
end
options[:page] += 1
end until collection.size < collection.per_page
Expand Down Expand Up @@ -161,10 +161,14 @@ def respond_to?(method, include_priv = false) #:nodoc:

protected

def method_missing_with_paginate(method, *args, &block) #:nodoc:
def method_missing_with_paginate(method, *args) #:nodoc:
# did somebody tried to paginate? if not, let them be
unless method.to_s.index('paginate') == 0
return method_missing_without_paginate(method, *args, &block)
if block_given?
return method_missing_without_paginate(method, *args) { |*a| yield(*a) }
else
return method_missing_without_paginate(method, *args)
end
end

# paginate finders are really just find_* with limit and offset
Expand All @@ -177,7 +181,7 @@ def method_missing_with_paginate(method, *args, &block) #:nodoc:
options[:finder] = finder
args << options

paginate(*args, &block)
paginate(*args) { |*a| yield(*a) if block_given? }
end

# Does the not-so-trivial job of finding out the total number of entries
Expand Down
12 changes: 6 additions & 6 deletions lib/will_paginate/named_scope.rb
Expand Up @@ -83,15 +83,15 @@ def scopes
#
# expected_options = { :conditions => { :colored => 'red' } }
# assert_equal expected_options, Shirt.colored('red').proxy_options
def named_scope(name, options = {}, &block)
def named_scope(name, options = {})
name = name.to_sym
scopes[name] = lambda do |parent_scope, *args|
Scope.new(parent_scope, case options
when Hash
options
when Proc
options.call(*args)
end, &block)
end) { |*a| yield(*a) if block_given? }
end
(class << self; self end).instance_eval do
define_method name do |*args|
Expand All @@ -112,9 +112,9 @@ class Scope

delegate :scopes, :with_scope, :to => :proxy_scope

def initialize(proxy_scope, options, &block)
def initialize(proxy_scope, options)
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
extend Module.new(&block) if block_given?
extend Module.new { |*args| yield(*args) } if block_given?
@proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
end

Expand Down Expand Up @@ -152,12 +152,12 @@ def proxy_found
end

private
def method_missing(method, *args, &block)
def method_missing(method, *args)
if scopes.include?(method)
scopes[method].call(self, *args)
else
with_scope :find => proxy_options do
proxy_scope.send(method, *args, &block)
proxy_scope.send(method, *args) { |*a| yield(*a) if block_given? }
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/will_paginate/named_scope_patch.rb
@@ -1,7 +1,7 @@
ActiveRecord::Associations::AssociationProxy.class_eval do
protected
def with_scope(*args, &block)
@reflection.klass.send :with_scope, *args, &block
def with_scope(*args)
@reflection.klass.send(:with_scope, *args) { |*a| yield(*a) if block_given? }
end
end

Expand All @@ -10,11 +10,11 @@ def with_scope(*args, &block)
klass.class_eval do
protected
alias :method_missing_without_scopes :method_missing_without_paginate
def method_missing_without_paginate(method, *args, &block)
def method_missing_without_paginate(method, *args)
if @reflection.klass.scopes.include?(method)
@reflection.klass.scopes[method].call(self, *args, &block)
@reflection.klass.scopes[method].call(self, *args) { |*a| yield(*a) if block_given? }
else
method_missing_without_scopes(method, *args, &block)
method_missing_without_scopes(method, *args) { |*a| yield(*a) if block_given? }
end
end
end
Expand All @@ -23,14 +23,14 @@ def method_missing_without_paginate(method, *args, &block)
# Rails 1.2.6
ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
protected
def method_missing(method, *args, &block)
def method_missing(method, *args)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
super
elsif @reflection.klass.scopes.include?(method)
@reflection.klass.scopes[method].call(self, *args)
else
@reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
@reflection.klass.send(method, *args, &block)
@reflection.klass.send(method, *args) { |*a| yield(*a) if block_given? }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/will_paginate/version.rb
Expand Up @@ -2,7 +2,7 @@ module WillPaginate
module VERSION
MAJOR = 2
MINOR = 3
TINY = 5
TINY = 8

STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down
24 changes: 18 additions & 6 deletions lib/will_paginate/view_helpers.rb
Expand Up @@ -100,7 +100,7 @@ def will_paginate(collection = nil, options = {})

options = options.symbolize_keys.reverse_merge WillPaginate::ViewHelpers.pagination_options
if options[:prev_label]
WillPaginate::Deprecation::warn(":prev_label view parameter is now :previous_label; the old name has been deprecated.")
WillPaginate::Deprecation::warn(":prev_label view parameter is now :previous_label; the old name has been deprecated", caller)
options[:previous_label] = options.delete(:prev_label)
end

Expand Down Expand Up @@ -189,12 +189,11 @@ def page_entries_info(collection, options = {})

def self.total_pages_for_collection(collection) #:nodoc:
if collection.respond_to?('page_count') and !collection.respond_to?('total_pages')
WillPaginate::Deprecation.warn <<-MSG
WillPaginate::Deprecation.warn %{
You are using a paginated collection of class #{collection.class.name}
which conforms to the old API of WillPaginate::Collection by using
`page_count`, while the current method name is `total_pages`. Please
upgrade yours or 3rd-party code that provides the paginated collection.
MSG
upgrade yours or 3rd-party code that provides the paginated collection}, caller
class << collection
def total_pages; page_count; end
end
Expand Down Expand Up @@ -330,8 +329,7 @@ def url_for(page)
stringified_merge @url_params, @options[:params] if @options[:params]

if complex = param_name.index(/[^\w-]/)
page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest).
parse_query_parameters("#{param_name}=#{page}")
page_param = parse_query_parameters("#{param_name}=#{page}")

stringified_merge @url_params, page_param
else
Expand Down Expand Up @@ -394,6 +392,20 @@ def stringified_merge(target, other)
end
end
end

def parse_query_parameters(params)
if defined?(CGIMethods)
CGIMethods.parse_query_parameters(params)
elsif defined?(ActionController::AbstractRequest)
ActionController::AbstractRequest.parse_query_parameters(params)
elsif defined?(ActionController::UrlEncodedPairParser)
# For Rails > 2.2
ActionController::UrlEncodedPairParser.parse_query_parameters(params)
else
# For Rails > 2.3
Rack::Utils.parse_nested_query(params)
end
end
end

class XmlLinkRenderer < LinkRenderer
Expand Down
5 changes: 4 additions & 1 deletion test/helper.rb
Expand Up @@ -29,7 +29,10 @@ def collect_deprecations

# Wrap tests that use Mocha and skip if unavailable.
def uses_mocha(test_name)
require 'mocha' unless Object.const_defined?(:Mocha)
unless Object.const_defined?(:Mocha)
gem 'mocha', '>= 0.9.5'
require 'mocha'
end
rescue LoadError => load_error
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
else
Expand Down
7 changes: 7 additions & 0 deletions test/lib/activerecord_test_case.rb
@@ -1,6 +1,13 @@
require 'lib/activerecord_test_connector'

class ActiveRecordTestCase < Test::Unit::TestCase
if defined?(ActiveSupport::Testing::SetupAndTeardown)
include ActiveSupport::Testing::SetupAndTeardown
end

if defined?(ActiveRecord::TestFixtures)
include ActiveRecord::TestFixtures
end
# Set our fixture path
if ActiveRecordTestConnector.able_to_connect
self.fixture_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
Expand Down
7 changes: 7 additions & 0 deletions test/lib/view_test_process.rb
Expand Up @@ -17,6 +17,13 @@
ActionController::Base.perform_caching = false

class WillPaginate::ViewTestCase < Test::Unit::TestCase
if defined?(ActionController::TestCase::Assertions)
include ActionController::TestCase::Assertions
end
if defined?(ActiveSupport::Testing::Deprecation)
include ActiveSupport::Testing::Deprecation
end

def setup
super
@controller = DummyController.new
Expand Down
4 changes: 2 additions & 2 deletions test/tasks.rake
Expand Up @@ -32,10 +32,10 @@ task :test_databases => %w(test_mysql test_sqlite3 test_postgres)
desc %{Test everything on SQLite3, MySQL and PostgreSQL}
task :test_full => %w(test test_mysql test_postgres)

desc %{Test everything with Rails 2.1.x, 2.0.x & 1.2.x gems}
desc %{Test everything with Rails 2.3.x, 2.2.x, 2.1.x, 2.0.x & 1.2.x gems}
task :test_all do
all = Rake::Task['test_full']
versions = %w(2.1.0 2.0.4 1.2.6)
versions = %w(2.3.1 2.2.2 2.1.0 2.0.4 1.2.6)
versions.each do |version|
ENV['RAILS_VERSION'] = "~> #{version}"
all.invoke
Expand Down
14 changes: 8 additions & 6 deletions test/view_test.rb
Expand Up @@ -250,13 +250,15 @@ def test_page_entries_info
@html_result
end

def test_page_entries_info_with_longer_class_name
@template = '<%= page_entries_info collection %>'
collection = ('a'..'z').to_a.paginate
collection.first.stubs(:class).returns(mock('class', :name => 'ProjectType'))
uses_mocha 'class name' do
def test_page_entries_info_with_longer_class_name
@template = '<%= page_entries_info collection %>'
collection = ('a'..'z').to_a.paginate
collection.first.stubs(:class).returns(mock('class', :name => 'ProjectType'))

paginate collection
assert @html_result.index('project types'), "expected <#{@html_result.inspect}> to mention 'project types'"
paginate collection
assert @html_result.index('project types'), "expected <#{@html_result.inspect}> to mention 'project types'"
end
end

def test_page_entries_info_with_single_page_collection
Expand Down
4 changes: 2 additions & 2 deletions will_paginate.gemspec
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = 'will_paginate'
s.version = '2.3.7'
s.date = '2008-10-26'
s.version = '2.3.8'
s.date = '2009-03-09'

s.summary = "Most awesome pagination solution for Rails"
s.description = "The will_paginate library provides a simple, yet powerful and extensible API for ActiveRecord pagination and rendering of pagination links in ActionView templates. [Experimental: addition of prev/next rel navigation for XML rendering]"
Expand Down

0 comments on commit 9c2c65b

Please sign in to comment.