Class: RailsAdmin::Extensions::PaperTrail::AuditingAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin/extensions/paper_trail/auditing_adapter.rb

Constant Summary collapse

COLUMN_MAPPING =
{
  table: :item_type,
  username: :whodunnit,
  item: :item_id,
  created_at: :created_at,
  message: :event,
}.freeze
E_VERSION_MODEL_NOT_SET =
<<-EOS.strip_heredoc.freeze
  Please set up PaperTrail's version model explicitly.

      config.audit_with :paper_trail, 'User', 'PaperTrail::Version'

  If you have configured a model to use a custom version class
  (https://github.com/paper-trail-gem/paper_trail#6a-custom-version-classes)
  that configuration will take precedence over what you specify in
  `audit_with`.
EOS

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, user_class = 'User', version_class = '::Version') ⇒ AuditingAdapter

Returns a new instance of AuditingAdapter.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 64

def initialize(controller, user_class = 'User', version_class = '::Version')
  @controller = controller
  @controller.send(:set_paper_trail_whodunnit) if @controller
  begin
    @user_class = user_class.to_s.constantize
  rescue NameError
    raise "Please set up Papertrail's user model explicitly. Ex: config.audit_with :paper_trail, 'User'"
  end

  begin
    @version_class = version_class.to_s.constantize
  rescue NameError
    raise E_VERSION_MODEL_NOT_SET
  end
end

Class Method Details

.setupObject



59
60
61
62
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 59

def self.setup
  raise('PaperTrail not found') unless defined?(::PaperTrail)
  RailsAdmin::Extensions::ControllerExtension.send(:include, ControllerExtension)
end

Instance Method Details

#create_object(_object, _abstract_model, _user) ⇒ Object



94
95
96
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 94

def create_object(_object, _abstract_model, _user)
  # do nothing
end

#delete_object(_object, _model, _user) ⇒ Object



86
87
88
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 86

def delete_object(_object, _model, _user)
  # do nothing
end

#latest(count = 100) ⇒ Object



80
81
82
83
84
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 80

def latest(count = 100)
  @version_class.
    order(id: :desc).includes(:item).limit(count).
    collect { |version| VersionProxy.new(version, @user_class) }
end

#listing_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20)) ⇒ Object



98
99
100
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 98

def listing_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
  listing_for_model_or_object(model, nil, query, sort, sort_reverse, all, page, per_page)
end

#listing_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20)) ⇒ Object



102
103
104
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 102

def listing_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
  listing_for_model_or_object(model, object, query, sort, sort_reverse, all, page, per_page)
end

#update_object(_object, _model, _user, _changes) ⇒ Object



90
91
92
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 90

def update_object(_object, _model, _user, _changes)
  # do nothing
end