Class: RailsAdmin::History

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rails_admin/extensions/history/history.rb

Constant Summary collapse

IGNORED_ATTRS =
Set[:id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]

Class Method Summary collapse

Class Method Details

.create_history_item(message, object, abstract_model, user) ⇒ Object


18
19
20
21
22
23
24
25
# File 'lib/rails_admin/extensions/history/history.rb', line 18

def create_history_item(message, object, abstract_model, user)
  create(
     message: [message].flatten.join(', '),
     item: object.id,
     table: abstract_model.to_s,
     username: user.try(:email),
   )
end

.history_for_model(abstract_model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20)) ⇒ Object


27
28
29
30
# File 'lib/rails_admin/extensions/history/history.rb', line 27

def history_for_model(abstract_model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
  history = where(table: abstract_model.to_s)
  history_for_model_or_object(history, abstract_model, query, sort, sort_reverse, all, page, per_page)
end

.history_for_model_or_object(history, _abstract_model, query, sort, sort_reverse, all, page, per_page) ⇒ Object (protected)


39
40
41
42
43
# File 'lib/rails_admin/extensions/history/history.rb', line 39

def history_for_model_or_object(history, _abstract_model, query, sort, sort_reverse, all, page, per_page)
  history = history.where('message LIKE ? OR username LIKE ?', "%#{query}%", "%#{query}%") if query
  history = history.order(sort_reverse == 'true' ? "#{sort} DESC" : sort) if sort
  all ? history : history.send(Kaminari.config.page_method_name, page.presence || '1').per(per_page)
end

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


32
33
34
35
# File 'lib/rails_admin/extensions/history/history.rb', line 32

def history_for_object(abstract_model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
  history = where(table: abstract_model.to_s, item: object.id)
  history_for_model_or_object(history, abstract_model, query, sort, sort_reverse, all, page, per_page)
end

.latestObject


14
15
16
# File 'lib/rails_admin/extensions/history/history.rb', line 14

def latest
  limit(100)
end