Created
March 22, 2023 02:47
-
-
Save sjha4/164a8dd3e29b795b0318f39ba6772d3e to your computer and use it in GitHub Desktop.
Override Default Audits in your model
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
class MyModel < ApplicationRecord | |
# Custom audit function to create audit records | |
def create_audit_record(action) | |
Audit.create!( | |
auditable_id: self.id, | |
auditable_type: self.class.name, | |
action: action, | |
user_id: current_user.id # replace with your authentication method | |
) | |
end | |
# Override create method to create audit record | |
def create | |
super | |
create_audit_record('create') | |
end | |
# Override update method to create audit record | |
def update(attributes) | |
super | |
create_audit_record('update') | |
end | |
# Override destroy method to create audit record | |
def destroy | |
create_audit_record('destroy') | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment