Module: Upkeep::Runtime::Install

Defined in:
lib/upkeep/runtime.rb

Class Method Summary collapse

Class Method Details

.callObject



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'lib/upkeep/runtime.rb', line 1120

def call
  return if @installed

  install_current_attributes_observer
  install_warden_observer
  install_action_dispatch_observers

  ActiveRecord::AttributeMethods::Read.prepend(AttributeObserver)
  ActiveRecord::Base.prepend(PersistenceObserver) unless ActiveRecord::Base < PersistenceObserver
  ActiveRecord::Base.prepend(CacheKeyObserver) unless ActiveRecord::Base < CacheKeyObserver
  ActiveRecord::Relation.prepend(RelationObserver)

  ActiveRecord::Base.after_commit do |record|
    ChangeLog.record(ChangeEvents.active_record_commit(record))
  end

  @installed = true
end

.current_attribute_names(klass) ⇒ Object



1171
1172
1173
1174
1175
1176
1177
# File 'lib/upkeep/runtime.rb', line 1171

def current_attribute_names(klass)
  if klass.respond_to?(:defaults)
    klass.defaults.keys
  else
    []
  end
end

.install_action_dispatch_observersObject



1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
# File 'lib/upkeep/runtime.rb', line 1157

def install_action_dispatch_observers
  if defined?(::ActionDispatch::Request::Session) && !(::ActionDispatch::Request::Session < SessionObserver)
    ::ActionDispatch::Request::Session.prepend(SessionObserver)
  end

  if defined?(::ActionDispatch::Cookies::CookieJar) && !(::ActionDispatch::Cookies::CookieJar < CookieObserver)
    ::ActionDispatch::Cookies::CookieJar.prepend(CookieObserver)
  end

  if defined?(::ActionDispatch::Request) && !(::ActionDispatch::Request < RequestObserver)
    ::ActionDispatch::Request.prepend(RequestObserver)
  end
end

.install_current_attributes_observerObject



1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
# File 'lib/upkeep/runtime.rb', line 1139

def install_current_attributes_observer
  singleton = class << ActiveSupport::CurrentAttributes; self; end
  singleton.prepend(CurrentAttributesClassObserver) unless singleton < CurrentAttributesClassObserver

  ObjectSpace.each_object(Class) do |klass|
    next unless klass < ActiveSupport::CurrentAttributes

    Runtime.wrap_current_attribute_readers(klass, current_attribute_names(klass))
  end
end

.install_warden_observerObject



1150
1151
1152
1153
1154
1155
# File 'lib/upkeep/runtime.rb', line 1150

def install_warden_observer
  return unless defined?(::Warden::Proxy)
  return if ::Warden::Proxy < WardenObserver

  ::Warden::Proxy.prepend(WardenObserver)
end