Last active
August 29, 2015 14:13
-
-
Save scruwys/8401065d79a66af98572 to your computer and use it in GitHub Desktop.
extension of paper_trail gem to act as an audit log and a recent activity feed
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
| # app/models/concerns/acts_as_trackable.rb | |
| module Trackable | |
| extend ActiveSupport::Concern | |
| module ActiveRecord | |
| def acts_as_trackable(field_name = nil) | |
| # create a reader on the class to access the field name | |
| class << self; attr_reader :displayable; end | |
| @displayable = field_name | |
| include Trackable | |
| end | |
| end | |
| included do | |
| has_paper_trail | |
| after_create :after_trackable_create | |
| after_update :after_trackable_update | |
| after_destroy :after_trackable_destroy | |
| end | |
| private | |
| def after_trackable_create | |
| create_event_log(PaperTrail.whodunnit, 'create') | |
| end | |
| def after_trackable_update | |
| create_event_log(PaperTrail.whodunnit, 'update') | |
| end | |
| def after_trackable_destroy | |
| create_event_log(PaperTrail.whodunnit, 'delete') | |
| end | |
| def create_event_log(user, event_type) | |
| if PaperTrail.enabled? && user | |
| version = self.versions.last | |
| EventLog.create do |c| # EventLog.delay.create - if using sidekiq | |
| c.user_id = user | |
| c.event_type = event_type | |
| c.source_id = self.id | |
| c.source_type = self.class.to_s | |
| c.source_display = self.send(self.class.displayable) if self.class.displayable # for deleted instances | |
| c.version_id = version.id | |
| end | |
| end | |
| end | |
| end |
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
| # app/helpers/event_logs_helper.rb | |
| module EventLogsHelper | |
| # you could implement some helper methods | |
| # to properly display the activity based | |
| # on attributes. This could act as an activity feed. | |
| end |
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
| class CreateEventLogs < ActiveRecord::Migration | |
| def change | |
| create_table :event_logs do |t| | |
| t.integer :version_id # paper_trail gem | |
| t.string :event_type # CRUD action | |
| t.string :source_type # class name | |
| t.string :source_display # displayable field if instance is deleted | |
| t.references :user, index: true | |
| t.references :source, index: true | |
| t.timestamps | |
| end | |
| end | |
| end |
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
| # app/models/event_log.rb | |
| class EventLog < ActiveRecord::Base | |
| belongs_to :user | |
| belongs_to :source, polymorphic: true | |
| end | |
| # app/models/widget.rb | |
| class Widget < ActiveRecord::Base | |
| acts_as_trackable :name | |
| has_many :event_log, as: :source | |
| end | |
| # app/models/user.rb | |
| class User < ActiveRecord::Base | |
| acts_as_trackable :full_name # takes methods | |
| has_many :event_logs | |
| def full_name | |
| "#{self.first_name self.last_name}" | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gem 'paper_trail', '~> 3.0.6' in Gemfile...