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_model_or_object(model, object, query, sort, sort_reverse, all, page, per_page) ⇒ Object (protected)

  • model - a RailsAdmin::AbstractModel

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 109

def listing_for_model_or_object(model, object, query, sort, sort_reverse, all, page, per_page)
  if sort.present?
    sort = COLUMN_MAPPING[sort.to_sym]
  else
    sort = :created_at
    sort_reverse = 'true'
  end

  current_page = page.presence || '1'

  versions = object.nil? ? versions_for_model(model) : object.versions
  versions = versions.where('event LIKE ?', "%#{query}%") if query.present?
  versions = versions.order(sort_reverse == 'true' ? "#{sort} DESC" : sort)
  versions = all ? versions : versions.send(Kaminari.config.page_method_name, current_page).per(per_page)
  paginated_proxies = Kaminari.paginate_array([], total_count: versions.try(:total_count) || versions.count)
  paginated_proxies = paginated_proxies.send(
    paginated_proxies.respond_to?(Kaminari.config.page_method_name) ? Kaminari.config.page_method_name : :page,
    current_page,
  ).per(per_page)
  versions.each do |version|
    paginated_proxies << VersionProxy.new(version, @user_class)
  end
  paginated_proxies
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

#version_class_for(model) ⇒ Object (protected)

PT can be configured to use custom version classes

has_paper_trail class_name: 'MyVersion'

154
155
156
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 154

def version_class_for(model)
  model.paper_trail_options[:class_name].try(:constantize) || @version_class
end

#versions_for_model(model) ⇒ Object (protected)


134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 134

def versions_for_model(model)
  model_name = model.model.name
  base_class_name = model.model.base_class.name

  options =
    if base_class_name == model_name
      {item_type: model_name}
    else
      {item_type: base_class_name, item_id: model.model.all}
    end

  version_class_for(model.model).where(options)
end