Rails - save tenant to Audited log (formerly act_as_audited)
I have checked (previously act_as_audited) setup and work. The User_id is successfully saved in the audit table, but I cannot find an efficient way to store the tenant_id (I have a scoped layering setup). I tried using the Associated Audits method described in the README, but that doesn't work for me.
My current solution is to use the after_audit callback in each model (can be implemented with Rails issues) to get the latest audit and store the tenant_id:
def after_audit
audit = Audit.last
audit.tenant_id = self.tenant_id
audit.save!
end
While this works, it seems like it would be inefficient to query the audit again and then update it. It would make more sense for me to add the tenant_id to the audit before it saves, but I can't figure out how. Can I add tenant_id to audit before saving? If so, how?
EDIT:
I also tried including my default lease scope in my audit model, but it doesn't seem to be called:
audit.rb
class Audit < ActiveRecord::Base
default_scope { where(tenant_id: Tenant.current_id) }
application_controller.rb
class ApplicationController < ActionController::Base
around_action :scope_current_tenant
def scope_current_tenant
Tenant.current_id = current_tenant.id
yield
ensure
Tenant.current_id = nil
end
EDIT: 2/1/16
I still haven't implemented a solution to this, but my current thoughts would be used:
#model_name.rb
def after_audit
audit = self.audits.last
audit.business_id = self.business_id
audit.save!
end
In this code, we get the latest audit for the current model. So we only deal with the current model, there is no way to add audit to another business (as far as I can tell). I would add this code to the concern to be DRY.
I still cannot get the normal Rails callbacks to work in the audit model. The only other way I can see at the moment is to develop a fork and change the gem's source code.
source to share
No one has answered this question yet
Check out similar questions: