Skip to content

Commit

Permalink
move Active Record pagination outside of the "Finders" namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jul 28, 2011
1 parent fb36491 commit dfd206b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/will_paginate/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/finders/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit dfd206b

Please sign in to comment.