module Blamescope::Trackable

Public Class Methods

send_event(model, method_name) click to toggle source

Send event for model @param model [Object] current object @param method_name [String|Symbol] method invoked

@return [type] [description]

# File lib/blamescope/trackable.rb, line 15
def self.send_event(model, method_name)
  class_name = model.class == Class ? model.name : model.class.name
    attrs = {}
    attrs[:id] = model.id if model.respond_to? :id
    if model.respond_to? :name
      attrs[:name] = model.name
    elsif model.respond_to? :title
      attrs[:title] = model.title
    end
    Blamescope.emit(model: class_name, action: method_name, attrs: attrs)
end

Public Instance Methods

blame_activerecord() click to toggle source

Track ActiveRecord model

# File lib/blamescope/trackable.rb, line 52
def blame_activerecord
  after_create do
    Blamescope::Trackable.send_event(self, :create)
  end
  after_update do
    Blamescope::Trackable.send_event(self, :update)
  end
  after_destroy do
    Blamescope::Trackable.send_event(self, :delete)
  end
end
blame_method(method_name) click to toggle source

Track method @param name [String|Symbol] method name

# File lib/blamescope/trackable.rb, line 31
def blame_method method_name
  alias_method "original_#{method_name}", method_name
  define_method method_name do |*args, &block|
    Blamescope::Trackable.send_event(self, method_name)
    send("original_#{method_name}", *args, &block)
  end
end
blame_methods(*names) click to toggle source

Track multiple methods @param *names [String|Symbol] method name

# File lib/blamescope/trackable.rb, line 43
def blame_methods *names
  names.each do |name|
    blame_method(name)
  end
end