Class: RailsAdmin::History

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/rails_admin/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



34
35
36
37
38
39
40
41
# File 'app/models/rails_admin/history.rb', line 34

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/rails_admin/history.rb', line 6

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[:child_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



43
44
45
46
47
48
49
50
51
52
# File 'app/models/rails_admin/history.rb', line 43

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



54
55
56
57
58
59
60
61
62
63
# File 'app/models/rails_admin/history.rb', line 54

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