Module: TrackerJacker::Trackable

Defined in:
lib/tracker_jacker/trackable.rb

Defined Under Namespace

Modules: ClassMethods Classes: AttributeTrackingCallback, EventTrackingCallback

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/tracker_jacker/trackable.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
  base.class_eval do
    after_save :run_track_event_callbacks
    after_save :run_track_attribute_callbacks
  end
end

Instance Method Details

#ignore_tracking!Object



81
82
83
# File 'lib/tracker_jacker/trackable.rb', line 81

def ignore_tracking!
  @ignore_tracking = true
end

#run_track_attribute_callbacksObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tracker_jacker/trackable.rb', line 43

def run_track_attribute_callbacks
  return if @ignore_tracking

  object = self
  object.class.analytic_attribute_tracking_callbacks.values.each do |event_callback|
    if object.saved_change_to_attribute?(event_callback.attribute)
      owner = object.public_send(event_callback.owner_method)
      TrackerJacker::TrackableEvent.create(
        category: event_callback.category,
        owner: owner,
        trackable: object,
        event: event_callback.attribute.to_s + "_changed",
        old_value: attribute_before_last_save(event_callback.attribute),
        new_value: object.public_send(event_callback.attribute)
      )
    end
  end
  true
end

#run_track_event_callbacksObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tracker_jacker/trackable.rb', line 63

def run_track_event_callbacks
  return if @ignore_tracking

  object = self
  object.class.analytic_event_tracking_callbacks.values.each do |event_callback|
    if event_callback.if_proc.call(object)
      owner = object.public_send(event_callback.owner_method)
      TrackerJacker::TrackableEvent.create(
        category: event_callback.category,
        owner: owner,
        trackable: object,
        event: event_callback.event
      )
    end
  end
  true
end