Skip to content

Instantly share code, notes, and snippets.

@dmitry
Created May 21, 2014 22:43
Show Gist options
  • Save dmitry/b94860e44516e417f6f4 to your computer and use it in GitHub Desktop.
Save dmitry/b94860e44516e417f6f4 to your computer and use it in GitHub Desktop.
Include paper_trail for all the models.
ActiveRecord::Base.module_eval do
class << self
def inherited_with_paper_trail(subclass)
skip_models = %w(schema_migrations versions)
inherited_without_paper_trail(subclass)
table_name = subclass.table_name
if !skip_models.include?(table_name) && table_name.present?
subclass.send(:has_paper_trail)
end
end
alias_method_chain :inherited, :paper_trail
end
end
@Workoidz
Copy link

Workoidz commented Dec 5, 2019

what is skip_models is for ..??

@dmitry
Copy link
Author

dmitry commented Dec 5, 2019

@Workoidz if you are using rails >= 5.2 then use the following trick:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  def self.inherited(subclass)
    super
    subclass.send(:has_paper_trail)
  end
end

And skip_models is used to ignore migrations and versions it self (as it doesn't required to make a versioning for those tables, and BTW versions will lead into a SystemStackError (stack level too deep)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment