Sha256: d7b2155c9b9c960503b088493e5e95665547338c394fd483e8c42e1123212788

Contents?: true

Size: 1.22 KB

Versions: 14

Compression:

Stored size: 1.22 KB

Contents

module NotifiablyAudited
  class Sweeper < ActiveModel::Observer
    observe NotifiablyAudited.audit_class

    def before(controller)
      self.controller = controller
      true
    end

    def after(controller)
      self.controller = nil
    end

    def before_create(audit)
      audit.user ||= current_user
      audit.remote_address = controller.try(:request).try(:ip)
    end

    def current_user
      controller.send(NotifiablyAudited.current_user_method) if controller.respond_to?(NotifiablyAudited.current_user_method, true)
    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
      ::NotifiablyAudited.store[:current_controller]
    end

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

if defined?(ActionController) and defined?(ActionController::Base)
  ActionController::Base.class_eval do
    around_filter NotifiablyAudited::Sweeper.instance
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
notifiably_audited-1.0.11 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.10 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.9 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.8 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.7 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.6 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.5 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.4 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.3 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.2 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.1 lib/notifiably_audited/sweeper.rb
notifiably_audited-1.0.0 lib/notifiably_audited/sweeper.rb
notifiably_audited-0.1.12 lib/notifiably_audited/sweeper.rb
notifiably_audited-0.1.11 lib/notifiably_audited/sweeper.rb