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

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

Constant Summary

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', version_class = '::Version') ⇒ AuditingAdapter



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 41

def initialize(controller, user_class = 'User', version_class = '::Version')
  fail('PaperTrail not found') unless defined?(PaperTrail)
  @controller = 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 "Please set up Papertrail's version model explicitly. Ex: config.audit_with :paper_trail, 'User', 'PaperTrail::Version'"
  end
end

Instance Method Details

#create_object(_object, _abstract_model, _user) ⇒ Object



69
70
71
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 69

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

#delete_object(_object, _model, _user) ⇒ Object



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

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

#latestObject



57
58
59
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 57

def latest
  @version_class.order('id DESC').limit(100).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



73
74
75
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 73

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)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 83

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

  model_name = model.model.name

  current_page = page.presence || '1'

  versions = version_class_for(model_name).where item_type: model_name
  versions = versions.where item_id: object.id if object
  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.total_count)
  paginated_proxies = paginated_proxies.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



77
78
79
# 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))
  listing_for_model_or_object(model, object, query, sort, sort_reverse, all, page, per_page)
end

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



65
66
67
# File 'lib/rails_admin/extensions/paper_trail/auditing_adapter.rb', line 65

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

#version_class_for(model_name) ⇒ Object (protected)



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

def version_class_for(model_name)
  klass = model_name.constantize.
          try(:version_class_name).
          try(:constantize)

  klass || @version_class
end