railtie.rb 3.2 KB
Newer Older
1 2 3 4
# 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.
5
require "active_record"
C
Carl Lerche 已提交
6
require "action_controller/railtie"
7
require "rails"
8 9

module ActiveRecord
C
Carl Lerche 已提交
10
  class Railtie < Rails::Railtie
11
    railtie_name :active_record
12

13
    rake_tasks do
14
      load "active_record/railties/databases.rake"
15 16
    end

17 18 19 20
    # TODO If we require the wrong file, the error never comes up.
    require "active_record/railties/subscriber"
    subscriber ActiveRecord::Railties::Subscriber.new

21 22 23 24 25
    initializer "active_record.initialize_timezone" do
      ActiveRecord::Base.time_zone_aware_attributes = true
      ActiveRecord::Base.default_timezone = :utc
    end

26 27 28 29 30 31 32 33 34 35 36 37 38
    initializer "active_record.set_configs" do |app|
      app.config.active_record.each do |k,v|
        ActiveRecord::Base.send "#{k}=", v
      end
    end

    # This sets the database configuration from Configuration#database_configuration
    # and then establishes the connection.
    initializer "active_record.initialize_database" do |app|
      ActiveRecord::Base.configurations = app.config.database_configuration
      ActiveRecord::Base.establish_connection
    end

39 40 41 42 43 44
    # Expose database runtime to controller for logging.
    initializer "active_record.log_runtime" do |app|
      require "active_record/railties/controller_runtime"
      ActionController::Base.send :include, ActiveRecord::Railties::ControllerRuntime
    end

45 46 47 48 49 50 51 52 53 54 55 56 57 58
    # Setup database middleware after initializers have run
    initializer "active_record.initialize_database_middleware" do |app|
      middleware = app.config.middleware
      if middleware.include?(ActiveRecord::SessionStore)
        middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::ConnectionAdapters::ConnectionManagement
        middleware.insert_before ActiveRecord::SessionStore, ActiveRecord::QueryCache
      else
        middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
        middleware.use ActiveRecord::QueryCache
      end
    end

    initializer "active_record.load_observers" do
      ActiveRecord::Base.instantiate_observers
59 60 61 62 63 64 65 66 67 68 69 70 71

      ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do
        ActiveRecord::Base.instantiate_observers
      end
    end

    initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
      unless app.config.cache_classes
        ActionDispatch::Callbacks.after do
          ActiveRecord::Base.reset_subclasses
          ActiveRecord::Base.clear_reloadable_connections!
        end
      end
72 73 74 75
    end

    # TODO: ActiveRecord::Base.logger should delegate to its own config.logger
    initializer "active_record.logger" do
76
      ActiveRecord::Base.logger ||= ::Rails.logger
77 78
    end

79 80 81 82 83 84 85 86 87 88 89
    initializer "active_record.i18n_deprecation" do
      require 'active_support/i18n'

      begin
        I18n.t(:"activerecord.errors", :raise => true)
        warn "[DEPRECATION] \"activerecord.errors\" namespace is deprecated in I18n " << 
          "yml files, please use just \"errors\" instead."
      rescue Exception => e
        # No message then.
      end
    end
90
  end
91
end