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
2010-06-16 13:45:04 -04:00
# = Active Record Railtie
2012-09-22 00:38:18 -04:00
class Railtie < Rails :: Railtie # :nodoc:
2010-03-26 13:47:55 -04:00
config . active_record = ActiveSupport :: OrderedOptions . new
2009-12-27 07:32:40 -05:00
2010-09-29 11:41:30 -04:00
config . app_generators . orm :active_record , :migration = > true ,
2010-10-28 12:00:52 -04:00
:timestamps = > true
2010-01-28 13:45:25 -05:00
2010-08-20 21:55:02 -04:00
config . app_middleware . insert_after " ::ActionDispatch::Callbacks " ,
" ActiveRecord::QueryCache "
config . app_middleware . insert_after " ::ActionDispatch::Callbacks " ,
" ActiveRecord::ConnectionAdapters::ConnectionManagement "
2010-05-15 09:08:55 -04:00
2011-12-01 13:16:19 -05:00
config . action_dispatch . rescue_responses . merge! (
'ActiveRecord::RecordNotFound' = > :not_found ,
'ActiveRecord::StaleObjectError' = > :conflict ,
'ActiveRecord::RecordInvalid' = > :unprocessable_entity ,
'ActiveRecord::RecordNotSaved' = > :unprocessable_entity
)
2012-08-01 14:54:22 -04:00
2012-08-01 10:16:04 -04:00
config . active_record . use_schema_cache_dump = true
2012-08-01 14:54:22 -04:00
config . eager_load_namespaces << ActiveRecord
2009-12-28 20:37:18 -05:00
rake_tasks do
2012-05-28 11:47:10 -04:00
require " active_record/base "
2013-04-24 09:37:55 -04:00
2013-04-24 14:57:09 -04:00
ActiveRecord :: Tasks :: DatabaseTasks . env = Rails . env
2013-04-24 09:37:55 -04:00
ActiveRecord :: Tasks :: DatabaseTasks . db_dir = Rails . application . config . paths [ " db " ] . first
ActiveRecord :: Tasks :: DatabaseTasks . seed_loader = Rails . application
ActiveRecord :: Tasks :: DatabaseTasks . database_configuration = Rails . application . config . database_configuration
ActiveRecord :: Tasks :: DatabaseTasks . migrations_paths = Rails . application . paths [ 'db/migrate' ] . to_a
2013-04-24 14:38:20 -04:00
ActiveRecord :: Tasks :: DatabaseTasks . fixtures_path = File . join Rails . root , 'test' , 'fixtures'
2013-04-24 09:37:55 -04:00
if defined? ( ENGINE_PATH ) && engine = Rails :: Engine . find ( ENGINE_PATH )
if engine . paths [ 'db/migrate' ] . existent
ActiveRecord :: Tasks :: DatabaseTasks . migrations_paths += engine . paths [ 'db/migrate' ] . to_a
end
end
2009-12-30 22:24:00 -05:00
load " active_record/railties/databases.rake "
2009-12-28 20:37:18 -05:00
end
2011-05-04 10:31:06 -04:00
# 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.
2011-05-24 19:37:55 -04:00
console do | app |
require " active_record/railties/console_sandbox " if app . sandbox?
2012-05-28 11:47:10 -04:00
require " active_record/base "
2012-01-20 14:56:38 -05:00
console = ActiveSupport :: Logger . new ( STDERR )
2012-01-20 17:18:29 -05:00
Rails . logger . extend ActiveSupport :: Logger . broadcast console
2010-07-17 04:59:41 -04:00
end
2013-03-14 01:17:13 -04:00
runner do
2012-05-29 10:43:46 -04:00
require " active_record/base "
end
2010-01-22 14:44:29 -05:00
initializer " active_record.initialize_timezone " do
2010-03-29 20:08:08 -04:00
ActiveSupport . on_load ( :active_record ) do
2010-03-07 09:24:30 -05:00
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-29 20:08:08 -04:00
ActiveSupport . on_load ( :active_record ) { self . logger || = :: Rails . logger }
2010-01-24 19:06:12 -05:00
end
2013-03-14 01:17:13 -04:00
initializer " active_record.migration_error " do
2012-06-05 20:15:16 -04:00
if config . active_record . delete ( :migration_error ) == :page_load
config . app_middleware . insert_after " ::ActionDispatch::Callbacks " ,
" ActiveRecord::Migration::CheckPending "
end
end
2012-09-08 03:11:42 -04:00
initializer " active_record.check_schema_cache_dump " do
2012-08-01 10:16:04 -04:00
if config . active_record . delete ( :use_schema_cache_dump )
config . after_initialize do | app |
ActiveSupport . on_load ( :active_record ) do
filename = File . join ( app . config . paths [ " db " ] . first , " schema_cache.dump " )
2012-09-22 00:38:18 -04:00
2012-08-01 10:16:04 -04:00
if File . file? ( filename )
cache = Marshal . load File . binread filename
if cache . version == ActiveRecord :: Migrator . current_version
2012-10-26 10:51:02 -04:00
self . connection . schema_cache = cache
2012-08-01 10:16:04 -04:00
else
2012-09-24 08:48:55 -04:00
warn " Ignoring db/schema_cache.dump because it has expired. The current schema version is #{ ActiveRecord :: Migrator . current_version } , but the one in the cache is #{ cache . version } . "
2012-08-01 10:16:04 -04:00
end
end
end
end
end
end
2009-12-23 17:55:12 -05:00
initializer " active_record.set_configs " do | app |
2010-03-29 20:08:08 -04:00
ActiveSupport . on_load ( :active_record ) do
2012-11-09 10:21:05 -05:00
begin
old_behavior , ActiveSupport :: Deprecation . behavior = ActiveSupport :: Deprecation . behavior , :stderr
whitelist_attributes = app . config . active_record . delete ( :whitelist_attributes )
if respond_to? ( :mass_assignment_sanitizer = )
mass_assignment_sanitizer = nil
else
mass_assignment_sanitizer = app . config . active_record . delete ( :mass_assignment_sanitizer )
end
unless whitelist_attributes . nil? && mass_assignment_sanitizer . nil?
ActiveSupport :: Deprecation . warn <<-EOF.strip_heredoc, []
Model based mass assignment security has been extracted
out of Rails into a gem . Please use the new recommended protection model for
params or add ` protected_attributes ` to your Gemfile to use the old one .
To disable this message remove the ` whitelist_attributes ` option from your
` config/application.rb ` file and any ` mass_assignment_sanitizer ` options
from your ` config/environments/*.rb ` files .
2013-02-24 12:59:34 -05:00
See http : / / guides . rubyonrails . org / security . html #mass-assignment for more information.
EOF
end
unless app . config . active_record . delete ( :auto_explain_threshold_in_seconds ) . nil?
ActiveSupport :: Deprecation . warn <<-EOF.strip_heredoc, []
The Active Record auto explain feature has been removed .
To disable this message remove the ` active_record.auto_explain_threshold_in_seconds `
option from the ` config/environments/*.rb ` config file .
See http : / / guides . rubyonrails . org / 4_0 _release_notes . html for more information .
2012-11-09 10:21:05 -05:00
EOF
end
2012-11-28 18:55:07 -05:00
unless app . config . active_record . delete ( :observers ) . nil?
ActiveSupport :: Deprecation . warn <<-EOF.strip_heredoc, []
Active Record Observers has been extracted out of Rails into a gem .
2012-11-28 20:08:41 -05:00
Please use callbacks or add ` rails-observers ` to your Gemfile to use observers .
2012-11-28 18:55:07 -05:00
To disable this message remove the ` observers ` option from your
` config/application.rb ` or from your initializers .
2013-02-24 12:59:34 -05:00
See http : / / guides . rubyonrails . org / 4_0 _release_notes . html for more information .
2012-11-28 18:55:07 -05:00
EOF
end
2012-11-09 10:21:05 -05:00
ensure
ActiveSupport :: Deprecation . behavior = old_behavior
end
2010-03-07 09:24:30 -05:00
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-29 20:08:08 -04:00
ActiveSupport . on_load ( :active_record ) do
2013-03-02 15:52:32 -05:00
self . configurations = app . config . database_configuration || { }
2010-03-07 09:24:30 -05:00
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.
2013-03-14 01:17:13 -04:00
initializer " active_record.log_runtime " do
2009-12-30 22:24:00 -05:00
require " active_record/railties/controller_runtime "
2010-03-29 20:08:08 -04:00
ActiveSupport . on_load ( :action_controller ) do
2010-03-07 09:24:30 -05:00
include ActiveRecord :: Railties :: ControllerRuntime
end
2009-12-30 22:24:00 -05:00
end
2011-12-15 12:48:10 -05:00
initializer " active_record.set_reloader_hooks " do | app |
2012-06-15 07:09:19 -04:00
hook = app . config . reload_classes_only_on_change ? :to_prepare : :to_cleanup
2011-12-15 12:48:10 -05:00
2012-06-15 07:09:19 -04:00
ActiveSupport . on_load ( :active_record ) do
ActionDispatch :: Reloader . send ( hook ) do
2012-11-12 20:24:10 -05:00
if ActiveRecord :: Base . connected?
ActiveRecord :: Base . clear_reloadable_connections!
ActiveRecord :: Base . clear_cache!
end
2010-03-07 09:24:30 -05:00
end
2010-01-14 13:53:07 -05:00
end
end
2011-12-12 16:51:33 -05:00
initializer " active_record.add_watchable_files " do | app |
2012-10-08 07:53:25 -04:00
path = app . paths [ " db " ] . first
config . watchable_files . concat [ " #{ path } /schema.rb " , " #{ path } /structure.sql " ]
2011-12-12 16:51:33 -05:00
end
2009-12-23 17:55:12 -05:00
end
2009-12-30 22:24:00 -05:00
end