Class | Task |
In: |
app/models/task.rb
|
Parent: | ActiveRecord::Base |
File: | task.rb |
(C): | Hipposoft 2008, 2009 |
Purpose: | Describe the behaviour of Task objects. See below for more details. |
24-Dec-2007 (ADH): Created.
DEFAULT_SORT_COLUMN | = | 'code' |
DEFAULT_SORT_DIRECTION | = | 'ASC' |
DEFAULT_SORT_ORDER | = | "#{ DEFAULT_SORT_COLUMN } #{ DEFAULT_SORT_DIRECTION }" |
Given an ID, generate a task code for an XML-imported task based on the current date.
# File app/models/task.rb, line 95 95: def self.generate_xml_code( id ) 96: today = Date.current 97: "XML-%04d%02d%02d-#{ id }" % [ today.year, today.month, today.day ] 98: end
Class method - sort an array of tasks by the augmented title. Since this isn‘t done by the database, it‘s slow.
# File app/models/task.rb, line 118 118: def self.sort_by_augmented_title( list ) 119: list.sort! { | x, y | x.augmented_title <=> y.augmented_title } 120: end
Assign default conditions for a brand new object, used whether or not the object ends up being saved in the database (so a before_create filter is not sufficient). For user-specific default values, pass a User object.
# File app/models/task.rb, line 148 148: def assign_defaults( user ) 149: self.duration = 0 150: self.active = true 151: self.code = "TID%05d" % Task.count 152: 153: # User-specific defaults - pick up the default project 154: # from the user's control panel, if defined. 155: 156: if ( user and user.control_panel ) 157: cp = user.control_panel 158: self.project = cp.project if ( cp.project and cp.project.active ) 159: end 160: end
Return the ‘augmented’ task title; that is, the task name, with the project and customer names appended if available.
# File app/models/task.rb, line 103 103: def augmented_title 104: if ( self.project ) 105: if ( self.project.customer ) 106: return "#{ self.project.customer.title } - #{ self.project.title }: #{ self.title }" 107: end 108: 109: return "#{ self.project.title }: #{ self.title }" 110: end 111: 112: return "#{ self.title }" 113: end
Is the given user permitted to update this task? Restricted users cannot modify tasks. Administrators always can. Managers only can if the task is still active.
# File app/models/task.rb, line 86 86: def can_be_modified_by?( user ) 87: return false if ( user.restricted? ) 88: return true if ( user.admin? ) 89: return self.active 90: end
Number of committed hours worked on this task.
# File app/models/task.rb, line 205 205: def committed_worked 206: sum = 0.0 207: 208: self.work_packets.each do | work_packet | 209: sum += work_packet.worked_hours if ( work_packet.timesheet_row.timesheet.committed ) 210: end 211: 212: return sum 213: end
Is the given user permitted to do anything with this task?
# File app/models/task.rb, line 78 78: def is_permitted_for?( user ) 79: return ( user.privileged? or user.tasks.include?( self ) ) 80: end
Number of not committed hours worked on this task.
# File app/models/task.rb, line 217 217: def not_committed_worked 218: sum = 0.0 219: 220: self.work_packets.each do | work_packet | 221: sum += work_packet.worked_hours unless ( work_packet.timesheet_row.timesheet.committed ) 222: end 223: 224: return sum 225: end
Return an array with two elements - the first is the restricted associated users, the second the unrestricted associated users. Arrays will be empty if there are no associated users. Does this the slow way, asking each user if it is restricted, rather than making assumptions about how to quickly find restricted types.
# File app/models/task.rb, line 128 128: def split_user_types 129: restricted = [] 130: unrestricted = [] 131: 132: self.users.each do | user | 133: if ( user.restricted? ) 134: restricted.push( user ) 135: else 136: unrestricted.push( user ) 137: end 138: end 139: 140: return [ restricted, unrestricted ] 141: end
Number of hours worked on the task between the given start and end Dates as a Range. Optionally pass a User object; work packets will only be counted if they‘re in a timesheet belonging to that user.
Returns an object with fields ‘committed’ and ‘not_committed’, giving sums for those types of hours.
# File app/models/task.rb, line 235 235: def sum_hours_over_range( date_range, user = nil ) 236: committed_sum = 0.0 237: not_committed_sum = 0.0 238: work_packets = self.work_packets.find( :all, :conditions => { :date => date_range } ) 239: 240: work_packets.each do | work_packet | 241: timesheet = work_packet.timesheet_row.timesheet 242: 243: if ( user.nil? or timesheet.user == user ) 244: if ( timesheet.committed ) 245: committed_sum += work_packet.worked_hours 246: else 247: not_committed_sum += work_packet.worked_hours 248: end 249: end 250: end 251: 252: return { :committed => committed_sum, :not_committed => not_committed_sum } 253: end
Number of hours worked on this task, committed or otherwise
# File app/models/task.rb, line 199 199: def total_worked 200: return self.work_packets.sum( :worked_hours ) || 0.0 201: end
Update an object with the given attributes. This is done by a special model method because changes of the ‘active’ flag have side effects for other associated objects. THE CALLER *MUST* USE A TRANSACTION around a call to this method. There is no need to call here unless the ‘active’ flag state is changing.
# File app/models/task.rb, line 168 168: def update_with_side_effects!( attrs ) 169: active = self.active 170: self.update_attributes!( attrs ) 171: 172: # If the active flag has changed and it *was* 'true', then the task 173: # has just been made inactive. 174: 175: if ( attrs[ :active ] != active and active == true ) 176: 177: # When tasks are made inactive, remove them from each of 178: # the Task lists in User and ControlPanel objects. There 179: # are checks for this elsewhere, but they're only to try 180: # and catch cases where this code has gone wrong. 181: # 182: # Since only restricted users actually make use of their 183: # tasks lists, save time by only changing that user type. 184: 185: User.restricted.each do | user | 186: user.remove_inactive_tasks() 187: user.save! 188: end 189: 190: ControlPanel.find( :all ).each do | cp | 191: cp.remove_inactive_tasks() 192: cp.save! 193: end 194: end 195: end