Permalink
Browse files

move Active Record pagination outside of the "Finders" namespace

  • Loading branch information...
1 parent fb36491 commit dfd206bf21957d7c25a7fdde7ff2ecfa3c9e4804 @mislav committed Jul 27, 2011
@@ -1,7 +1,8 @@
-require 'will_paginate/finders/base'
+require 'will_paginate/per_page'
+require 'will_paginate/collection'
require 'active_record'
-module WillPaginate::Finders
+module WillPaginate
# = Paginating finders for ActiveRecord models
#
# WillPaginate adds +paginate+, +per_page+ and other methods to
@@ -15,8 +16,8 @@ module WillPaginate::Finders
#
module ActiveRecord
# In Rails, this is automatically called to mix-in pagination functionality to ActiveRecord.
- def self.enable!
- ::ActiveRecord::Base.extend Base
+ def self.setup
+ ::ActiveRecord::Base.extend PerPage
::ActiveRecord::Base.extend ActiveRecord::Pagination
::ActiveRecord::Base.extend ActiveRecord::BaseMethods
@@ -106,9 +107,14 @@ def to_a
module Pagination
def paginate(options)
- pagenum, per_page, total = wp_parse_options(options)
- count_options = options[:count]
- options = options.except(:page, :per_page, :total_entries, :count)
+ 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)
+
+ count_options = options.delete(:count)
+ options.delete(:page)
+
rel = limit(per_page).page(pagenum)
rel = rel.apply_finder_options(options) if options.any?
rel.wp_count_options = count_options if count_options
@@ -142,7 +148,11 @@ module BaseMethods
# application.
#
def paginate_by_sql(sql, options)
- WillPaginate::Collection.create(*wp_parse_options(options)) do |pager|
+ pagenum = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
+ per_page = options[:per_page] || self.per_page
+ total = options[:total_entries]
+
+ WillPaginate::Collection.create(pagenum, per_page, total) do |pager|
query = sanitize_sql(sql.dup)
original_query = query.dup
# add limit, offset
@@ -5,8 +5,8 @@ module WillPaginate
class Railtie < Rails::Railtie
initializer "will_paginate" do |app|
ActiveSupport.on_load :active_record do
- require 'will_paginate/finders/active_record'
- WillPaginate::Finders::ActiveRecord.enable!
+ require 'will_paginate/active_record'
+ WillPaginate::ActiveRecord.setup
end
ActiveSupport.on_load :action_controller do
@@ -1,11 +1,11 @@
require 'spec_helper'
-require 'will_paginate/finders/active_record'
+require 'will_paginate/active_record'
require File.expand_path('../activerecord_test_connector', __FILE__)
ActiverecordTestConnector.setup
-WillPaginate::Finders::ActiveRecord.enable!
+WillPaginate::ActiveRecord.setup
-describe WillPaginate::Finders::ActiveRecord do
+describe WillPaginate::ActiveRecord do
extend ActiverecordTestConnector::FixtureSetup

0 comments on commit dfd206b

Please sign in to comment.