Module | TimesheetsHelper |
In: |
app/helpers/timesheets_helper.rb
|
File: | timesheets_helper.rb |
(C): | Hipposoft 2008, 2009 |
Purpose: | Support functions for views related to Timesheet objects. See controllers/timesheets_controller.rb for more. |
07-Jan-2008 (ADH): Created.
Return appropriate list view actions for the given timesheet
# File app/helpers/timesheets_helper.rb, line 232 232: def timesheethelp_actions( timesheet ) 233: if ( @current_user.admin? ) 234: return [ 'edit', 'delete', 'show' ] 235: elsif ( @current_user.manager? or timesheet.user_id == @current_user.id ) 236: return [ 'show' ] if ( timesheet.committed ) 237: return [ 'edit', 'show' ] 238: else 239: return [] 240: end 241: end
Return the timesheet description, or ‘None’ if it is empty.
# File app/helpers/timesheets_helper.rb, line 92 92: def timesheethelp_always_visible_description( timesheet ) 93: if ( timesheet.description.nil? or timesheet.description.empty? ) 94: des = 'None' 95: else 96: des = h( timesheet.description ) 97: end 98: 99: return des 100: end
Output HTML suitable as a label to show whether or not the given timesheet is committed or otherwise. The second parameter lets you override the given timesheet and force the generation of a committed (pass ‘true’) or not committed (pass ‘false’) label.
# File app/helpers/timesheets_helper.rb, line 84 84: def timesheethelp_commit_label( timesheet, committed = nil ) 85: committed = @timesheet.committed if ( committed.nil? ) 86: committed ? '<span class="timesheet_committed">Committed</span>' : 87: '<span class="timesheet_not_committed">Not Committed</span>' 88: end
List helper - formatted ‘committed at’ date for the given timesheet
# File app/helpers/timesheets_helper.rb, line 216 216: def timesheethelp_committed_at( timesheet ) 217: if ( timesheet.committed ) 218: return apphelp_date( timesheet.committed_at ) 219: else 220: return 'Not committed' 221: end 222: end
List helper - number of hours in total recorded in the given timesheet
# File app/helpers/timesheets_helper.rb, line 226 226: def timesheethelp_hours( timesheet ) 227: return apphelp_string_hours( timesheet.total_sum.to_s, '-', '-' ) 228: end
List helper - owner of the given timesheet
# File app/helpers/timesheets_helper.rb, line 204 204: def timesheethelp_owner( timesheet ) 205: return link_to( timesheet.user.name, user_path( timesheet.user ) ) 206: end
Return HTML suitable for inclusion in the form passed in the first parameter (i.e. the ‘f’ in "form for … do |f|" ), based on the task array given in the second parameter, which provides:
# File app/helpers/timesheets_helper.rb, line 63 63: def timesheethelp_task_selection( form, tasks ) 64: if ( tasks.empty? ) 65: return '' 66: else 67: Task.sort_by_augmented_title( tasks ) 68: 69: return apphelp_collection_select( 70: form, 71: 'task_ids', 72: tasks, 73: :id, 74: :augmented_title 75: ) 76: end 77: end
Return an array of tasks suitable for timesheet row addition. Will be empty if all tasks are already included, or no tasks are available for any other reason. Pass the timesheet of interest.
# File app/helpers/timesheets_helper.rb, line 49 49: def timesheethelp_tasks_for_addition( timesheet ) 50: @current_user.active_permitted_tasks - timesheet.tasks 51: end
List helper - formatted ‘updated at’ date for the given timesheet
# File app/helpers/timesheets_helper.rb, line 210 210: def timesheethelp_updated_at( timesheet ) 211: return apphelp_date( timesheet.updated_at ) 212: end
Return HTML suitable for inclusion in the form passed in the first parameter (i.e. the ‘f’ in "form for … do |f|" ), based on the timesheet given in the second parameter, which provides:
# File app/helpers/timesheets_helper.rb, line 27 27: def timesheethelp_week_selection( form, timesheet ) 28: weeks = timesheet.unused_weeks(); 29: 30: if ( weeks.empty? ) 31: return '' 32: else 33: return form.select( 34: :week_number, 35: weeks.collect do | week | 36: [ 37: "#{ week } (#{ Timesheet.date_for( timesheet.year, week, TimesheetRow::FIRST_DAY ) })", 38: week 39: ] 40: end 41: ) 42: end 43: end
Return a year chart for the given year. This is a complete table of months down the left and week numbers with dates of the first week in individual cells along the monthly rows. Months indicate the month of the first day of the week in that year, so in week 1 will often be for the previous year (which is clearly indicated in the table). Cell colours indicate the condition of a timesheet for each week with links to edit the existing timesheets or create new timesheets as necessary.
# File app/helpers/timesheets_helper.rb, line 110 110: def timesheethelp_year_chart( year ) 111: week_range = 1..( Timesheet.get_last_week_number( year ) ) 112: first_day = TimesheetRow::FIRST_DAY 113: months = Hash.new 114: 115: # Compile a hash keyed by year/month number which points to arrays of 116: # week numbers with start date. The length of each keyed entry indicates 117: # the number of weeks in that month. Key names are sortable by default 118: # sort function behaviour to provide a date-ascending list. 119: 120: week_range.each do | week | 121: start_date = Timesheet.date_for( year, week, first_day, true ) 122: key = "#{ start_date.year }-%02i" % start_date.month 123: data = { :week => week, :start_date => start_date } 124: 125: if ( months[ key ].nil? ) 126: months[ key ] = [ data ] 127: else 128: months[ key ].push( data ) 129: end 130: end 131: 132: # Now run through the collated data to build the chart, working on the 133: # basis of the sorted keys in the hash for each row and a maximum of 5 134: # weeks in any of those rows. Blank entries are put at the start of a 135: # row to make up 5 columns in case there aren't that many weeks in that 136: # particular month. 137: 138: keys = months.keys.sort 139: row_class = 'even' 140: output = "<table class=\"timesheet_chart\" border=\"0\" cellspacing=\"0\" cellpadding=\"4\">\n" 141: output << " <tr><th>Month</th><th colspan=\"5\">Week start date and number</th></tr>\n" 142: 143: keys.each do | key | 144: data = months[ key ] 145: row_start = data[ 0 ][ :start_date ] 146: 147: heading = "#{ Date::MONTHNAMES[ row_start.month ] } "<< 148: "#{ row_start.year }" 149: 150: row_class = ( row_class == 'even' ) ? 'odd' : 'even' 151: row_class = row_class + ' last' if ( key == keys.last ) 152: 153: output << " <tr valign=\"middle\" class=\"#{ row_class }\">\n" 154: output << " <td class=\"timesheet_chart_month\" align=\"left\">#{ heading }</td>\n" 155: output << " <td align=\"center\"> </td>\n" * ( 5 - data.length ) 156: 157: data.each do | week | 158: timesheet = Timesheet.find_by_user_id_and_year_and_week_number( 159: @current_user.id, 160: year, 161: week[ :week ] 162: ) 163: 164: bgcolor = '' 165: content = "#{ week[ :start_date ].day }" << 166: " #{ Date::ABBR_MONTHNAMES[ week[ :start_date ].month ] }" << 167: " (#{ week[ :week ] })" 168: 169: if ( timesheet ) 170: if ( timesheet.committed ) 171: bgcolor = ' bgcolor="#77cc77" class="committed"' 172: content = link_to( content, timesheet_path( timesheet ) ) 173: else 174: bgcolor = ' bgcolor="#ffaa77" class="not_committed"' 175: content = link_to( content, edit_timesheet_path( timesheet ) ) 176: end 177: else 178: content = button_to( 179: content, 180: { 181: :action => :create, 182: :method => :post, 183: :year => year, 184: :week_number => week[ :week ] 185: } 186: ) 187: end 188: 189: output << " <td align='center'#{ bgcolor }>#{ content }</td>\n" 190: end 191: 192: output << " </tr>\n" 193: end 194: 195: return output << '</table>' 196: end