Last active
April 27, 2017 14:36
-
-
Save ryenski/b7e08699cd07724f90fca3ba88267967 to your computer and use it in GitHub Desktop.
An ActiveSupport concern that adds commit hooks for action auditing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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