1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Replace the placeholder base_hook API with on_load. To specify some code that

should run during framework load do:

ActiveSupport.on_load(:action_controller) do
  # Code run in the context of AC::Base
end
This commit is contained in:
wycats 2010-03-29 17:08:08 -07:00
parent 331327d391
commit 4aded43b73
17 changed files with 49 additions and 57 deletions

View file

@ -291,7 +291,7 @@ module ActionMailer #:nodoc:
:parts_order => [ "text/plain", "text/enriched", "text/html" ] :parts_order => [ "text/plain", "text/enriched", "text/html" ]
}.freeze }.freeze
ActionMailer.run_base_hooks(self) ActiveSupport.run_load_hooks(:action_mailer, self)
class << self class << self

View file

@ -6,18 +6,18 @@ module ActionMailer
config.action_mailer = ActiveSupport::OrderedOptions.new config.action_mailer = ActiveSupport::OrderedOptions.new
initializer "action_mailer.url_for", :before => :load_environment_config do |app| initializer "action_mailer.url_for", :before => :load_environment_config do |app|
ActionMailer.base_hook { include app.routes.url_helpers } ActiveSupport.on_load(:action_mailer) { include app.routes.url_helpers }
end end
require "action_mailer/railties/log_subscriber" require "action_mailer/railties/log_subscriber"
log_subscriber :action_mailer, ActionMailer::Railties::LogSubscriber.new log_subscriber :action_mailer, ActionMailer::Railties::LogSubscriber.new
initializer "action_mailer.logger" do initializer "action_mailer.logger" do
ActionMailer.base_hook { self.logger ||= Rails.logger } ActiveSupport.on_load(:action_mailer) { self.logger ||= Rails.logger }
end end
initializer "action_mailer.set_configs" do |app| initializer "action_mailer.set_configs" do |app|
ActionMailer.base_hook do ActiveSupport.on_load(:action_mailer) do
app.config.action_mailer.each do |k,v| app.config.action_mailer.each do |k,v|
send "#{k}=", v send "#{k}=", v
end end

View file

@ -64,8 +64,7 @@ module ActionController
filter filter
end end
ActionController.run_base_hooks(self) ActiveSupport.run_load_hooks(:action_controller, self)
end end
end end

View file

@ -41,7 +41,7 @@ module ActionController
log_subscriber :action_controller, ActionController::Railties::LogSubscriber.new log_subscriber :action_controller, ActionController::Railties::LogSubscriber.new
initializer "action_controller.logger" do initializer "action_controller.logger" do
ActionController.base_hook { self.logger ||= Rails.logger } ActiveSupport.on_load(:action_controller) { self.logger ||= Rails.logger }
end end
initializer "action_controller.set_configs" do |app| initializer "action_controller.set_configs" do |app|
@ -53,23 +53,23 @@ module ActionController
ac.stylesheets_dir = paths.public.stylesheets.to_a.first ac.stylesheets_dir = paths.public.stylesheets.to_a.first
ac.secret = app.config.cookie_secret ac.secret = app.config.cookie_secret
ActionController.base_hook do ActiveSupport.on_load(:action_controller) do
self.config.merge!(ac) self.config.merge!(ac)
end end
end end
initializer "action_controller.initialize_framework_caches" do initializer "action_controller.initialize_framework_caches" do
ActionController.base_hook { self.cache_store ||= RAILS_CACHE } ActiveSupport.on_load(:action_controller) { self.cache_store ||= RAILS_CACHE }
end end
initializer "action_controller.set_helpers_path" do |app| initializer "action_controller.set_helpers_path" do |app|
ActionController.base_hook do ActiveSupport.on_load(:action_controller) do
self.helpers_path = app.config.paths.app.helpers.to_a self.helpers_path = app.config.paths.app.helpers.to_a
end end
end end
initializer "action_controller.url_helpers" do |app| initializer "action_controller.url_helpers" do |app|
ActionController.base_hook do ActiveSupport.on_load(:action_controller) do
extend ::ActionController::Railties::UrlHelpers.with(app.routes) extend ::ActionController::Railties::UrlHelpers.with(app.routes)
end end

View file

@ -173,7 +173,7 @@ module ActionView #:nodoc:
delegate :logger, :to => 'ActionController::Base', :allow_nil => true delegate :logger, :to => 'ActionController::Base', :allow_nil => true
end end
ActionView.run_base_hooks(self) ActiveSupport.run_load_hooks(:action_view, self)
attr_accessor :base_path, :assigns, :template_extension, :lookup_context attr_accessor :base_path, :assigns, :template_extension, :lookup_context
attr_internal :captures, :request, :controller, :template, :config attr_internal :captures, :request, :controller, :template, :config

View file

@ -6,7 +6,7 @@ require 'active_support/core_ext/kernel/reporting'
require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/blank'
module ActionView module ActionView
ActionView.base_hook do ActiveSupport.on_load(:action_view) do
class ActionView::Base class ActionView::Base
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>".html_safe } @@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>".html_safe }
cattr_accessor :field_error_proc cattr_accessor :field_error_proc

View file

@ -1221,7 +1221,7 @@ module ActionView
end end
end end
ActionView.base_hook do ActiveSupport.on_load(:action_view) do
class ActionView::Base class ActionView::Base
cattr_accessor :default_form_builder cattr_accessor :default_form_builder
@@default_form_builder = ::ActionView::Helpers::FormBuilder @@default_form_builder = ::ActionView::Helpers::FormBuilder

View file

@ -10,14 +10,14 @@ module ActionView
initializer "action_view.cache_asset_timestamps" do |app| initializer "action_view.cache_asset_timestamps" do |app|
unless app.config.cache_classes unless app.config.cache_classes
ActionView.base_hook do ActiveSupport.on_load(:action_view) do
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
end end
end end
end end
initializer "action_view.set_configs" do |app| initializer "action_view.set_configs" do |app|
ActionView.base_hook do ActiveSupport.on_load(:action_view) do
app.config.action_view.each do |k,v| app.config.action_view.each do |k,v|
send "#{k}=", v send "#{k}=", v
end end

View file

@ -111,10 +111,10 @@ module ActiveRecord
autoload :TestCase autoload :TestCase
autoload :TestFixtures, 'active_record/fixtures' autoload :TestFixtures, 'active_record/fixtures'
base_hook do
Arel::Table.engine = Arel::Sql::Engine.new(self)
end end
ActiveSupport.on_load(:active_record) do
Arel::Table.engine = Arel::Sql::Engine.new(self)
end end
I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml' I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'

View file

@ -2245,4 +2245,4 @@ end
# TODO: Remove this and make it work with LAZY flag # TODO: Remove this and make it work with LAZY flag
require 'active_record/connection_adapters/abstract_adapter' require 'active_record/connection_adapters/abstract_adapter'
ActiveRecord.run_base_hooks(ActiveRecord::Base) ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)

View file

@ -23,18 +23,18 @@ module ActiveRecord
log_subscriber :active_record, ActiveRecord::Railties::LogSubscriber.new log_subscriber :active_record, ActiveRecord::Railties::LogSubscriber.new
initializer "active_record.initialize_timezone" do initializer "active_record.initialize_timezone" do
ActiveRecord.base_hook do ActiveSupport.on_load(:active_record) do
self.time_zone_aware_attributes = true self.time_zone_aware_attributes = true
self.default_timezone = :utc self.default_timezone = :utc
end end
end end
initializer "active_record.logger" do initializer "active_record.logger" do
ActiveRecord.base_hook { self.logger ||= ::Rails.logger } ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
end end
initializer "active_record.set_configs" do |app| initializer "active_record.set_configs" do |app|
ActiveRecord.base_hook do ActiveSupport.on_load(:active_record) do
app.config.active_record.each do |k,v| app.config.active_record.each do |k,v|
send "#{k}=", v send "#{k}=", v
end end
@ -44,7 +44,7 @@ module ActiveRecord
# This sets the database configuration from Configuration#database_configuration # This sets the database configuration from Configuration#database_configuration
# and then establishes the connection. # and then establishes the connection.
initializer "active_record.initialize_database" do |app| initializer "active_record.initialize_database" do |app|
ActiveRecord.base_hook do ActiveSupport.on_load(:active_record) do
self.configurations = app.config.database_configuration self.configurations = app.config.database_configuration
establish_connection establish_connection
end end
@ -53,7 +53,7 @@ module ActiveRecord
# Expose database runtime to controller for logging. # Expose database runtime to controller for logging.
initializer "active_record.log_runtime" do |app| initializer "active_record.log_runtime" do |app|
require "active_record/railties/controller_runtime" require "active_record/railties/controller_runtime"
ActionController.base_hook do ActiveSupport.on_load(:action_controller) do
include ActiveRecord::Railties::ControllerRuntime include ActiveRecord::Railties::ControllerRuntime
end end
end end
@ -71,9 +71,9 @@ module ActiveRecord
end end
initializer "active_record.load_observers" do initializer "active_record.load_observers" do
ActiveRecord.base_hook { instantiate_observers } ActiveSupport.on_load(:active_record) { instantiate_observers }
ActiveRecord.base_hook do ActiveSupport.on_load(:active_record) do
ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do
ActiveRecord::Base.instantiate_observers ActiveRecord::Base.instantiate_observers
end end
@ -81,7 +81,7 @@ module ActiveRecord
end end
initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app| initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
ActiveRecord.base_hook do ActiveSupport.on_load(:active_record) do
unless app.config.cache_classes unless app.config.cache_classes
ActionDispatch::Callbacks.after do ActionDispatch::Callbacks.after do
ActiveRecord::Base.reset_subclasses ActiveRecord::Base.reset_subclasses

View file

@ -3,10 +3,6 @@ require "active_support/lazy_load_hooks"
module ActiveSupport module ActiveSupport
module Autoload module Autoload
def self.extended(base)
base.extend(LazyLoadHooks)
end
@@autoloads = {} @@autoloads = {}
@@under_path = nil @@under_path = nil
@@at_path = nil @@at_path = nil

View file

@ -1,3 +1,3 @@
require 'i18n' require 'i18n'
I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml" I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml"
ActiveSupport.run_base_hooks(:i18n) ActiveSupport.run_load_hooks(:i18n)

View file

@ -1,25 +1,17 @@
module ActiveSupport module ActiveSupport
module LazyLoadHooks @load_hooks = Hash.new {|h,k| h[k] = [] }
def _setup_base_hooks @loaded = {}
@base_hooks ||= Hash.new {|h,k| h[k] = [] }
@base ||= {}
end
def base_hook(name = nil, &block) def self.on_load(name, &block)
_setup_base_hooks if base = @loaded[name]
if base = @base[name]
base.instance_eval(&block) base.instance_eval(&block)
else else
@base_hooks[name] << block @load_hooks[name] << block
end end
end end
def run_base_hooks(base, name = nil) def self.run_load_hooks(name, base = Object)
_setup_base_hooks @load_hooks[name].each { |hook| base.instance_eval(&hook) }
@loaded[name] = base
@base_hooks[name].each { |hook| base.instance_eval(&hook) } if @base_hooks
@base[name] = base
end
end end
end end

View file

@ -35,7 +35,7 @@ module I18n
config.i18n.load_path = [] config.i18n.load_path = []
initializer "i18n.initialize" do initializer "i18n.initialize" do
ActiveSupport.base_hook(:i18n) do ActiveSupport.on_load(:i18n) do
I18n.reload! I18n.reload!
ActionDispatch::Callbacks.to_prepare do ActionDispatch::Callbacks.to_prepare do

View file

@ -27,7 +27,7 @@ module Rails
routes.clear! routes.clear!
paths.each { |path| load(path) } paths.each { |path| load(path) }
ActionController.base_hook { routes.finalize! } ActiveSupport.on_load(:action_controller) { routes.finalize! }
nil nil
ensure ensure

View file

@ -180,8 +180,13 @@ module Rails
initializer :add_view_paths do initializer :add_view_paths do
views = paths.app.views.to_a views = paths.app.views.to_a
ActionController.base_hook { prepend_view_path(views) } if defined?(ActionController) ActiveSupport.on_load(:action_controller) do
ActionMailer.base_hook { prepend_view_path(views) } if defined?(ActionMailer) prepend_view_path(views)
end
ActiveSupport.on_load(:action_mailer) do
prepend_view_path(views)
end
end end
initializer :add_metals do |app| initializer :add_metals do |app|