module Tracer::Changes::InstanceMethods

Private Instance Methods

changed_and_not_ignored() click to toggle source
# File lib/tracer_client/changes.rb, line 155
def changed_and_not_ignored
  ignore = self.changes_logging_options[:ignore].dup
  # remove Hash arguments and then evaluate whether the attributes (the keys of the hash) should also get pushed into the collection
  ignore.delete_if do |obj|
    obj.is_a?(Hash) && obj.each { |attr, condition| ignore << attr if condition.respond_to?(:call) && condition.call(self) }
  end
  skip = self.changes_logging_options[:skip]
  changed - ignore - skip
end
changed_notably?() click to toggle source
# File lib/tracer_client/changes.rb, line 141
def changed_notably?
  notably_changed.any?
end
changes_for_tracing() click to toggle source
# File lib/tracer_client/changes.rb, line 100
def changes_for_tracing
  changed = self.changes.delete_if do |key, value|
    !notably_changed.include?(key)
  end

  unwrap_serialized_attributes(changed) do |changed, (a, b), attr|
    if (a.key?(attr) || b.key?(attr)) && a[attr] != b[attr]
      changed[attr] = [a[attr], b[attr]]
    end
  end

  changed.as_json
end
item_before_change() click to toggle source
# File lib/tracer_client/changes.rb, line 128
def item_before_change
  previous = self.dup
  # `dup` clears timestamps so we add them back.
  all_timestamp_attributes.each do |column|
    previous[column] = send(column) if self.class.column_names.include?(column.to_s) and not try(column).nil?
  end
  previous.tap do |prev|
    prev.id = id # `dup` clears the `id` so we add that back
    changed_attributes.select { |k,v| self.class.column_names.include?(k) }.each { |attr, before| prev[attr] = before }
  end
end
log_create() click to toggle source
# File lib/tracer_client/changes.rb, line 42
def log_create
  Tracer::Client.log_changes(
      item_id:   id,
      item_type: self.class.base_class.name,
      event:     'create',
      changes:   changes_for_tracing,
  )
rescue => e
  Log.exception_with_alert(e, 'Ошибка регистрации создания', 'log_changes create',
                           item_id:   id,
                           item_type: self.class.base_class.name)
end
log_destroy() click to toggle source
# File lib/tracer_client/changes.rb, line 73
def log_destroy
  if persisted?
    Tracer::Client.log_changes(
        item_id:   id,
        item_type: self.class.base_class.name,
        event:     'destroy',
        object:    object_attrs_for_tracing(item_before_change),
    )
  end
rescue => e
  Log.exception_with_alert(e, 'Ошибка регистрации удаления', 'log_changes destroy',
                           item_id:   id,
                           item_type: self.class.base_class.name)
end
log_update() click to toggle source
# File lib/tracer_client/changes.rb, line 56
def log_update
  if changed_notably?
    Tracer::Client.log_changes(
        item_id:   id,
        item_type: self.class.base_class.name,
        event:     'update',
        object:    object_attrs_for_tracing(item_before_change),
        changes:   changes_for_tracing,
    )
  end
rescue => e
  Log.exception_with_alert(e, 'Ошибка регистрации изменения', 'log_changes update',
                           item_id:   id,
                           item_type: self.class.base_class.name)
end
notably_changed() click to toggle source
# File lib/tracer_client/changes.rb, line 146
def notably_changed
  only = self.changes_logging_options[:only].dup
  # remove Hash arguments and then evaluate whether the attributes (the keys of the hash) should also get pushed into the collection
  only.delete_if do |obj|
    obj.is_a?(Hash) && obj.each { |attr, condition| only << attr if condition.respond_to?(:call) && condition.call(self) }
  end
  only.empty? ? changed_and_not_ignored : (changed_and_not_ignored & only)
end
object_attrs_for_tracing(object) click to toggle source
# File lib/tracer_client/changes.rb, line 89
def object_attrs_for_tracing(object)
  object_attrs = object.attributes.except(*changes_logging_options[:skip]).with_indifferent_access

  unwrap_serialized_attributes(object_attrs) do |object_attrs, values, attr|
    object_attrs[attr] = values[attr] if values.key?(attr)
  end

  object_attrs.as_json
end
unwrap_serialized_attributes(attrs) { |attrs, value, attr| ... } click to toggle source

attrs должны быть либо с ключами-символами, либо HashWithIndifferentAccess

# File lib/tracer_client/changes.rb, line 116
def unwrap_serialized_attributes(attrs)
  stored_attrs = self.class.stored_attributes
  serialized = attrs.extract!(*stored_attrs.keys)

  serialized.each do |store_attr, value|
    stored_attrs[store_attr.to_sym].each do |attr|
      yield(attrs, value, attr)
    end
  end
end