Class: Audited::Sweeper

Inherits:
ActiveModel::Observer
  • Object
show all
Defined in:
lib/audited/sweeper.rb

Instance Method Summary collapse

Instance Method Details

#add_observer!(klass) ⇒ Object



23
24
25
26
# File 'lib/audited/sweeper.rb', line 23

def add_observer!(klass)
  super
  define_callback(klass)
end

#after(controller) ⇒ Object



10
11
12
# File 'lib/audited/sweeper.rb', line 10

def after(controller)
  self.controller = nil
end

#before(controller) ⇒ Object



5
6
7
8
# File 'lib/audited/sweeper.rb', line 5

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

#before_create(audit) ⇒ Object



14
15
16
17
# File 'lib/audited/sweeper.rb', line 14

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

#controllerObject



37
38
39
# File 'lib/audited/sweeper.rb', line 37

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

#controller=(value) ⇒ Object



41
42
43
# File 'lib/audited/sweeper.rb', line 41

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

#current_userObject



19
20
21
# File 'lib/audited/sweeper.rb', line 19

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

#define_callback(klass) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/audited/sweeper.rb', line 28

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