railtie.rb 3.3 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
    config.app_generators.orm :active_record, :migration => true,
17
                                              :timestamps => true
18

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

    config.app_middleware.insert_after "::ActionDispatch::Callbacks",
      "ActiveRecord::ConnectionAdapters::ConnectionManagement"
24

25
    rake_tasks do
26
      load "active_record/railties/databases.rake"
27 28
    end

29 30 31
    # 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.
32 33
    console do |sandbox|
      require "active_record/railties/console_sandbox" if sandbox
34
      ActiveRecord::Base.logger = Logger.new(STDERR)
35 36
    end

37
    initializer "active_record.initialize_timezone" do
38
      ActiveSupport.on_load(:active_record) do
39 40 41
        self.time_zone_aware_attributes = true
        self.default_timezone = :utc
      end
42 43
    end

44
    initializer "active_record.logger" do
45
      ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
46 47
    end

48 49 50 51 52
    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

53
    initializer "active_record.set_configs" do |app|
54
      ActiveSupport.on_load(:active_record) do
55 56 57
        if app.config.active_record.delete(:whitelist_attributes)
          attr_accessible(nil)
        end
58 59 60
        app.config.active_record.each do |k,v|
          send "#{k}=", v
        end
61 62 63 64 65 66
      end
    end

    # This sets the database configuration from Configuration#database_configuration
    # and then establishes the connection.
    initializer "active_record.initialize_database" do |app|
67
      ActiveSupport.on_load(:active_record) do
68 69 70
        self.configurations = app.config.database_configuration
        establish_connection
      end
71 72
    end

73 74 75
    # Expose database runtime to controller for logging.
    initializer "active_record.log_runtime" do |app|
      require "active_record/railties/controller_runtime"
76
      ActiveSupport.on_load(:action_controller) do
77 78
        include ActiveRecord::Railties::ControllerRuntime
      end
79 80
    end

81
    initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
82 83 84
      ActiveSupport.on_load(:active_record) do
        ActionDispatch::Reloader.to_cleanup do
          ActiveRecord::Base.clear_reloadable_connections!
85
          ActiveRecord::Base.clear_cache!
86
        end
87 88 89
      end
    end

90
    config.after_initialize do
91 92 93
      ActiveSupport.on_load(:active_record) do
        instantiate_observers

J
José Valim 已提交
94
        ActionDispatch::Reloader.to_prepare do
95
          ActiveRecord::Base.instantiate_observers
96 97
        end
      end
98 99
    end
  end
100
end