lib/audited/sweeper.rb in audited-4.2.2 vs lib/audited/sweeper.rb in audited-4.3.0
- old
+ new
@@ -1,19 +1,16 @@
require "rails/observers/activerecord/active_record"
require "rails/observers/action_controller/caching"
module Audited
class Sweeper < ActionController::Caching::Sweeper
- observe Audited.audit_class
+ observe Audited::Audit
- attr_accessor :controller
- def before(controller)
+ def around(controller)
self.controller = controller
- true
- end
-
- def after(controller)
+ yield
+ ensure
self.controller = nil
end
def before_create(audit)
audit.user ||= current_user
@@ -28,19 +25,17 @@
def request_uuid
controller.try(:request).try(:uuid)
end
def add_observer!(klass)
- if defined?(::ActiveRecord)
- super
- define_callback(klass)
- end
+ super
+ define_callback(klass)
end
def define_callback(klass)
observer = self
- callback_meth = :"_notify_audited_sweeper"
+ callback_meth = :_notify_audited_sweeper
klass.send(:define_method, callback_meth) do
observer.update(:before_create, self)
end
klass.send(:before_create, callback_meth)
end
@@ -53,19 +48,13 @@
::Audited.store[:current_controller] = value
end
end
end
-if defined?(ActionController) and defined?(ActionController::Base)
- # Create dynamic subclass of Audited::Sweeper otherwise rspec will
- # fail with both ActiveRecord and MongoMapper tests as there will be
- # around_filter collision
- sweeper_class = Class.new(Audited::Sweeper) do
- def self.name
- "#{Audited.audit_class}::Sweeper"
- end
+ActiveSupport.on_load(:action_controller) do
+ if defined?(ActionController::Base)
+ ActionController::Base.around_action Audited::Sweeper.instance
end
-
- ActionController::Base.class_eval do
- around_filter sweeper_class.instance
+ if defined?(ActionController::API)
+ ActionController::API.around_action Audited::Sweeper.instance
end
end