diff --git a/init.rb b/init.rb index 195f392d9..dcaead04d 100644 --- a/init.rb +++ b/init.rb @@ -4,7 +4,7 @@ # the initializer defined by the Railtie won't ever run when loaded as plugin. if defined? ActiveRecord::Base - WillPaginate::Railtie.setup_activerecord + require 'will_paginate/active_record' end if defined? ActionController::Base @@ -12,7 +12,7 @@ end if defined? ActionView::Base - WillPaginate::Railtie.setup_actionview + require 'will_paginate/view_helpers/action_view' end WillPaginate::Railtie.add_locale_path config diff --git a/lib/will_paginate/active_record.rb b/lib/will_paginate/active_record.rb index faa269822..7d944adb1 100644 --- a/lib/will_paginate/active_record.rb +++ b/lib/will_paginate/active_record.rb @@ -15,23 +15,6 @@ module WillPaginate # @posts = Post.paginate :all, :page => params[:page], :order => 'created_at DESC' # module ActiveRecord - # In Rails, this is automatically called to mix-in pagination functionality to ActiveRecord. - def self.setup - ::ActiveRecord::Base.extend PerPage - ::ActiveRecord::Base.extend ActiveRecord::Pagination - ::ActiveRecord::Base.extend ActiveRecord::BaseMethods - - klasses = [::ActiveRecord::Relation] - if defined? ::ActiveRecord::Associations::CollectionProxy - klasses << ::ActiveRecord::Associations::CollectionProxy - else - klasses << ::ActiveRecord::Associations::AssociationCollection - end - - # support pagination on associations and scopes - klasses.each { |klass| klass.send(:include, ActiveRecord::Pagination) } - end - # makes a Relation look like WillPaginate::Collection module RelationMethods attr_accessor :current_page @@ -192,5 +175,20 @@ def paginate_by_sql(sql, options) end end end + + # mix everything into Active Record + ::ActiveRecord::Base.extend PerPage + ::ActiveRecord::Base.extend Pagination + ::ActiveRecord::Base.extend BaseMethods + + klasses = [::ActiveRecord::Relation] + if defined? ::ActiveRecord::Associations::CollectionProxy + klasses << ::ActiveRecord::Associations::CollectionProxy + else + klasses << ::ActiveRecord::Associations::AssociationCollection + end + + # support pagination on associations and scopes + klasses.each { |klass| klass.send(:include, Pagination) } end end diff --git a/lib/will_paginate/railtie.rb b/lib/will_paginate/railtie.rb index 3cb82c5f3..e89d1e399 100644 --- a/lib/will_paginate/railtie.rb +++ b/lib/will_paginate/railtie.rb @@ -5,7 +5,7 @@ module WillPaginate class Railtie < Rails::Railtie initializer "will_paginate" do |app| ActiveSupport.on_load :active_record do - WillPaginate::Railtie.setup_activerecord + require 'will_paginate/active_record' end ActiveSupport.on_load :action_controller do @@ -13,26 +13,16 @@ class Railtie < Rails::Railtie end ActiveSupport.on_load :action_view do - WillPaginate::Railtie.setup_actionview + require 'will_paginate/view_helpers/action_view' end self.class.add_locale_path config end - def self.setup_activerecord - require 'will_paginate/active_record' - WillPaginate::ActiveRecord.setup - end - def self.setup_actioncontroller ActionDispatch::ShowExceptions.rescue_responses['WillPaginate::InvalidPage'] = :not_found end - def self.setup_actionview - require 'will_paginate/view_helpers/action_view' - ::ActionView::Base.send :include, WillPaginate::ActionView - end - def self.add_locale_path(config) locale_path = File.expand_path('../locale', __FILE__) config.i18n.railties_load_path.concat Dir["#{locale_path}/*.{rb,yml}"] diff --git a/lib/will_paginate/view_helpers/action_view.rb b/lib/will_paginate/view_helpers/action_view.rb index cfe502d32..ff2fe7eae 100644 --- a/lib/will_paginate/view_helpers/action_view.rb +++ b/lib/will_paginate/view_helpers/action_view.rb @@ -135,5 +135,7 @@ def parse_query_parameters(params) Rack::Utils.parse_nested_query(params) end end + + ::ActionView::Base.send :include, self end end diff --git a/spec/console_fixtures.rb b/spec/console_fixtures.rb index 1cda955b2..40b41d119 100644 --- a/spec/console_fixtures.rb +++ b/spec/console_fixtures.rb @@ -26,5 +26,3 @@ # load all fixtures ActiverecordTestConnector::Fixtures.create_fixtures \ ActiverecordTestConnector::FIXTURES_PATH, ActiveRecord::Base.connection.tables - -WillPaginate::ActiveRecord.setup diff --git a/spec/finders/active_record_spec.rb b/spec/finders/active_record_spec.rb index 3ac1fc65e..4def4b110 100644 --- a/spec/finders/active_record_spec.rb +++ b/spec/finders/active_record_spec.rb @@ -1,9 +1,8 @@ require 'spec_helper' require 'will_paginate/active_record' require File.expand_path('../activerecord_test_connector', __FILE__) -ActiverecordTestConnector.setup -WillPaginate::ActiveRecord.setup +ActiverecordTestConnector.setup abort unless ActiverecordTestConnector.able_to_connect describe WillPaginate::ActiveRecord do