0

I am adding the Audited-ActiveRecord gem to my Rails 4 application. In order to implement the gem I need to add an audited method call to each model.

Is it possible to include this method in a single location instead of having to add it to each of my ActiveRecord models?

https://rubygems.org/gems/audited-activerecord

The audited gem allows me to do paste audited into each model however as I'm trying to add auditing without having to paste audited into 50+ existing models as well as future models.

For example:

class ModelOne < ActiveRecord::Base
  audited
  ...
end

class ModelTwo < ActiveRecord::Base
  audited
  ...
end

class ModelN < ActiveRecord::Base
  audited
  ...
end
Charles Green
  • 413
  • 3
  • 15
  • Thanks everyone for your help.I ended up just adding the `audited` method manually to each ActiveRecord model as shown in the question. – Charles Green Sep 14 '16 at 08:38

2 Answers2

2

You can call audited on ActiveRecord::Base using an initializer. This will include it into all your ActiveRecord models.

# config/initializers/audited.rb
module Audited
  extend ActiveSupport::Concern

  included do
    audited
  end
end

ActiveRecord::Base.include Audited
fylooi
  • 3,840
  • 14
  • 24
0

You can use concerns to do that, however you must include this module from concerns in each model files. For example:

class SomethingModel
  include Auditable
  #...
end

class OtherModel
  include Auditable
  #...
end


# app/models/concerns/auditable.rb
module Auditable
  extend ActiveSupport::Concern
  #Your methods from Audited-Active-Record
end
Bartłomiej Gładys
  • 4,525
  • 1
  • 14
  • 24
  • I tried this method a few different ways however the result throws an exception. This answer did help me find a different answer that lead me to my final approach. http://stackoverflow.com/questions/20824470/adding-scope-to-activerecord-causes-hierarchy-error Thanks again. – Charles Green Sep 14 '16 at 08:48