Module: Mongoid::SimpleAudit::Helper

Defined in:
lib/mongoid/simple_audit/helper.rb

Instance Method Summary collapse

Instance Method Details

#render_audits(audited_model) ⇒ Object

Render the change log for the given audited model



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
33
34
35
36
# File 'lib/mongoid/simple_audit/helper.rb', line 8

def render_audits(audited_model)
  return '' if !audited_model.respond_to?(:audit) || audited_model.audit.nil?
  audits = (audited_model.audit.modifications).dup.sort{|a,b| b.created_at <=> a.created_at}
  res = ''
  audits.each_with_index do |audit, index|
    older_audit = audits[index + 1]
    res += (:div, :class => 'audit') do
      (:div, audit.action, :class => "action #{audit.action}") +
      (:div, audit.username, :class => "user") + 
      (:div, l(audit.created_at), :class => "timestamp") + 
      (:div, :class => 'changes') do
        changes = if older_audit.present?
          audit.delta(older_audit).sort{|x,y| audited_model.class.human_attribute_name(x.first) <=> audited_model.class.human_attribute_name(y.first)}.collect do |k, v|                
            next if k.to_s == 'created_at' || k.to_s == 'updated_at'
            "\n" + 
            audited_model.class.human_attribute_name(k) +
            ":" +
            (:span, (v.last ? v.last.to_s.force_encoding("UTF-8") : '' ), :class => 'current') +
            (:span, (v.first ? v.first.to_s.force_encoding("UTF-8") : '' ), :class => 'previous')
          end
        else
          audit.change_log.sort{|x,y| audited_model.class.human_attribute_name(x.first) <=> audited_model.class.human_attribute_name(y.first)}.reject{|k, v| v.blank?}.collect {|k, v| "\n#{audited_model.class.human_attribute_name(k)}: #{v}"}
        end
        raw changes.join
      end        
    end
  end
  raw res
end