Module: AuditedViewsHelper

Defined in:
app/helpers/audited_views_helper.rb

Instance Method Summary collapse

Instance Method Details



21
22
23
24
25
26
27
28
# File 'app/helpers/audited_views_helper.rb', line 21

def audit_link(model, text = nil)
  text ||= I18n.t("audits.link_text")
  unless params[:show_log]
    link_to "#{request.path}?show_log=true#audits", class: 'btn btn-default pull-right' do
      text
    end
  end
end

#audit_object_info(audit) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/audited_views_helper.rb', line 30

def audit_object_info(audit)
  classe = audit.auditable_type.constantize
  (:span, title: "Id #{audit.auditable_id}") do
    begin
      begin
        classe.find(audit.auditable_id).to_s
      rescue
        if classe.respond_to?(:with_deleted)
          classe.with_deleted.find(audit.auditable_id).to_s
        else
          raise
        end
      end
    rescue ActiveRecord::RecordNotFound => e
      I18n.t(:registro_excluido, scope: 'audits')
    end
  end
end

#audit_record_human_field(audit_record, field) ⇒ Object



105
106
107
108
109
110
111
112
# File 'app/helpers/audited_views_helper.rb', line 105

def audit_record_human_field(audit_record, field)
  # The most of reference field locales are defined only with the association name (not field name)
  normalized_field = field.gsub(/_id$/, "")
  audit_record
    .auditable_type
    .constantize
    .human_attribute_name(normalized_field)
end

#audited_controls(model) ⇒ Object



3
4
5
# File 'app/helpers/audited_views_helper.rb', line 3

def audited_controls(model)
  render "controls", model: model
end

#audits_for(models) ⇒ Object

Retorna audits para o model ou para todos os models caso o parĂ¢metro for uma relation ou array Retorna audits para todos os associados aos models



9
10
11
12
13
14
15
16
17
18
19
# File 'app/helpers/audited_views_helper.rb', line 9

def audits_for(models)
  models = [models] unless models.kind_of?(Array) || models.kind_of?(ActiveRecord::Relation)
  audits = []
  models.each do |model|
    audits += model.audits#.reject {|a| a.action =~ /create/ }
    audits += model.associated_audits if model.respond_to?(:associated_audits)
  end
  audits.sort do |a,b|
    b.created_at <=> a.created_at
  end
end

#created_by(model) ⇒ Object



89
90
91
92
93
94
# File 'app/helpers/audited_views_helper.rb', line 89

def created_by(model)
  raw [nome_usuario(get_model_creator(model)), ltime(model.created_at)]
    .compact
    .delete_if(&:empty?)
    .join(" - ")
end

#created_by_info(model) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/audited_views_helper.rb', line 49

def created_by_info(model)
  html = []

  html << t("audits.created_at")
  html << ltime(model.created_at)
  if (nome = get_model_creator(model))
    html << t("audits.created_by")
    html << nome
  end

  raw html.join(" ")
end

#format_audited_value(value) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/helpers/audited_views_helper.rb', line 70

def format_audited_value(value)
  case value
  when true, false
    t("audits.#{value}_word")
  when Date
    ldate value
  when NilClass
    "-"
  when String
    sanitize(value)
  else
    value
  end
end

#get_model_creator(model) ⇒ Object



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

def get_model_creator(model)
  model.audits.where(action: 'create').first.try(:user)
end

#ldate(date) ⇒ Object



62
63
64
# File 'app/helpers/audited_views_helper.rb', line 62

def ldate(date)
  I18n.l(date, format: :data) if date
end

#ltime(time) ⇒ Object



66
67
68
# File 'app/helpers/audited_views_helper.rb', line 66

def ltime(time)
  I18n.l(time, format: :short) if time
end

#panel_class_for_audit_action(action) ⇒ Object



96
97
98
99
100
101
102
103
# File 'app/helpers/audited_views_helper.rb', line 96

def panel_class_for_audit_action(action)
  case action
  when "create"  then "panel-success"
  when "update"  then "panel-info"
  when "destroy" then "panel-danger"
  else                "panel-default"
  end
end