ruby-on-railsrubyactiveadminransackpaper-trail-gem

Paper_trail gem stopped working when upgraded ransack to 4.0.0


In my rails 7.0.4 app I use:

I have the page for filtering and sorting history of changes to the data:

ActiveAdmin.register PaperTrail::Version do
  menu label: 'History'
  actions :index

  index title: 'History' do
    id_column
    column :item
    column :event
    column 'Author' do |version|
      User.where(id: version.whodunnit.to_i).first
    end
    column 'Changes', :object_changes
    column :created_at
  end

  filter :item_type
  filter :item_id, as: :numeric
  filter :created_at, input_html: {autocomplete: :off}
  filter :whodunnit, label: 'Author', as: :numeric
  filter :object_changes
end

Before I upgraded to activeadmin 3.0.0 and ransack 4.0.0, everything worked fine. After upgrade, I am getting this error on every page: "Ransack needs ... attributes explicitly allowlisted as ...". This was easily fixed for all pages except the history page, where the error states:

Ransack needs PaperTrail::Version attributes explicitly allowlisted as searchable. Define a ransackable_attributes class method in your PaperTrail::Version model, watching out for items you DON'T want searchable (for example, encrypted_password, password_reset_token, owner or other sensitive information). You can use the following as a base:

class PaperTrail::Version < ApplicationRecord

  # ...

  def self.ransackable_attributes(auth_object = nil)
    ["created_at", "event", "id", "item_id", "item_type", "object", "object_changes", "whodunnit"]
  end

  # ...

end

My config/initializers/paper_trail.rb is not much different and I think I added the ransackable_attributes method correctly:

module PaperTrail
  class Version < ActiveRecord::Base

    def self.ransackable_attributes(auth_object = nil)
      %w[created_at event id item_id item_type object object_changes whodunnit]
    end

    def user
      User.where(id: self.whodunnit.to_i).first
    end
  end
end

PaperTrail::Model::ClassMethods.module_eval do
  alias_method :old_has_paper_trail, :has_paper_trail
  def has_paper_trail(options = {})
    options[:ignore] ||= []
    options[:ignore] += [:created_at, :updated_at, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :sign_in_count]
    old_has_paper_trail(options)
  end
end

But the error stays the same. How to fix it?


Solution

  • A littly bit silly, but restarting the server solved the issue.

    Since I had PaperTrail::Version as an initializer, the change didn't take effect without server restart. Changes to other models took effect even without restart.