Skip to content
This repository
Browse code

Replace the placeholder base_hook API with on_load. To specify some c…

…ode that

should run during framework load do:

ActiveSupport.on_load(:action_controller) do
  # Code run in the context of AC::Base
end
  • Loading branch information...
commit 4aded43b73ff94dbf06b4a2d2075651ce454e1d5 1 parent 331327d
Yehuda Katz authored March 29, 2010
2  actionmailer/lib/action_mailer/base.rb
@@ -291,7 +291,7 @@ class Base < AbstractController::Base
291 291
       :parts_order  => [ "text/plain", "text/enriched", "text/html" ]
292 292
     }.freeze
293 293
 
294  
-    ActionMailer.run_base_hooks(self)
  294
+    ActiveSupport.run_load_hooks(:action_mailer, self)
295 295
 
296 296
     class << self
297 297
 
6  actionmailer/lib/action_mailer/railtie.rb
@@ -6,18 +6,18 @@ class Railtie < Rails::Railtie
6 6
     config.action_mailer = ActiveSupport::OrderedOptions.new
7 7
 
8 8
     initializer "action_mailer.url_for", :before => :load_environment_config do |app|
9  
-      ActionMailer.base_hook { include app.routes.url_helpers }
  9
+      ActiveSupport.on_load(:action_mailer) { include app.routes.url_helpers }
10 10
     end
11 11
 
12 12
     require "action_mailer/railties/log_subscriber"
13 13
     log_subscriber :action_mailer, ActionMailer::Railties::LogSubscriber.new
14 14
 
15 15
     initializer "action_mailer.logger" do
16  
-      ActionMailer.base_hook { self.logger ||= Rails.logger }
  16
+      ActiveSupport.on_load(:action_mailer) { self.logger ||= Rails.logger }
17 17
     end
18 18
 
19 19
     initializer "action_mailer.set_configs" do |app|
20  
-      ActionMailer.base_hook do
  20
+      ActiveSupport.on_load(:action_mailer) do
21 21
         app.config.action_mailer.each do |k,v|
22 22
           send "#{k}=", v
23 23
         end
3  actionpack/lib/action_controller/base.rb
@@ -64,8 +64,7 @@ def self.filter_parameter_logging(*args, &block)
64 64
       filter
65 65
     end
66 66
 
67  
-    ActionController.run_base_hooks(self)
68  
-
  67
+    ActiveSupport.run_load_hooks(:action_controller, self)
69 68
   end
70 69
 end
71 70
 
10  actionpack/lib/action_controller/railtie.rb
@@ -41,7 +41,7 @@ class Railtie < Rails::Railtie
41 41
     log_subscriber :action_controller, ActionController::Railties::LogSubscriber.new
42 42
 
43 43
     initializer "action_controller.logger" do
44  
-      ActionController.base_hook { self.logger ||= Rails.logger }
  44
+      ActiveSupport.on_load(:action_controller) { self.logger ||= Rails.logger }
45 45
     end
46 46
 
47 47
     initializer "action_controller.set_configs" do |app|
@@ -53,23 +53,23 @@ class Railtie < Rails::Railtie
53 53
       ac.stylesheets_dir = paths.public.stylesheets.to_a.first
54 54
       ac.secret          = app.config.cookie_secret
55 55
 
56  
-      ActionController.base_hook do
  56
+      ActiveSupport.on_load(:action_controller) do
57 57
         self.config.merge!(ac)
58 58
       end
59 59
     end
60 60
 
61 61
     initializer "action_controller.initialize_framework_caches" do
62  
-      ActionController.base_hook { self.cache_store ||= RAILS_CACHE }
  62
+      ActiveSupport.on_load(:action_controller) { self.cache_store ||= RAILS_CACHE }
63 63
     end
64 64
 
65 65
     initializer "action_controller.set_helpers_path" do |app|
66  
-      ActionController.base_hook do
  66
+      ActiveSupport.on_load(:action_controller) do
67 67
         self.helpers_path = app.config.paths.app.helpers.to_a
68 68
       end
69 69
     end
70 70
 
71 71
     initializer "action_controller.url_helpers" do |app|
72  
-      ActionController.base_hook do
  72
+      ActiveSupport.on_load(:action_controller) do
73 73
         extend ::ActionController::Railties::UrlHelpers.with(app.routes)
74 74
       end
75 75
 
2  actionpack/lib/action_view/base.rb
@@ -173,7 +173,7 @@ class << self
173 173
       delegate :logger, :to => 'ActionController::Base', :allow_nil => true
174 174
     end
175 175
 
176  
-    ActionView.run_base_hooks(self)
  176
+    ActiveSupport.run_load_hooks(:action_view, self)
177 177
 
178 178
     attr_accessor :base_path, :assigns, :template_extension, :lookup_context
179 179
     attr_internal :captures, :request, :controller, :template, :config
2  actionpack/lib/action_view/helpers/active_model_helper.rb
@@ -6,7 +6,7 @@
6 6
 require 'active_support/core_ext/object/blank'
7 7
 
8 8
 module ActionView
9  
-  ActionView.base_hook do
  9
+  ActiveSupport.on_load(:action_view) do
10 10
     class ActionView::Base
11 11
       @@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>".html_safe }
12 12
       cattr_accessor :field_error_proc
2  actionpack/lib/action_view/helpers/form_helper.rb
@@ -1221,7 +1221,7 @@ def nested_child_index(name)
1221 1221
     end
1222 1222
   end
1223 1223
 
1224  
-  ActionView.base_hook do
  1224
+  ActiveSupport.on_load(:action_view) do
1225 1225
     class ActionView::Base
1226 1226
       cattr_accessor :default_form_builder
1227 1227
       @@default_form_builder = ::ActionView::Helpers::FormBuilder
4  actionpack/lib/action_view/railtie.rb
@@ -10,14 +10,14 @@ class Railtie < Rails::Railtie
10 10
 
11 11
     initializer "action_view.cache_asset_timestamps" do |app|
12 12
       unless app.config.cache_classes
13  
-        ActionView.base_hook do
  13
+        ActiveSupport.on_load(:action_view) do
14 14
           ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
15 15
         end
16 16
       end
17 17
     end
18 18
 
19 19
     initializer "action_view.set_configs" do |app|
20  
-      ActionView.base_hook do
  20
+      ActiveSupport.on_load(:action_view) do
21 21
         app.config.action_view.each do |k,v|
22 22
           send "#{k}=", v
23 23
         end
6  activerecord/lib/active_record.rb
@@ -111,10 +111,10 @@ module ConnectionAdapters
111 111
 
112 112
   autoload :TestCase
113 113
   autoload :TestFixtures, 'active_record/fixtures'
  114
+end
114 115
 
115  
-  base_hook do
116  
-    Arel::Table.engine = Arel::Sql::Engine.new(self)
117  
-  end
  116
+ActiveSupport.on_load(:active_record) do
  117
+  Arel::Table.engine = Arel::Sql::Engine.new(self)
118 118
 end
119 119
 
120 120
 I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'
2  activerecord/lib/active_record/base.rb
@@ -2245,4 +2245,4 @@ def object_from_yaml(string)
2245 2245
 
2246 2246
 # TODO: Remove this and make it work with LAZY flag
2247 2247
 require 'active_record/connection_adapters/abstract_adapter'
2248  
-ActiveRecord.run_base_hooks(ActiveRecord::Base)
  2248
+ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
16  activerecord/lib/active_record/railtie.rb
@@ -23,18 +23,18 @@ class Railtie < Rails::Railtie
23 23
     log_subscriber :active_record, ActiveRecord::Railties::LogSubscriber.new
24 24
 
25 25
     initializer "active_record.initialize_timezone" do
26  
-      ActiveRecord.base_hook do
  26
+      ActiveSupport.on_load(:active_record) do
27 27
         self.time_zone_aware_attributes = true
28 28
         self.default_timezone = :utc
29 29
       end
30 30
     end
31 31
 
32 32
     initializer "active_record.logger" do
33  
-      ActiveRecord.base_hook { self.logger ||= ::Rails.logger }
  33
+      ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
34 34
     end
35 35
 
36 36
     initializer "active_record.set_configs" do |app|
37  
-      ActiveRecord.base_hook do
  37
+      ActiveSupport.on_load(:active_record) do
38 38
         app.config.active_record.each do |k,v|
39 39
           send "#{k}=", v
40 40
         end
@@ -44,7 +44,7 @@ class Railtie < Rails::Railtie
44 44
     # This sets the database configuration from Configuration#database_configuration
45 45
     # and then establishes the connection.
46 46
     initializer "active_record.initialize_database" do |app|
47  
-      ActiveRecord.base_hook do
  47
+      ActiveSupport.on_load(:active_record) do
48 48
         self.configurations = app.config.database_configuration
49 49
         establish_connection
50 50
       end
@@ -53,7 +53,7 @@ class Railtie < Rails::Railtie
53 53
     # Expose database runtime to controller for logging.
54 54
     initializer "active_record.log_runtime" do |app|
55 55
       require "active_record/railties/controller_runtime"
56  
-      ActionController.base_hook do
  56
+      ActiveSupport.on_load(:action_controller) do
57 57
         include ActiveRecord::Railties::ControllerRuntime
58 58
       end
59 59
     end
@@ -71,9 +71,9 @@ class Railtie < Rails::Railtie
71 71
     end
72 72
 
73 73
     initializer "active_record.load_observers" do
74  
-      ActiveRecord.base_hook { instantiate_observers }
  74
+      ActiveSupport.on_load(:active_record) { instantiate_observers }
75 75
 
76  
-      ActiveRecord.base_hook do
  76
+      ActiveSupport.on_load(:active_record) do
77 77
         ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do
78 78
           ActiveRecord::Base.instantiate_observers
79 79
         end
@@ -81,7 +81,7 @@ class Railtie < Rails::Railtie
81 81
     end
82 82
 
83 83
     initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
84  
-      ActiveRecord.base_hook do
  84
+      ActiveSupport.on_load(:active_record) do
85 85
         unless app.config.cache_classes
86 86
           ActionDispatch::Callbacks.after do
87 87
             ActiveRecord::Base.reset_subclasses
4  activesupport/lib/active_support/dependencies/autoload.rb
@@ -3,10 +3,6 @@
3 3
 
4 4
 module ActiveSupport
5 5
   module Autoload
6  
-    def self.extended(base)
7  
-      base.extend(LazyLoadHooks)
8  
-    end
9  
-
10 6
     @@autoloads = {}
11 7
     @@under_path = nil
12 8
     @@at_path = nil
2  activesupport/lib/active_support/i18n.rb
... ...
@@ -1,3 +1,3 @@
1 1
 require 'i18n'
2 2
 I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml"
3  
-ActiveSupport.run_base_hooks(:i18n)
  3
+ActiveSupport.run_load_hooks(:i18n)
30  activesupport/lib/active_support/lazy_load_hooks.rb
... ...
@@ -1,25 +1,17 @@
1 1
 module ActiveSupport
2  
-  module LazyLoadHooks
3  
-    def _setup_base_hooks
4  
-      @base_hooks ||= Hash.new {|h,k| h[k] = [] }
5  
-      @base ||= {}
6  
-    end
7  
-
8  
-    def base_hook(name = nil, &block)
9  
-      _setup_base_hooks
  2
+  @load_hooks = Hash.new {|h,k| h[k] = [] }
  3
+  @loaded = {}
10 4
 
11  
-      if base = @base[name]
12  
-        base.instance_eval(&block)
13  
-      else
14  
-        @base_hooks[name] << block
15  
-      end
  5
+  def self.on_load(name, &block)
  6
+    if base = @loaded[name]
  7
+      base.instance_eval(&block)
  8
+    else
  9
+      @load_hooks[name] << block
16 10
     end
  11
+  end
17 12
 
18  
-    def run_base_hooks(base, name = nil)
19  
-      _setup_base_hooks
20  
-
21  
-      @base_hooks[name].each { |hook| base.instance_eval(&hook) } if @base_hooks
22  
-      @base[name] = base
23  
-    end
  13
+  def self.run_load_hooks(name, base = Object)
  14
+    @load_hooks[name].each { |hook| base.instance_eval(&hook) }
  15
+    @loaded[name] = base
24 16
   end
25 17
 end
2  activesupport/lib/active_support/railtie.rb
@@ -35,7 +35,7 @@ class Railtie < Rails::Railtie
35 35
     config.i18n.load_path = []
36 36
 
37 37
     initializer "i18n.initialize" do
38  
-      ActiveSupport.base_hook(:i18n) do
  38
+      ActiveSupport.on_load(:i18n) do
39 39
         I18n.reload!
40 40
 
41 41
         ActionDispatch::Callbacks.to_prepare do
2  railties/lib/rails/application/routes_reloader.rb
@@ -27,7 +27,7 @@ def reload!
27 27
 
28 28
         routes.clear!
29 29
         paths.each { |path| load(path) }
30  
-        ActionController.base_hook { routes.finalize! }
  30
+        ActiveSupport.on_load(:action_controller) { routes.finalize! }
31 31
 
32 32
         nil
33 33
       ensure
9  railties/lib/rails/engine.rb
@@ -180,8 +180,13 @@ def load_tasks
180 180
 
181 181
     initializer :add_view_paths do
182 182
       views = paths.app.views.to_a
183  
-      ActionController.base_hook { prepend_view_path(views) } if defined?(ActionController)
184  
-      ActionMailer.base_hook { prepend_view_path(views) } if defined?(ActionMailer)
  183
+      ActiveSupport.on_load(:action_controller) do
  184
+        prepend_view_path(views)
  185
+      end
  186
+
  187
+      ActiveSupport.on_load(:action_mailer) do
  188
+        prepend_view_path(views)
  189
+      end
185 190
     end
186 191
 
187 192
     initializer :add_metals do |app|

0 notes on commit 4aded43

Please sign in to comment.
Something went wrong with that request. Please try again.