mirror of
https://github.com/github/rails.git
synced 2026-01-08 22:27:59 -05:00
AR 3.0 railtie
This commit is contained in:
@@ -13,8 +13,8 @@ module ActiveRecord
|
||||
class Railtie < Rails::Railtie
|
||||
config.active_record = ActiveSupport::OrderedOptions.new
|
||||
|
||||
config.app_generators.orm :active_record, :migration => true,
|
||||
:timestamps => true
|
||||
config.generators.orm :active_record, :migration => true,
|
||||
:timestamps => true
|
||||
|
||||
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
||||
"ActiveRecord::QueryCache"
|
||||
@@ -22,29 +22,14 @@ module ActiveRecord
|
||||
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
||||
"ActiveRecord::ConnectionAdapters::ConnectionManagement"
|
||||
|
||||
config.action_dispatch.rescue_responses.merge!(
|
||||
'ActiveRecord::RecordNotFound' => :not_found,
|
||||
'ActiveRecord::StaleObjectError' => :conflict,
|
||||
'ActiveRecord::RecordInvalid' => :unprocessable_entity,
|
||||
'ActiveRecord::RecordNotSaved' => :unprocessable_entity
|
||||
)
|
||||
|
||||
rake_tasks do
|
||||
require "active_record/base"
|
||||
load "active_record/railties/databases.rake"
|
||||
end
|
||||
|
||||
# When loading console, force ActiveRecord::Base to be loaded
|
||||
# to avoid cross references when loading a constant for the
|
||||
# first time. Also, make it output to STDERR.
|
||||
console do |app|
|
||||
require "active_record/railties/console_sandbox" if app.sandbox?
|
||||
require "active_record/base"
|
||||
ActiveRecord::Base.logger = Logger.new(STDERR)
|
||||
end
|
||||
|
||||
runner do |app|
|
||||
require "active_record/base"
|
||||
# When loading console, force ActiveRecord to be loaded to avoid cross
|
||||
# references when loading a constant for the first time.
|
||||
console do
|
||||
ActiveRecord::Base
|
||||
end
|
||||
|
||||
initializer "active_record.initialize_timezone" do
|
||||
@@ -58,16 +43,8 @@ module ActiveRecord
|
||||
ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
|
||||
end
|
||||
|
||||
initializer "active_record.identity_map" do |app|
|
||||
config.app_middleware.insert_after "::ActionDispatch::Callbacks",
|
||||
"ActiveRecord::IdentityMap::Middleware" if config.active_record.delete(:identity_map)
|
||||
end
|
||||
|
||||
initializer "active_record.set_configs" do |app|
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
if app.config.active_record.delete(:whitelist_attributes)
|
||||
attr_accessible(nil)
|
||||
end
|
||||
app.config.active_record.each do |k,v|
|
||||
send "#{k}=", v
|
||||
end
|
||||
@@ -78,13 +55,7 @@ module ActiveRecord
|
||||
# and then establishes the connection.
|
||||
initializer "active_record.initialize_database" do |app|
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
db_connection_type = "DATABASE_URL"
|
||||
unless ENV['DATABASE_URL']
|
||||
db_connection_type = "database.yml"
|
||||
self.configurations = app.config.database_configuration
|
||||
end
|
||||
Rails.logger.info "Connecting to database specified by #{db_connection_type}"
|
||||
|
||||
self.configurations = app.config.database_configuration
|
||||
establish_connection
|
||||
end
|
||||
end
|
||||
@@ -97,32 +68,21 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
|
||||
initializer "active_record.set_reloader_hooks" do |app|
|
||||
hook = lambda do
|
||||
ActiveRecord::Base.clear_reloadable_connections!
|
||||
ActiveRecord::Base.clear_cache!
|
||||
end
|
||||
|
||||
if app.config.reload_classes_only_on_change
|
||||
initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
|
||||
unless app.config.cache_classes
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
ActionDispatch::Reloader.to_prepare(&hook)
|
||||
end
|
||||
else
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
ActionDispatch::Reloader.to_cleanup(&hook)
|
||||
ActionDispatch::Callbacks.after do
|
||||
ActiveRecord::Base.clear_reloadable_connections!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
initializer "active_record.add_watchable_files" do |app|
|
||||
config.watchable_files.concat ["#{app.root}/db/schema.rb", "#{app.root}/db/structure.sql"]
|
||||
end
|
||||
|
||||
config.after_initialize do
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
instantiate_observers
|
||||
|
||||
ActionDispatch::Reloader.to_prepare do
|
||||
ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do
|
||||
ActiveRecord::Base.instantiate_observers
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user