Module: AuditsHelper

Defined in:
app/helpers/audits_helper.rb

Constant Summary collapse

MainObjects =
%w(Host Hostgroup User Operatingsystem Environment Puppetclass Parameter Architecture ComputeResource ConfigTemplate)

Instance Method Summary collapse

Instance Method Details

#audit_action_name(audit) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/audits_helper.rb', line 65

def audit_action_name audit
  return audit.action == 'destroy' ? 'destroyed' : "#{audit.action}d" if main_object? audit

  case audit.action
    when 'create'
      'added'
    when 'destroy'
      'removed'
    else
      'updated'
  end
end

#audit_login?(audit) ⇒ Boolean


60
61
62
63
# File 'app/helpers/audits_helper.rb', line 60

def audit_login? audit
  name = audit.audited_changes.keys[0] rescue ''
  name == 'last_login_on'
end

#audit_remote_address(audit) ⇒ Object


124
125
126
127
128
129
# File 'app/helpers/audits_helper.rb', line 124

def audit_remote_address audit
  return if audit.remote_address.empty?
   :span, :style => 'color:#999;' do
    "(" + audit.remote_address + ")"
  end
end

#audit_template?(audit) ⇒ Boolean


55
56
57
58
# File 'app/helpers/audits_helper.rb', line 55

def audit_template? audit
  audit.auditable_type == "ConfigTemplate" && audit.action == 'update' && audit.audited_changes["template"] &&
    audit.audited_changes["template"][0] != audit.audited_changes["template"][1]
end

#audit_time(audit) ⇒ Object


84
85
86
87
# File 'app/helpers/audits_helper.rb', line 84

def audit_time audit
   :span, _("%s ago") % time_ago_in_words(audit.created_at),
              { :'data-original-title' => audit.created_at.to_s(:long), :rel => 'twipsy' }
end

#audit_title(audit) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/audits_helper.rb', line 22

def audit_title audit
  type_name = audited_type audit
  if type_name == "Puppet Class"
    "#{id_to_label audit.audited_changes.keys[0], audit.audited_changes.values[0]}"
  else
    name = audit.auditable_name.blank? ? audit.revision.to_label : audit.auditable_name
    name += " / #{audit.associated_name}" if audit.associated_id and !audit.associated_name.blank?
    name
  end
rescue
  ""
end

#audit_user(audit) ⇒ Object


78
79
80
81
82
# File 'app/helpers/audits_helper.rb', line 78

def audit_user audit
  return if audit.username.nil?
   = audit.user. rescue nil # aliasing the user method sometimes yields strings
  link_to(icon_text('user', audit.username.gsub(_('User'), '')), hash_for_audits_path(:search =>  ? "user = #{login}" : "username = \"#{audit.username}\""))
end

#audited_icon(audit) ⇒ Object


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/audits_helper.rb', line 89

def audited_icon audit
  style = 'label-info'
  style = case audit.action
            when 'create'
              'label-success'
            when 'update'
              'label-info'
            when 'destroy'
              'label-important'
            else
              ''
          end if main_object? audit
  style += " label"

  type   = audited_type(audit)
  symbol = case type
             when "Host"
               'hdd'
             when "Hostgroup"
               'tasks'
             when "User"
               'user'
             else
               'cog'
           end
  (:b, icon_text(symbol, type, :class => 'icon-white'), :class => style)
end

#audited_type(audit) ⇒ Object


117
118
119
120
121
122
# File 'app/helpers/audits_helper.rb', line 117

def audited_type audit
  type_name = audit.auditable_type
  type_name = "Puppet Class" if type_name == "HostClass"
  type_name = "#{audit.associated_type || 'Global'}-#{type_name}" if type_name == "Parameter"
  type_name.underscore.titleize
end

#details(audit) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/audits_helper.rb', line 35

def details audit
  if audit.action == 'update'
    Array.wrap(audit.audited_changes).map do |name, change|
      next if change.nil? or change.to_s.empty?
      if name == 'template'
        (_("Provisioning Template content changed %s") % (link_to 'view diff', audit_path(audit))).html_safe if audit_template? audit
      elsif name == "owner_id" || name == "owner_type"
        _("Owner changed to %s") % (audit.revision.owner rescue _('N/A'))
      else
        _("%{name} changed from %{label1} to %{label2}") % { :name => name.humanize, :label1 => id_to_label(name, change[0]), :label2 => id_to_label(name, change[1]) }
      end
    end
  elsif !main_object? audit
    ["#{audit_action_name(audit).humanize} #{id_to_label audit.audited_changes.keys[0], audit.audited_changes.values[0]}
     #{audit_action_name(audit)=="removed" ? "from" : "to"} #{id_to_label audit.audited_changes.keys[1], audit.audited_changes.values[1]}"]
  else
    []
  end
end

#id_to_label(name, change) ⇒ Object

lookup the Model representing the numerical id and return its label


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/audits_helper.rb', line 6

def id_to_label name, change
  return _("N/A") if change.nil?
  case name
    when "ancestry"
      change.blank? ? "" : change.split('/').map { |i| Hostgroup.find(i).name rescue _("NA") }.join('/')
    when 'last_login_on'
      change.to_s(:short)
    when /.*_id$/
      name.humanize.constantize.find(change).to_label
    else
      change.to_s
  end.truncate(50)
rescue
  _("N/A")
end