railtie.rb 3.0 KB
Newer Older
1 2 3 4
require "active_record"
require "rails"
require "active_model/railtie"

5 6 7 8
# 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.
C
Carl Lerche 已提交
9
require "action_controller/railtie"
10 11

module ActiveRecord
R
Rizwan Reza 已提交
12
  # = Active Record Railtie
C
Carl Lerche 已提交
13
  class Railtie < Rails::Railtie
14
    config.active_record = ActiveSupport::OrderedOptions.new
15

16 17 18
    config.generators.orm :active_record, :migration => true,
                                          :timestamps => true

19
    config.app_middleware.insert_after "::ActionDispatch::Callbacks", "ActiveRecord::QueryCache"
20

21
    rake_tasks do
22
      load "active_record/railties/databases.rake"
23 24
    end

25 26 27 28 29 30
    # 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

31
    initializer "active_record.initialize_timezone" do
32
      ActiveSupport.on_load(:active_record) do
33 34 35
        self.time_zone_aware_attributes = true
        self.default_timezone = :utc
      end
36 37
    end

38
    initializer "active_record.logger" do
39
      ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
40 41
    end

42
    initializer "active_record.set_configs" do |app|
43
      ActiveSupport.on_load(:active_record) do
44 45 46
        app.config.active_record.each do |k,v|
          send "#{k}=", v
        end
47 48 49 50 51 52
      end
    end

    # This sets the database configuration from Configuration#database_configuration
    # and then establishes the connection.
    initializer "active_record.initialize_database" do |app|
53
      ActiveSupport.on_load(:active_record) do
54 55 56
        self.configurations = app.config.database_configuration
        establish_connection
      end
57 58
    end

59 60 61
    # Expose database runtime to controller for logging.
    initializer "active_record.log_runtime" do |app|
      require "active_record/railties/controller_runtime"
62
      ActiveSupport.on_load(:action_controller) do
63 64
        include ActiveRecord::Railties::ControllerRuntime
      end
65 66
    end

67
    initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
68 69
      unless app.config.cache_classes
        ActiveSupport.on_load(:active_record) do
70 71 72
          ActionDispatch::Callbacks.after do
            ActiveRecord::Base.clear_reloadable_connections!
          end
73
        end
74 75 76
      end
    end

77 78 79 80 81 82 83
    initializer "active_record.add_concurrency_middleware" do |app|
      if app.config.allow_concurrency
        app.config.middleware.insert_after "::ActionDispatch::Callbacks",
          "ActiveRecord::ConnectionAdapters::ConnectionManagement"
      end
    end

84
    config.after_initialize do
85 86 87
      ActiveSupport.on_load(:active_record) do
        instantiate_observers

88 89
        ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do
          ActiveRecord::Base.instantiate_observers
90 91
        end
      end
92 93
    end
  end
94
end