Joe Stepowski
Joe Stepowski

Reputation: 666

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!

Upvotes: 5

Views: 863

Answers (3)

Mr. Mi
Mr. Mi

Reputation: 31

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

Upvotes: 3

David Gleba
David Gleba

Reputation: 537

I did the following in the rails_admin initializer and it worked.

config.model 'DcDiscipline' do
  edit do
    exclude_fields :versions 
  end     
end

Upvotes: 0

coorasse
coorasse

Reputation: 5528

In your rails_admin configuratoin you can specify it:

config.actions do
  history_index do
    except %w(ModelA ModelB)
  end
end

or the other way round:

config.actions do
  history_index do
    only %w(ModelA ModelB)
  end
end

Upvotes: 0

Related Questions