Module: Platanus::Traceable

Defined in:
lib/platanus/traceable.rb

Overview

When included in a model, this module will provide seamless C®UD operations tracing.

This module operates under a couple of conventions:

  • When a new operation is detected it will look for the user_id method for the current controller

and assing it’s value to the <action>_by attribute of the current model.

  • When a new operation is detected the controllers trace method is called passing the action and

the current model.

This module will also trace Activable.remove calls. Will only work if the gcontroller mod is active.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/platanus/traceable.rb', line 21

def self.included(base)
  # Make sure gcontroller was loaded.
  unless ActionController::Base.respond_to? :current
    # TODO: better warning!
    base.logger.warn 'gcontroller not loaded, tracing disabled'
    return
  end

  base.around_create :__trace_create
  base.around_update :__trace_update
  base.around_destroy :__trace_destroy
  # Activable support (remove event).
  begin; base.set_callback :remove, :around, :__trace_remove; rescue; end
end

Instance Method Details

#__trace_createObject

CALLBACKS



38
39
40
41
42
43
44
45
# File 'lib/platanus/traceable.rb', line 38

def __trace_create # :nodoc:
  controller = ActionController::Base.current
  if controller and controller.respond_to? :trace_user_id and self.respond_to? :created_by=
    self.created_by = controller.trace_user_id
  end
  yield
  controller.trace(:create, self) if controller and controller.respond_to? :trace
end

#__trace_destroyObject

:nodoc:



56
57
58
59
60
61
62
63
# File 'lib/platanus/traceable.rb', line 56

def __trace_destroy # :nodoc:
  controller = ActionController::Base.current
  if controller and controller.respond_to? :trace_user_id and self.respond_to? :destroyed_by=
    self.destroyed_by = controller.trace_user_id
  end
  yield
  controller.trace(:destroy, self) if controller and controller.respond_to? :trace
end

#__trace_removeObject

:nodoc:



65
66
67
68
69
70
71
72
# File 'lib/platanus/traceable.rb', line 65

def __trace_remove # :nodoc:
  controller = ActionController::Base.current
  if controller and controller.respond_to? :trace_user_id and self.respond_to? :removed_by=
    self.removed_by = controller.trace_user_id
  end
  yield
  controller.trace(:remove, self) if controller and controller.respond_to? :trace
end

#__trace_updateObject

:nodoc:



47
48
49
50
51
52
53
54
# File 'lib/platanus/traceable.rb', line 47

def __trace_update # :nodoc:
  controller = ActionController::Base.current
  if controller and controller.respond_to? :trace_user_id and self.respond_to? :updated_by=
    self.updated_by = controller.trace_user_id
  end
  yield
  controller.trace(:update, self) if controller and controller.respond_to? :trace
end