Module: Upkeep::Runtime::Install

Defined in:
lib/upkeep/runtime.rb

Class Method Summary collapse

Class Method Details

.callObject



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/upkeep/runtime.rb', line 1019

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



1069
1070
1071
1072
1073
1074
1075
# File 'lib/upkeep/runtime.rb', line 1069

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

.install_action_dispatch_observersObject



1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# File 'lib/upkeep/runtime.rb', line 1055

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



1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'lib/upkeep/runtime.rb', line 1037

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



1048
1049
1050
1051
1052
1053
# File 'lib/upkeep/runtime.rb', line 1048

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

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