Module AuditsHelper
In: app/helpers/audits_helper.rb
File:audits_helper.rb
(C):Hipposoft 2008, 2009
Purpose:Support functions for views related to audit data. See controllers/audits_controller.rb for more.

          20-Jan-2008 (ADH): Created.

Methods

Public Instance methods

List view helper - change details

[Source]

     # File app/helpers/audits_helper.rb, line 58
 58:   def audithelp_changes( record )
 59:     type    = record.auditable_type
 60:     changes = record.changes
 61: 
 62:     if changes.nil? or changes.empty?
 63:       if ( type == 'Timesheet' )
 64:         return 'Added or removed rows, or edited hours within rows'
 65:       else
 66:         return 'No other details available'
 67:       end
 68:     end
 69: 
 70:     output  = '<table border="0" cellspacing="0" cellpadding="1" class="audit_changes" width="100%">'
 71:     output << '<tr><th width="24%">Field</th><th width="43%">From</th><th width="43%">To</th></tr>'
 72: 
 73:     changes.keys.each do | key |
 74:       cdata = changes[ key ]
 75: 
 76:       if ( cdata.instance_of?( Array ) )
 77:         cfrom = to_descriptive_s( cdata[ 0 ] )
 78:         cto   = to_descriptive_s( cdata[ 1 ] )
 79:       else
 80:         cfrom = '(N/A)'
 81:         cto   = cdata.to_s()
 82:       end
 83: 
 84:       # Try to get clever :-) and see if we have "[foo_]id" in a string. If
 85:       # so, extract "foo" and see if the item can be found.
 86: 
 87:       array = key.split( '_' )
 88: 
 89:       if ( array.size > 1 )
 90:         check = array.pop()
 91: 
 92:         if ( check == 'id' )
 93:           type = array[ 0 ].capitalize
 94: 
 95:           if ( type == 'User' or type == 'Project' or type == 'Customer' or type == 'Task' )
 96:             field = ( type == 'User' ) ? 'name' : 'title'
 97: 
 98:             begin
 99:               const = type.constantize
100: 
101:               if ( cfrom.to_i.to_s == cfrom )
102:                 item  = const.find_by_id( cfrom )
103:                 cfrom = "#{ cfrom } (#{ item[ field ] })" unless ( item.nil? )
104:               end
105: 
106:               if ( cto.to_i.to_s == cto )
107:                 item = const.find_by_id( cto )
108:                 cto  = "#{ cto } (#{ item[ field ] })" unless ( item.nil? )
109:               end
110:             rescue
111:               # Do nothing - just ignore errors
112:             end
113:           end
114:         end
115:       end
116: 
117:       output << "<tr><td>#{ h( key ) }</td><td>#{ cfrom }</td><td>#{ cto }</td></tr>"
118:     end
119: 
120:     return output << '</table>'
121:   end

List view helper - display the type of change. Combines the action with the auditable type (the name of the model object on which the operation was performed), then converts the auditable type into an actual model class (via "constantize"). Uses the auditable ID to extract information specific to the object type and adds to the column output to include that data where possible. All exceptions caught to cope with deleted items etc. not being found.

[Source]

    # File app/helpers/audits_helper.rb, line 21
21:   def audithelp_type_of_change( record )
22:     type   = record.auditable_type.downcase
23:     type   = 'permitted OpenID' if type == 'permittedopenid'
24:     output = "#{ record.action.capitalize() } #{ h( type ) }"
25: 
26:     begin
27:       objtype = record.auditable_type.constantize()
28:       objinst = objtype.find_by_id( record.auditable_id )
29: 
30:       case ( record.auditable_type )
31:         when 'User'
32:           return append_link( output, "'#{ h( objinst.name ) }'", record )
33: 
34:         when 'Task', 'Project', 'Customer'
35:           return append_link( output, "'#{ h( objinst.title ) }'", record )
36: 
37:         when 'Timesheet'
38:           return append_link( output << ' for week', "#{ h( objinst.week_number ) }, #{ h( objinst.year ) }", record )
39: 
40:         else
41:           return output
42:       end
43: 
44:     rescue
45:       return output
46: 
47:     end
48:   end

List view helper - user name of person responsible for change

[Source]

    # File app/helpers/audits_helper.rb, line 52
52:   def audithelp_user_name( record )
53:     return to_descriptive_s( record.user ? record.user.name : nil )
54:   end

[Validate]