File: railtie.rb

package info (click to toggle)
ruby-rails-observers 0.1.5-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 164 kB
  • sloc: ruby: 449; makefile: 3
file content (63 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'rails/railtie'

module Rails
  module Observers
    class Railtie < ::Rails::Railtie
      initializer "active_record.observer", :before => "active_record.set_configs" do |app|
        ActiveSupport.on_load(:active_record) do
          require "rails/observers/activerecord/active_record"
          observers = app.config.active_record.delete(:observers)
          self.observers = observers if observers
        end
      end

      initializer "action_controller.caching.sweepers" do
        ActiveSupport.on_load(:action_controller) do
          require "rails/observers/action_controller/caching"
        end
      end

      initializer "active_resource.observer" do |app|
        ActiveSupport.on_load(:active_resource) do
          require 'rails/observers/active_resource/observing'

          prepend ActiveResource::Observing
        end
      end

      config.after_initialize do |app|
        begin
          # Eager load `ActiveRecord::Base` to avoid circular references when
          # loading a constant for the first time.
          #
          # E.g. loading a `User` model that references `ActiveRecord::Base`
          # which calls `instantiate_observers` to instantiate a `UserObserver`
          # which eventually calls `observed_class` thus constantizing `"User"`,
          # the class we're loading. 💣💥
          require "active_record/base"
        rescue LoadError
        end

        ActiveSupport.on_load(:active_record) do
          ActiveRecord::Base.instantiate_observers

          # Rails 5.1 forward-compat. AD::R is deprecated to AS::R in Rails 5.
          reloader = defined?(ActiveSupport::Reloader) ? ActiveSupport::Reloader : ActionDispatch::Reloader
          reloader.to_prepare do
            ActiveRecord::Base.instantiate_observers
          end
        end

        ActiveSupport.on_load(:active_resource) do
          self.instantiate_observers

          # Rails 5.1 forward-compat. AD::R is deprecated to AS::R in Rails 5.
          reloader = defined?(ActiveSupport::Reloader) ? ActiveSupport::Reloader : ActionDispatch::Reloader
          reloader.to_prepare do
            ActiveResource::Base.instantiate_observers
          end
        end
      end
    end
  end
end