Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Save tenant in Audited (formerly acts_as_audited)

I have Audited (formerly acts_as_audited) setup and working. The user_id is successfully saved in the audit table but I can't figure out an efficient way to save the tenant_id (I have multitenancy setup with scopes). I have tried using the Associated Audits technique described in the README but that doesn't work for me.

My current solution is to use the after_audit callback in every model (can be implemented with Rails concerns) to get the last audit and save the tenant_id:

def after_audit
  audit = Audit.last
  audit.tenant_id = self.tenant_id
  audit.save!
end

Whilst this works it seems like it would be inefficient to have to query for the audit again and then update it. It would make more sense to me to add the tenant_id to the audit before it saves but I can't figure out how to do this. Is it possible to add the tenant_id to the audit before saving? If yes, then how?

EDIT:

I've also tried including my default tenant scope in my Audit model but it does not 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 however my current thoughts would be to use:

#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 last audit for the current model. This way we are only dealing with the current model, there is no chance of adding the audit to another business (as far as I can tell). I would add this code into a concern to keep it DRY.

I still can't get normal Rails callbacks to work within the Audit model. The only other way I see at the moment is to fork and modified the gem source code.

like image 903
Marklar Avatar asked Nov 26 '14 09:11

Marklar


1 Answers

I was tasked with implementing Auditing, and also adding a reference to an Org. The migration adds this line:

t.references :org, type: :uuid, index: true, null: true

To save an org_id, I ended up writing an initializer - audited.rb. That file looks like this:

Rails.configuration.after_initialize do
  Audited.audit_class.class_eval do
    belongs_to :org, optional: true

    default_scope MyAppContext.context_scope

    before_create :ensure_org

    private

    def ensure_org
      return unless auditable.respond_to? :org_id

      self.org_id = auditable.org_id
    end
  end
end

Hope this helps!

like image 60
Cannon Collins Avatar answered Nov 15 '22 05:11

Cannon Collins