-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `Audited::Sweeper` class made limited use of the cache sweeping functionality provided by rails-observer, mostly using it as a way to implement `Audit#before_create` on the same class that was attached around controller actions. Since there is no released version of rails-observer that's compatible with Rails 5.0 (and not in the foreseeable future without considerable refactoring), the population of IPs, users and request UUIDs is replaced by thread-local storage and the existing `Audit#before_create` methods. This is a net code reduction and somewhat simplifies the "sweeper" by removing the assumptions about how rails-observer works.
- Loading branch information
Showing
9 changed files
with
36 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
require 'rails/observers/active_model/active_model' | ||
require 'active_record' | ||
|
||
module Audited | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,49 @@ | ||
require "rails/observers/activerecord/active_record" | ||
require "rails/observers/action_controller/caching" | ||
|
||
module Audited | ||
class Sweeper < ActionController::Caching::Sweeper | ||
observe Audited::Audit | ||
class Sweeper | ||
STORED_DATA = { | ||
current_remote_address: :remote_ip, | ||
current_request_uuid: :request_uuid, | ||
current_user: :current_user | ||
} | ||
|
||
delegate :store, to: ::Audited | ||
|
||
def around(controller) | ||
self.controller = controller | ||
STORED_DATA.each { |k,m| store[k] = send(m) } | ||
yield | ||
ensure | ||
self.controller = nil | ||
end | ||
|
||
def before_create(audit) | ||
audit.user ||= current_user | ||
audit.remote_address = controller.try(:request).try(:remote_ip) | ||
audit.request_uuid = request_uuid if request_uuid | ||
STORED_DATA.keys.each { |k| store.delete(k) } | ||
end | ||
|
||
def current_user | ||
controller.send(Audited.current_user_method) if controller.respond_to?(Audited.current_user_method, true) | ||
end | ||
|
||
def request_uuid | ||
controller.try(:request).try(:uuid) | ||
end | ||
|
||
def add_observer!(klass) | ||
super | ||
define_callback(klass) | ||
def remote_ip | ||
controller.try(:request).try(:remote_ip) | ||
end | ||
|
||
def define_callback(klass) | ||
observer = self | ||
callback_meth = :_notify_audited_sweeper | ||
klass.send(:define_method, callback_meth) do | ||
observer.update(:before_create, self) | ||
end | ||
klass.send(:before_create, callback_meth) | ||
def request_uuid | ||
controller.try(:request).try(:uuid) | ||
end | ||
|
||
def controller | ||
::Audited.store[:current_controller] | ||
store[:current_controller] | ||
end | ||
|
||
def controller=(value) | ||
::Audited.store[:current_controller] = value | ||
store[:current_controller] = value | ||
end | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:action_controller) do | ||
if defined?(ActionController::Base) | ||
ActionController::Base.around_action Audited::Sweeper.instance | ||
ActionController::Base.around_action Audited::Sweeper.new | ||
end | ||
if defined?(ActionController::API) | ||
ActionController::API.around_action Audited::Sweeper.instance | ||
ActionController::API.around_action Audited::Sweeper.new | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters