module AuditsHelper # Given a value, returns the string NONE if the value is blank?, # otherwise returns the given value def blank_is_none(value) value.blank? ? "NONE" : value end # Allows you to iterate over a given Audit#change_set. Given a block, each time # yield is called, it will return the attribute, the old version and the new # version as arguments. If the Audit is a message audit, it will set the new # attribute to the audit message. # # Example: # <% change_set_iter(audit.change_set) do |attr, old, new| %> #
<%=h attr %> # <% if audit.message %> # <%= new %> # <% else %> # changed from <%=h old %> to <%=h new %>. # <% end %> #
# <% end %> def change_set_iter(change_set, &block) change_set.each_pair do |attr, change| if change.is_a?(Array) old_val = blank_is_none(change.first) new_val = blank_is_none(change.last) else old_val = blank_is_none("") new_val = blank_is_none(change) end yield attr.titleize, old_val, new_val end end end