Module: Upkeep::Runtime::Install

Defined in:
lib/upkeep/runtime.rb

Class Method Summary collapse

Class Method Details

.callObject



994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
# File 'lib/upkeep/runtime.rb', line 994

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::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



1044
1045
1046
1047
1048
1049
1050
# File 'lib/upkeep/runtime.rb', line 1044

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

.install_action_dispatch_observersObject



1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/upkeep/runtime.rb', line 1030

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



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/upkeep/runtime.rb', line 1012

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



1023
1024
1025
1026
1027
1028
# File 'lib/upkeep/runtime.rb', line 1023

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

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