Sha256: 5ad43e416c43c9cbef76255b70964efdb210d33c708ac0b599fd13c880ab5433

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

module Audited
  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
      STORED_DATA.keys.each { |k| store.delete(k) }
    end

    def current_user
      lambda { controller.send(Audited.current_user_method) if controller.respond_to?(Audited.current_user_method, true) }
    end

    def remote_ip
      controller.try(:request).try(:remote_ip)
    end

    def request_uuid
      controller.try(:request).try(:uuid)
    end

    def controller
      store[:current_controller]
    end

    def 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.new
  end
  if defined?(ActionController::API)
    ActionController::API.around_action Audited::Sweeper.new
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
audited-4.10.0 lib/audited/sweeper.rb
audited-4.9.0 lib/audited/sweeper.rb
audited-4.8.0 lib/audited/sweeper.rb
audited-4.7.1 lib/audited/sweeper.rb
audited-4.7.0 lib/audited/sweeper.rb
audited-4.6.0 lib/audited/sweeper.rb
audited-4.5.0 lib/audited/sweeper.rb