Module: Upkeep::Runtime::Install

Defined in:
lib/upkeep/runtime.rb

Class Method Summary collapse

Class Method Details

.callObject



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# File 'lib/upkeep/runtime.rb', line 1071

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



1122
1123
1124
1125
1126
1127
1128
# File 'lib/upkeep/runtime.rb', line 1122

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

.install_action_dispatch_observersObject



1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'lib/upkeep/runtime.rb', line 1108

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



1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'lib/upkeep/runtime.rb', line 1090

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



1101
1102
1103
1104
1105
1106
# File 'lib/upkeep/runtime.rb', line 1101

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

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