Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude PaperTrail versions from views in RailsAdmin

I'm using RailsAdmin v0.6.8 with PaperTrail for versioning.

The list, show, create and edit views for each of my has_paper_trail Models includes the versions attribute. In fact the create and edit views allow the addition/removal of versions, which doesn't really make sense to me. Other than using exclude_fields :versions for each view on each Model, is there a global way to do this?

Thanks!

like image 295
Joe Stepowski Avatar asked Oct 20 '22 06:10

Joe Stepowski


1 Answers

Method 1

If all models are inherited an abstract class(for example: "ApplicationRecord")
you can create a new file(for example: app/models/concerns/exclude_versions.rb):

module ExcludeVersions
  extend ActiveSupport::Concern

  included do
    rails_admin do
      configure :versions do
        hide
      end
    end
  end

end

and edit the abstract class like:

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

  def self.inherited(subclass)
    subclass.include(ExcludeVersions)
    super
  end
end

Method 2

If all models are not inherited an abstract class, you can just add such code in config/initializers/rails_admin.rb

  Rails.application.eager_load! if Rails.env.development?

  ActiveRecord::Base.descendants.each do |imodel|
    ### next if ['ApplicationRecord'].include?(imodel.name) ### if all models are inherited an abstract class, please uncomment this line, or some strange error will happen
    config.model "#{imodel.name}" do
      configure :versions do
        hide
      end
    end
  end

Reference: https://github.com/sferik/rails_admin/wiki/Models#configuring-models-all-at-once

like image 147
Mr. Mi Avatar answered Oct 22 '22 00:10

Mr. Mi