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



40
41
42
43
44
45
46
47
# File 'lib/rails_admin/extensions/history/history.rb', line 40

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

.create_update_history(model, object, associations_before, associations_after, modified_associations, old_object, user) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_admin/extensions/history/history.rb', line 12

def self.create_update_history(model, object, associations_before, associations_after, modified_associations, old_object, user)
  messages = []
  changed_property_list = []
  properties = model.properties.reject{|p| IGNORED_ATTRS.include?(p[:name])}

  properties.each do |p|
    property_name = p[:name].to_param
    changed_property_list << property_name if old_object.safe_send(property_name) != object.safe_send(property_name)
  end

  model.associations.each do |t|
    assoc = changed_property_list.index(t[:foreign_key].to_param)
    changed_property_list[assoc] = "associated #{t[:pretty_name]}" if assoc
  end

  associations_after.each do |key, current|
    removed_ids = (associations_before[key] - current).map{|m| '#' + m.to_s}
    added_ids = (current - associations_before[key]).map{|m| '#' + m.to_s}
    messages << "Removed #{key.to_s.capitalize} #{removed_ids.join(', ')} associations" if removed_ids.any?
    messages << "Added #{key.to_s.capitalize} #{added_ids.join(', ')} associations" if added_ids.any?
  end

  modified_associations.uniq.each { |t| changed_property_list << "associated #{t}" }

  messages << "Changed #{changed_property_list.join(", ")}" unless changed_property_list.empty?
  create_history_item(messages, object, model, user) unless messages.empty?
end

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/rails_admin/extensions/history/history.rb', line 49

def self.history_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
  history = where(:table => model.pretty_name)
  history = history.where("message LIKE ? OR username LIKE ?", "%#{query}%", "%#{query}%") if query
  if sort
    history = history.order(sort_reverse == "true" ? "#{sort} DESC" : sort)
  else
    history = history.order('created_at DESC')
  end
  all ? history : history.page(page.presence || "1").per(per_page)
end

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/rails_admin/extensions/history/history.rb', line 60

def self.history_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
  history = where(:table => model.pretty_name, :item => object.id)
  history = history.where("message LIKE ? OR username LIKE ?", "%#{query}%", "%#{query}%") if query
  if sort
    history = history.order(sort_reverse == "true" ? "#{sort} DESC" : sort)
  else
    history = history.order('created_at DESC')
  end
  all ? history : history.page(page.presence || "1").per(per_page)
end

.latestObject



8
9
10
# File 'lib/rails_admin/extensions/history/history.rb', line 8

def self.latest
  self.limit(100)
end