JP Duffy
JP Duffy

Reputation: 1368

Adding ActiveRecord validations to PaperTrail's Version model?

I'm trying to add a validation to PaperTrail::Version which will prevent sensitive data from being stored in the versions table. The idea being you'll get lots of obvious errors if you forget to sanitize your has_paper_trail call within your model.

If I add a custom validator in config/initializers/paper_trail it works ... for a while. Then PaperTrail starts acting with its default behavior and my methods are undefined.

Example Code:

PaperTrail::Rails::Engine.eager_load! 

module PaperTrail
  class Version

    # Ensure no sensitive values end up in the versions table
    validate  :prohibited_attributes

    ...

Upvotes: 2

Views: 191

Answers (1)

Jared Beck
Jared Beck

Reputation: 17528

Try a custom version class. See documentation section 6.a. Custom Version Classes.

6.a. Custom Version Classes

You can specify custom version subclasses with the :class_name option:

class PostVersion < PaperTrail::Version
  # custom behaviour, e.g:
  self.table_name = :post_versions
end

class Post < ActiveRecord::Base
  has_paper_trail :class_name => 'PostVersion'
end

Using PaperTrail::Rails::Engine.eager_load! was a good idea. Not sure why that didn't work for you. Hopefully this is a workaround.

Upvotes: 0

Related Questions