Skip to content

Commit 4d260bc

Browse files
committedAug 5, 2011
mix in Active Record/Action View directly on require
Get rid of extra steps, they were a bad idea
1 parent 3a3e07c commit 4d260bc

File tree

6 files changed

+22
-35
lines changed

6 files changed

+22
-35
lines changed
 

‎init.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
# the initializer defined by the Railtie won't ever run when loaded as plugin.
55

66
if defined? ActiveRecord::Base
7-
WillPaginate::Railtie.setup_activerecord
7+
require 'will_paginate/active_record'
88
end
99

1010
if defined? ActionController::Base
1111
WillPaginate::Railtie.setup_actioncontroller
1212
end
1313

1414
if defined? ActionView::Base
15-
WillPaginate::Railtie.setup_actionview
15+
require 'will_paginate/view_helpers/action_view'
1616
end
1717

1818
WillPaginate::Railtie.add_locale_path config

‎lib/will_paginate/active_record.rb

+15-17
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,6 @@ module WillPaginate
1515
# @posts = Post.paginate :all, :page => params[:page], :order => 'created_at DESC'
1616
#
1717
module ActiveRecord
18-
# In Rails, this is automatically called to mix-in pagination functionality to ActiveRecord.
19-
def self.setup
20-
::ActiveRecord::Base.extend PerPage
21-
::ActiveRecord::Base.extend ActiveRecord::Pagination
22-
::ActiveRecord::Base.extend ActiveRecord::BaseMethods
23-
24-
klasses = [::ActiveRecord::Relation]
25-
if defined? ::ActiveRecord::Associations::CollectionProxy
26-
klasses << ::ActiveRecord::Associations::CollectionProxy
27-
else
28-
klasses << ::ActiveRecord::Associations::AssociationCollection
29-
end
30-
31-
# support pagination on associations and scopes
32-
klasses.each { |klass| klass.send(:include, ActiveRecord::Pagination) }
33-
end
34-
3518
# makes a Relation look like WillPaginate::Collection
3619
module RelationMethods
3720
attr_accessor :current_page
@@ -192,5 +175,20 @@ def paginate_by_sql(sql, options)
192175
end
193176
end
194177
end
178+
179+
# mix everything into Active Record
180+
::ActiveRecord::Base.extend PerPage
181+
::ActiveRecord::Base.extend Pagination
182+
::ActiveRecord::Base.extend BaseMethods
183+
184+
klasses = [::ActiveRecord::Relation]
185+
if defined? ::ActiveRecord::Associations::CollectionProxy
186+
klasses << ::ActiveRecord::Associations::CollectionProxy
187+
else
188+
klasses << ::ActiveRecord::Associations::AssociationCollection
189+
end
190+
191+
# support pagination on associations and scopes
192+
klasses.each { |klass| klass.send(:include, Pagination) }
195193
end
196194
end

‎lib/will_paginate/railtie.rb

+2-12
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,24 @@ module WillPaginate
55
class Railtie < Rails::Railtie
66
initializer "will_paginate" do |app|
77
ActiveSupport.on_load :active_record do
8-
WillPaginate::Railtie.setup_activerecord
8+
require 'will_paginate/active_record'
99
end
1010

1111
ActiveSupport.on_load :action_controller do
1212
WillPaginate::Railtie.setup_actioncontroller
1313
end
1414

1515
ActiveSupport.on_load :action_view do
16-
WillPaginate::Railtie.setup_actionview
16+
require 'will_paginate/view_helpers/action_view'
1717
end
1818

1919
self.class.add_locale_path config
2020
end
2121

22-
def self.setup_activerecord
23-
require 'will_paginate/active_record'
24-
WillPaginate::ActiveRecord.setup
25-
end
26-
2722
def self.setup_actioncontroller
2823
ActionDispatch::ShowExceptions.rescue_responses['WillPaginate::InvalidPage'] = :not_found
2924
end
3025

31-
def self.setup_actionview
32-
require 'will_paginate/view_helpers/action_view'
33-
::ActionView::Base.send :include, WillPaginate::ActionView
34-
end
35-
3626
def self.add_locale_path(config)
3727
locale_path = File.expand_path('../locale', __FILE__)
3828
config.i18n.railties_load_path.concat Dir["#{locale_path}/*.{rb,yml}"]

‎lib/will_paginate/view_helpers/action_view.rb

+2
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,7 @@ def parse_query_parameters(params)
135135
Rack::Utils.parse_nested_query(params)
136136
end
137137
end
138+
139+
::ActionView::Base.send :include, self
138140
end
139141
end

‎spec/console_fixtures.rb

-2
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,3 @@
2626
# load all fixtures
2727
ActiverecordTestConnector::Fixtures.create_fixtures \
2828
ActiverecordTestConnector::FIXTURES_PATH, ActiveRecord::Base.connection.tables
29-
30-
WillPaginate::ActiveRecord.setup

‎spec/finders/active_record_spec.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
require 'spec_helper'
22
require 'will_paginate/active_record'
33
require File.expand_path('../activerecord_test_connector', __FILE__)
4-
ActiverecordTestConnector.setup
54

6-
WillPaginate::ActiveRecord.setup
5+
ActiverecordTestConnector.setup
76
abort unless ActiverecordTestConnector.able_to_connect
87

98
describe WillPaginate::ActiveRecord do

0 commit comments

Comments
 (0)
Please sign in to comment.