Nitin Kumar
Nitin Kumar

Reputation: 184

Global for all models with ignore options model wise

I am using paper_trail (10.0.1). I have defined paper_trail to be available to all my models in application_record as

 has_paper_trail(
   on: %i[create, update],
   ignore: %i[created_at, updated_at],
   unless: proc { |obj| CONSTANT.include?(obj.class.name) }
)

I would like to define option for ignore common for few models and specific for few other models. For example say I have 10 models in which I have 5 models where [created_at, updated_at] would be ignored. And other 5 models have different attributes to be ignore.

Is there any possibilities to define DEFAULT_IGNORE options in application_record and can we have constant specified model wise to call here in ignore to be used only for that model.

Upvotes: 0

Views: 202

Answers (1)

Jared Beck
Jared Beck

Reputation: 17528

Is there any possibilities to define DEFAULT_IGNORE options in application_record and can we [override that] constant [in the child classes]?

Yes, see: In Ruby, is there a way to 'override' a constant in a subclass so that inherited methods use the new constant instead of the old?

But, there are many other options here worth considering. Here's one design I like:

class ApplicationRecord
  def self.paper_trail_ignore
    %i[created_at updated_at]
  end
end

class Banana < ApplicationRecord
  def self.paper_trail_ignore
    %i[other_attribute]
  end
  has_paper_trail(ignore: paper_trail_ignore)
end

Edit: updated example to avoid calling has_paper_trail twice

Upvotes: 1

Related Questions