Skip to content

Commit

Permalink
mix in Active Record/Action View directly on require
Browse files Browse the repository at this point in the history
Get rid of extra steps, they were a bad idea
  • Loading branch information
mislav committed Aug 5, 2011
1 parent 3a3e07c commit 4d260bc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 35 deletions.
4 changes: 2 additions & 2 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# 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
WillPaginate::Railtie.setup_actioncontroller
end

if defined? ActionView::Base
WillPaginate::Railtie.setup_actionview
require 'will_paginate/view_helpers/action_view'
end

WillPaginate::Railtie.add_locale_path config
32 changes: 15 additions & 17 deletions lib/will_paginate/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
14 changes: 2 additions & 12 deletions lib/will_paginate/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,24 @@ 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
WillPaginate::Railtie.setup_actioncontroller
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}"]
Expand Down
2 changes: 2 additions & 0 deletions lib/will_paginate/view_helpers/action_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,7 @@ def parse_query_parameters(params)
Rack::Utils.parse_nested_query(params)
end
end

::ActionView::Base.send :include, self
end
end
2 changes: 0 additions & 2 deletions spec/console_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@
# load all fixtures
ActiverecordTestConnector::Fixtures.create_fixtures \
ActiverecordTestConnector::FIXTURES_PATH, ActiveRecord::Base.connection.tables

WillPaginate::ActiveRecord.setup
3 changes: 1 addition & 2 deletions spec/finders/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4d260bc

Please sign in to comment.