Module: AuditsHelper
- Defined in:
- lib/generators/auditing/templates/audits_helper.rb
Instance Method Summary collapse
- #description(audit) ⇒ Object
-
#get_belongs_to(audit, value) ⇒ Object
For a belongs_to association, we only store the ID of what is being refered to (:industry_id => 5).
- #get_has_many(audit, value) ⇒ Object
Instance Method Details
#description(audit) ⇒ Object
3 4 5 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 33 34 35 36 |
# File 'lib/generators/auditing/templates/audits_helper.rb', line 3 def description(audit) str = case audit.action when 'created' "Created Record #{audit.auditable_type}" when 'updated' if audit.association.blank? if audit.field_name.match(/_id$/) "Old: #{get_belongs_to(audit, audit.old_value)} <br/>" + "New: #{get_belongs_to(audit, audit.new_value)}" else "Old: #{audit.old_value} <br/>" + "New: #{audit.new_value}" end else if audit.old_value "Old: #{get_has_many(audit, audit.old_value)}<br/>"+ "New: #{get_has_many(audit, audit.new_value)}" else "New: #{get_has_many(audit, audit.new_value)}" end end when 'added' if audit.old_value "Old: #{get_has_many(audit, audit.old_value)}<br/>"+ "New: #{get_has_many(audit, audit.new_value)}" else "New: #{get_has_many(audit, audit.new_value)}" end when 'removed' "Removed #{audit.association_type}" end str.html_safe end |
#get_belongs_to(audit, value) ⇒ Object
For a belongs_to association, we only store the ID of what is being refered to (:industry_id => 5). We cannot guess what would be relevant to actually show for the value. We recommend implementing a #to_label method and/or overriding this method to display what is relevant to your application.
EG:
class Industry < ActiveRecord::Base
has_many :companies
def to_label
name
end
end
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/generators/auditing/templates/audits_helper.rb', line 63 def get_belongs_to(audit, value) return nil unless value if audit.association klass = audit.association.class.reflect_on_association(audit.field_name.gsub(/_id$/,'').to_sym).class_name.constantize else klass = audit.auditable.class.reflect_on_association(audit.field_name.gsub(/_id$/,'').to_sym).class_name.constantize end value = klass.where(:id => value).first return 'Unknown' unless value return value.to_label end |
#get_has_many(audit, value) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/generators/auditing/templates/audits_helper.rb', line 38 def get_has_many(audit, value) return nil unless value if audit.field_name.match(/_id$/) get_belongs_to(audit, value) else value end end |