2010-02-04 12:29:18 -05:00
|
|
|
require "active_record"
|
|
|
|
require "rails"
|
|
|
|
require "active_model/railtie"
|
|
|
|
|
2009-12-23 17:55:12 -05:00
|
|
|
# For now, action_controller must always be present with
|
|
|
|
# rails, so let's make sure that it gets required before
|
|
|
|
# here. This is needed for correctly setting up the middleware.
|
|
|
|
# In the future, this might become an optional require.
|
2009-12-31 16:11:54 -05:00
|
|
|
require "action_controller/railtie"
|
2009-12-23 17:55:12 -05:00
|
|
|
|
|
|
|
module ActiveRecord
|
2009-12-31 16:11:54 -05:00
|
|
|
class Railtie < Rails::Railtie
|
2010-03-26 13:47:55 -04:00
|
|
|
config.active_record = ActiveSupport::OrderedOptions.new
|
2009-12-27 07:32:40 -05:00
|
|
|
|
2010-01-28 13:45:25 -05:00
|
|
|
config.generators.orm :active_record, :migration => true,
|
|
|
|
:timestamps => true
|
|
|
|
|
2009-12-28 20:37:18 -05:00
|
|
|
rake_tasks do
|
2009-12-30 22:24:00 -05:00
|
|
|
load "active_record/railties/databases.rake"
|
2009-12-28 20:37:18 -05:00
|
|
|
end
|
|
|
|
|
2010-02-15 09:44:30 -05:00
|
|
|
require "active_record/railties/log_subscriber"
|
2010-03-26 13:47:55 -04:00
|
|
|
log_subscriber :active_record, ActiveRecord::Railties::LogSubscriber.new
|
2010-01-12 12:29:48 -05:00
|
|
|
|
2010-01-22 14:44:29 -05:00
|
|
|
initializer "active_record.initialize_timezone" do
|
2010-03-07 09:24:30 -05:00
|
|
|
ActiveRecord.base_hook do
|
|
|
|
self.time_zone_aware_attributes = true
|
|
|
|
self.default_timezone = :utc
|
|
|
|
end
|
2010-01-22 14:44:29 -05:00
|
|
|
end
|
|
|
|
|
2010-01-24 19:06:12 -05:00
|
|
|
initializer "active_record.logger" do
|
2010-03-07 09:24:30 -05:00
|
|
|
ActiveRecord.base_hook { self.logger ||= ::Rails.logger }
|
2010-01-24 19:06:12 -05:00
|
|
|
end
|
|
|
|
|
2009-12-23 17:55:12 -05:00
|
|
|
initializer "active_record.set_configs" do |app|
|
2010-03-07 09:24:30 -05:00
|
|
|
ActiveRecord.base_hook do
|
|
|
|
app.config.active_record.each do |k,v|
|
|
|
|
send "#{k}=", v
|
|
|
|
end
|
2009-12-23 17:55:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This sets the database configuration from Configuration#database_configuration
|
|
|
|
# and then establishes the connection.
|
|
|
|
initializer "active_record.initialize_database" do |app|
|
2010-03-07 09:24:30 -05:00
|
|
|
ActiveRecord.base_hook do
|
|
|
|
self.configurations = app.config.database_configuration
|
|
|
|
establish_connection
|
|
|
|
end
|
2009-12-23 17:55:12 -05:00
|
|
|
end
|
|
|
|
|
2009-12-30 22:24:00 -05:00
|
|
|
# Expose database runtime to controller for logging.
|
|
|
|
initializer "active_record.log_runtime" do |app|
|
|
|
|
require "active_record/railties/controller_runtime"
|
2010-03-07 09:24:30 -05:00
|
|
|
ActionController.base_hook do
|
|
|
|
include ActiveRecord::Railties::ControllerRuntime
|
|
|
|
end
|
2009-12-30 22:24:00 -05:00
|
|
|
end
|
|
|
|
|
2009-12-23 17:55:12 -05:00
|
|
|
# Setup database middleware after initializers have run
|
2010-01-27 01:08:50 -05:00
|
|
|
initializer "active_record.initialize_database_middleware", :after => "action_controller.set_configs" do |app|
|
2009-12-23 17:55:12 -05:00
|
|
|
middleware = app.config.middleware
|
2010-01-25 16:59:08 -05:00
|
|
|
if middleware.include?("ActiveRecord::SessionStore")
|
|
|
|
middleware.insert_before "ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
|
|
|
|
middleware.insert_before "ActiveRecord::SessionStore", ActiveRecord::QueryCache
|
2009-12-23 17:55:12 -05:00
|
|
|
else
|
|
|
|
middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
|
|
|
middleware.use ActiveRecord::QueryCache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
initializer "active_record.load_observers" do
|
2010-03-07 09:24:30 -05:00
|
|
|
ActiveRecord.base_hook { instantiate_observers }
|
2010-01-14 13:53:07 -05:00
|
|
|
|
2010-03-07 09:24:30 -05:00
|
|
|
ActiveRecord.base_hook do
|
|
|
|
ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do
|
|
|
|
ActiveRecord::Base.instantiate_observers
|
|
|
|
end
|
2010-01-14 13:53:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
|
2010-03-07 09:24:30 -05:00
|
|
|
ActiveRecord.base_hook do
|
|
|
|
unless app.config.cache_classes
|
|
|
|
ActionDispatch::Callbacks.after do
|
|
|
|
ActiveRecord::Base.reset_subclasses
|
|
|
|
ActiveRecord::Base.clear_reloadable_connections!
|
|
|
|
end
|
2010-01-14 13:53:07 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-23 17:55:12 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-30 22:24:00 -05:00
|
|
|
end
|