Skip to content

Instantly share code, notes, and snippets.

@ryenski
Last active April 27, 2017 14:36
Show Gist options
  • Save ryenski/b7e08699cd07724f90fca3ba88267967 to your computer and use it in GitHub Desktop.
Save ryenski/b7e08699cd07724f90fca3ba88267967 to your computer and use it in GitHub Desktop.
An ActiveSupport concern that adds commit hooks for action auditing.
module Trackable
extend ActiveSupport::Concern
included do
has_many :activities, -> {order("created_at DESC")}, as: :trackable, dependent: :nullify
after_commit :track_activity
before_destroy :track_destroy
end
def track_destroy
link_to_contact = self.is_a?(Contact) ? self : self.try(:contact)
ActivityCreator.new('destroy', self, link_to_contact).call
end
def track_activity
action = previous_changes.include?('id') ? 'create' : 'update'
link_to_contact = self.is_a?(Contact) ? self : self.try(:contact)
ActivityCreator.new(action, self, link_to_contact).call
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment