Created
April 9, 2018 13:28
-
-
Save werdlerk/3cf429b7e2495c125749ce5260dffdf5 to your computer and use it in GitHub Desktop.
Audited 3.0 gem : add extra user information to Audit
This file contains 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/controllers/application_controller.rb | |
class ApplicationController < ActionController::Base | |
# (...) | |
def save_admin_user_current_user | |
Thread.current[:audited_admin_user_id] = current_admin_user&.id | |
current_user | |
end | |
end |
This file contains 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/auditable.rb | |
module Auditable | |
extend ActiveSupport::Concern | |
included do | |
audited | |
end | |
def after_audit | |
admin_user_id = Thread.current[:audited_admin_user_id] | |
return unless admin_user_id.present? | |
audits.last.tap { |audit| audit.admin_user_id = admin_user_id}.save! | |
end | |
end |
This file contains 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
# config/initializers/audited.rb | |
Audited.current_user_method = :save_admin_user_current_user |
This file contains 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/your_model.rb | |
class YourModel < ActiveRecord::Base | |
include Auditable | |
# (...) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment