Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

different audit table per model? #471

Open
francescob opened this issue Oct 25, 2018 · 8 comments · May be fixed by #641
Open

different audit table per model? #471

francescob opened this issue Oct 25, 2018 · 8 comments · May be fixed by #641

Comments

@francescob
Copy link

is it possibile to have audits saved to different tables, for example <model_name>_audits ?
I've managed to save to a different table using the initializer to define a custom audit model, but that apply to all my models. Any idea?

@Marri
Copy link

Marri commented Jan 23, 2019

I too am interested in this question! Code as it is now looks like this isn't supported, but I'd love to see the option in future.

@francescob
Copy link
Author

francescob commented Mar 20, 2019

@Marri I think I found a quick and easy solution:

in the initializer I've defined a custom model:

Audited.config do |config|
  config.audit_class = Audit
end

the Audit model is:

class Audit < Audited::Audit self.table_name= 'audits' end

then in the model for which I want to audit to a different table:

before_save :set_audit_table

  def set_audit_table
    ::Audit.table_name = "whateveriwant_audits"
  end

@yanchengv
Copy link

yanchengv commented Sep 4, 2019

@francescob This way is work for me ! Thanks

But I have a little trouble. if executed this callbackbefore_save :set_audit_table then self.table_name will become self.table_name= 'whateveriwant_audits', other modle records will saved to whateveriwant_audits table not audits.

So I have to add:

after_save :init_audit_table
def init_audit_table
    ::Audit.table_name = "audits"
end

But before_save to after_save has a time interval.Maybe other modle records still use whateveriwant_audits table not audits!

Have you ever had this situations?

@timbielawski
Copy link

timbielawski commented Oct 24, 2019

I provided a clean up method in the Audit class

after_save :clean_up

def clean_up
::Audit.table_name = 'audits'
end

do you see any problems with this?

thanks!

@oldigor
Copy link

oldigor commented Jan 21, 2020

I wish that could be configurable too.

@jeromedalbert
Copy link

jeromedalbert commented May 8, 2020

The workarounds above don't seem thread-safe since they are modifying a global Audit.table_name variable. So if you're using multi-threaded gems like Puma or Sidekiq, there will likely be bugs.

@JoeWoodward
Copy link

This appears to be an easy fix from the code. Instead of calling Audited.audit_class, a class method should be defined when you include the module which can then be overridden through the options

e.g. Could instead of calling Audited.audit_class just call audit_class, then allow audit_class_name in the options and define a method
https://github.com/collectiveidea/audited/blob/456204de3b16d57a0df4c497615c2cb5509c508b/lib/audited/auditor.rb

def audited(options = {})
  define_method :audit_class do
    options[:audit_class_name]&.safe_constantize || Audit.audit_class
  end
  ...

  has_many :audits, -> { order(version: :asc) }, as: :auditable, class_name: audit_class.name, inverse_of: :auditable

Then when you define your model e.g. User

class User < AR::Base
  audited audit_class_name: 'UserAudit'
end

Associated audits don't even need to use the same class. There may be some other issues to solve around the audited_classes and other helpers. Not really sure what they are used for? I think overall this gem is not super flexible, clearly designed for a specific use case where you only need to audit a single model. I think most of the time you could create this functionality really easily without a gem/dependency tbh.

@kuldeepaggarwal
Copy link

kuldeepaggarwal commented Nov 25, 2022

Are we accepting feature changes? if yes, I will be happy to raise a PR to address this issue.

@kuldeepaggarwal kuldeepaggarwal linked a pull request Nov 30, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants