Sha256: 1119ee2a740499c2c54bf9f98f2341085a5b3147d61714cb1542c94420efb752

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

require "rails/observers/activerecord/active_record"
require "rails/observers/action_controller/caching"

module Audited
  class Sweeper < ActionController::Caching::Sweeper
    observe Audited::Audit

    def around(controller)
      self.controller = controller
      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
    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)
    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)
    end

    def controller
      ::Audited.store[:current_controller]
    end

    def controller=(value)
      ::Audited.store[:current_controller] = value
    end
  end
end

ActiveSupport.on_load(:action_controller) do
  if defined?(ActionController::Base)
    ActionController::Base.around_action Audited::Sweeper.instance
  end
  if defined?(ActionController::API)
    ActionController::API.around_action Audited::Sweeper.instance
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
audited-hp-4.3.1 lib/audited/sweeper.rb
audited-hp-4.3.0 lib/audited/sweeper.rb
audited-4.3.0 lib/audited/sweeper.rb