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
}

Instance Method Summary collapse

Constructor Details

#initialize(controller, user_class = User) ⇒ AuditingAdapter

Returns a new instance of AuditingAdapter.



41
42
43
44
45
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 41

def initialize(controller, user_class = User)
  raise "PaperTrail not found" unless defined?(PaperTrail)
  @controller = controller
  @user_class = user_class.to_s.constantize
end

Instance Method Details

#create_object(object, abstract_model, user) ⇒ Object



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

def create_object(object, abstract_model, user)
  # do nothing
end

#delete_object(object, model, user) ⇒ Object



51
52
53
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 51

def delete_object(object, model, user)
  # do nothing
end

#latestObject



47
48
49
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 47

def latest
  ::Version.limit(100).map{|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



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

def listing_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
  if sort.present?
    sort = COLUMN_MAPPING[sort.to_sym]
  else
    sort = :created_at
    sort_reverse = "true"
  end
  versions = ::Version.where :item_type => model.model.name
  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, page.presence || "1").per(per_page)
  versions.map{|version| VersionProxy.new(version, @user_class)}
end

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



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

def listing_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
  if sort.present?
    sort = COLUMN_MAPPING[sort.to_sym]
  else
    sort = :created_at
    sort_reverse = "true"
  end
  versions = ::Version.where :item_type => model.model.name, :item_id => object.id
  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, page.presence || "1").per(per_page)
  versions.map{|version| VersionProxy.new(version, @user_class)}
end

#update_object(object, model, user, changes) ⇒ Object



55
56
57
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 55

def update_object(object, model, user, changes)
  # do nothing
end