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

Inherits:
Object
  • Object
show all
Includes:
Config::Configurable
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_USER_CLASS_NOT_SET =
<<~ERROR
  Please set up PaperTrail's user class explicitly.

      config.audit_with :paper_trail do
        user_class { User }
      end
ERROR
E_VERSION_MODEL_NOT_SET =
<<~ERROR
  Please set up PaperTrail's version model explicitly.

      config.audit_with :paper_trail do
        version_class { PaperTrail::Version }
      end

  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`.
ERROR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config::Configurable

#has_option?, included, #register_deprecated_instance_option, #register_instance_option

Constructor Details

#initialize(controller, user_class_name = nil, version_class_name = nil, &block) ⇒ AuditingAdapter

Returns a new instance of AuditingAdapter.



81
82
83
84
85
86
87
88
89
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 81

def initialize(controller, user_class_name = nil, version_class_name = nil, &block)
  @controller = controller
  @controller&.send(:set_paper_trail_whodunnit)

  user_class { user_class_name.to_s.constantize } if user_class_name
  version_class { version_class_name.to_s.constantize } if version_class_name

  instance_eval(&block) if block
end

Class Method Details

.setupObject



75
76
77
78
79
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 75

def self.setup
  raise 'PaperTrail not found' unless defined?(::PaperTrail)

  RailsAdmin::Extensions::ControllerExtension.include ControllerExtension
end

Instance Method Details

#create_object(_object, _abstract_model, _user) ⇒ Object



121
122
123
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 121

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

#delete_object(_object, _model, _user) ⇒ Object



113
114
115
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 113

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

#latest(count = 100) ⇒ Object



107
108
109
110
111
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 107

def latest(count = 100)
  version_class.
    order(sort_by).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



125
126
127
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 125

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



129
130
131
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 129

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



117
118
119
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 117

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