Class | TasksController |
In: |
app/controllers/tasks_controller.rb
|
Parent: | ApplicationController |
File: | tasks_controller.rb |
(C): | Hipposoft 2008, 2009 |
Purpose: | Manage Task objects. See models/task.rb for more. |
04-Jan-2008 (ADH): Created.
Create a new task following submission of a ‘create’ view form. Restricted users can‘t do this. Works via ApplicationController.appctrl_create.
# File app/controllers/tasks_controller.rb, line 141 141: def create 142: appctrl_create( 'Task' ) 143: end
Tasks should not normally be destroyed. Only administrators can do this. Works via ApplicationController.appctrl_delete.
# File app/controllers/tasks_controller.rb, line 177 177: def delete 178: appctrl_delete( 'Task' ) 179: end
Show an ‘Are you sure?’ prompt. Works via ApplicationController.appctrl_delete_confirm.
# File app/controllers/tasks_controller.rb, line 184 184: def delete_confirm 185: appctrl_delete_confirm( 'Task' ) 186: end
List tasks.
# File app/controllers/tasks_controller.rb, line 36 36: def index 37: 38: # Set up the column data; see the index helper functions in 39: # application_helper.rb for details. 40: 41: @columns = [ 42: { :header_text => 'Customer', :sort_by => 'customers.title', :value_helper => :taskhelp_customer }, 43: { :header_text => 'Project', :sort_by => 'projects.title', :value_helper => :taskhelp_project }, 44: { :header_text => 'Task title', :value_method => :title, :value_in_place => true }, 45: { :header_text => 'Task code', :value_method => :code, :value_in_place => true }, 46: { :header_text => 'Billable?', :value_method => :billable, :value_in_place => true, 47: :header_align => 'center', :value_align => 'center' }, 48: { :header_text => 'Created at', :value_method => :created_at, :value_helper => :apphelp_created_at }, 49: { :header_text => 'Duration', :value_method => :duration, :value_in_place => true, 50: :header_align => 'center', :value_align => 'center' }, 51: ] 52: 53: # Get the basic options hash from ApplicationController, then work out 54: # the conditions on objects being fetched, including handling the search 55: # form data. 56: 57: options = appctrl_index_assist( Task ) 58: active_vars = { :active => true } 59: inactive_vars = { :active => false } 60: conditions_sql = "WHERE ( tasks.active = :active )\n" 61: 62: # The user may only be able to see tasks matching a specific list of IDs. 63: 64: restrictions_sql = '' 65: 66: if ( @current_user.restricted? ) 67: restrictions_sql = 'AND ( ' 68: if ( @current_user.task_ids.empty? ) 69: restrictions_sql << 'tasks.id = -1' # Never matches - forces no results 70: else 71: restrictions_sql << "tasks.project_id = projects.id AND tasks.id IN (#{ @current_user.task_ids.join( ',' ) } )" 72: end 73: restrictions_sql << " )\n" 74: end 75: 76: # If asked to search for something, build extra conditions to do so. 77: 78: unless ( params[ :search ].nil? ) 79: if ( params[ :search ].empty? or params[ :search_cancel ] ) 80: params.delete( :search ) 81: else 82: search = "%#{ params[ :search ] }%" # SQL wildcards either side of the search string 83: conditions_sql << "AND ( tasks.title ILIKE :search OR tasks.code ILIKE :search OR projects.title ILIKE :search OR customers.title ILIKE :search )\n" 84: vars = { :search => search } 85: active_vars.merge!( vars ) 86: inactive_vars.merge!( vars ) 87: end 88: end 89: 90: # Sort order is already partially compiled in 'options' from the earlier 91: # call to 'appctrl_index_assist'. 92: 93: order_sql = "ORDER BY #{ options[ :order ] }" 94: options.delete( :order ) 95: 96: # Compile the main SQL statement. We want to select all columns of any 97: # task, fetching projects where the project ID matches the project ID used 98: # by each task and customers where the customer ID matches the customer ID 99: # used by each project; LEFT OUTER JOIN means that any task lacking a 100: # project or any project lacking a customer will still be included; we then 101: # apply any specific search conditions determined above and finally specify 102: # the order for returning results. 103: 104: finder_sql = "SELECT tasks.* FROM tasks\n" << 105: "LEFT OUTER JOIN projects ON ( tasks.project_id = projects.id )\n" << 106: "LEFT OUTER JOIN customers ON ( projects.customer_id = customers.id )\n" << 107: "#{ conditions_sql }\n" << 108: "#{ restrictions_sql }\n" << 109: "#{ order_sql }" 110: 111: # Now paginate using this SQL. The only difference between the active and 112: # inactive cases is the value of the variables passed to Active Record for 113: # substitution into the final SQL query going to the database. 114: 115: @active_tasks = Task.paginate_by_sql( [ finder_sql, active_vars ], options ) 116: @inactive_tasks = Task.paginate_by_sql( [ finder_sql, inactive_vars ], options ) 117: end
Prepare to create a new task. Restricted users can‘t do this. There must be at least one active project available first.
# File app/controllers/tasks_controller.rb, line 129 129: def new 130: if ( Project.active.count.zero? ) 131: flash[ :error ] = 'You must create at least one active project first.' 132: redirect_to( new_project_path() ) 133: else 134: appctrl_new( 'Task' ) 135: end 136: end
Show details of a task. Restricted users can only see tasks in their permitted tasks list. Works via ApplicationController.appctrl_show.
# File app/controllers/tasks_controller.rb, line 122 122: def show 123: appctrl_show( 'Task' ) 124: end
Update a task following submission of an ‘edit’ view form. Restricted users can‘t do this.
@record is set by the "can_be_modified?" before_filter method.
# File app/controllers/tasks_controller.rb, line 150 150: def update 151: 152: # The update call used below deals with repercussions of changes 153: # to the 'active' flag and is the reason for the transaction. 154: 155: begin 156: Task.transaction do 157: @record.update_with_side_effects!( params[ :task ] ) 158: 159: flash[ :notice ] = 'Task details updated.' 160: redirect_to( tasks_path() ) 161: end 162: 163: rescue ActiveRecord::StaleObjectError 164: flash[ :error ] = 'The task details were modified by someone else while you were making changes. Please examine the updated information before editing again.' 165: redirect_to( task_path( params[ :task ] ) ) 166: 167: rescue => error 168: flash[ :error ] = "Could not update task details: #{ error }" 169: render( :action => 'edit' ) 170: 171: end 172: end