class TasksController < ApplicationController skip_before_filter :populate_layout, :only => [:create, :update, :destroy, :set_task_description, :move_to, :move_to_next_period, :move_to_period, :reopen, :start_work] verify :method => :post, :except => [ :new, :show, :edit, :list_started, :move_to_next_period], :redirect_to => { :controller => 'backlogs' } def list_started @tasks = Task.find_started(user) @selected_task = Task.find_by_id(params[:id]) back_or_redirect_to :controller => nil, :action => nil if @tasks.empty? end def new redirect_to params if request.post? @task = Task.new params[:task] @task.backlog_id ||= @task.period && @task.period.most_frequent_backlog @backlogs = Backlog.find(:all, :order => 'name') @periods = Period.find_active_or_future end def create if params[:task] && params[:task][:period_id] && params[:task][:period_id][0..0] == 'G' detour_to :controller => 'periods', :action => :new, :group_id => params[:task][:period_id][1..-1].to_i return end @task = Task.new(params[:task]) if @task.save flash[:notice] = 'Task was successfully created.' @task.move_to_top if @task.period back_or_redirect_to :controller => 'periods', :action => 'show', :id => @task.period, :task => @task.id else back_or_redirect_to :controller => 'backlogs', :action => 'show', :id => @task.backlog, :task => @task.id end else populate_layout @backlogs = Backlog.find(:all, :order => 'name') @periods = Period.find_active_or_future render :action => 'new' end end def edit @task = Task.find(params[:id]) @task.period_id = params[:task][:period_id] if params[:task] && params[:task][:period_id] @periods = Period.find_active_or_future @backlogs = Backlog.find(:all, :order => 'name') end def update redirect_to(:controller => 'backlogs', :action => :index) unless params[:id] if params[:task] && params[:task][:description] == '' destroy return end @task = Task.find(params[:id]) unless @task.period.active? @task.errors.add(:period_id, "You may not update a task in an inactive period. Move the task to an active period first.") redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task => @task.id return end if params[:task] && @task.update_attributes(params[:task]) flash[:notice] = 'Task was successfully updated.' if params[:task][:todo] && params[:task][:todo].to_i == 0 unless @task.finished_at next_task = @task.lower_item || @task.higher_item @task.finish Task::COMPLETED, true, user @task.save redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task => next_task ? next_task.id : nil else redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task => @task.id end else if @task.finished_at reopen else redirect_to :controller => 'periods', :action => 'show', :id => @task.period, :task => @task.id end end else populate_layout edit render :action => 'edit' end end def destroy task = Task.find(params[:id]) lower_item = task.lower_item task.destroy flash[:notice] = 'Task was successfully deleted.' redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => lower_item ? lower_item.id : nil end def move_up task = Task.find(params[:id]) task.move_higher flash[:notice] = "Task #{@focus_id} was successfully moved." redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => task.id end def move_down task = Task.find(params[:id]) task.move_lower redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => task.id end def move_to_top task = Task.find(params[:id]) task.move_to_top redirect_to :action => :list, :id => task end def move_to_bottom task = Task.find(params[:id]) task.move_to_bottom redirect_to :action => :list, :id => task end def move_to task = Task.find(params[:id][5..-1]) task.insert_at params[:position] back_or_redirect_to :controller => 'periods', :action => :show_nolayout, :id => task.period, :task => task.id end def move_to_period(with_layout = false) task = Task.find(params[:id]) if request.post? period = params[:period_id] && Period.find(params[:period_id]) if period && period.active_or_future? next_task = task.higher_item ? task.higher_item : task.lower_item ? task.lower_item : task task.move_to_period period if with_layout back_or_redirect_to :controller => 'periods', :action => :show, :id => next_task.period, :task_id => next_task.id else back_or_redirect_to :controller => 'periods', :action => :show_nolayout, :id => next_task.period, :task_id => next_task.id end else detour_to :controller => 'periods', :action => :new, :period => {:party_id => task.period.party_id}, :layout => with_layout && determine_layout end else redirect_to :controller => 'periods', :action => :show, :id => task.period, :task_id => task.id, :layout => with_layout && determine_layout end end def move_to_next_period task = Task.find(params[:id]) if task.period params[:period_id] = task.period.lower_item && task.period.lower_item.id move_to_period(true) else detour_to :action => :edit, :id => task end end def finish @task = Task.find(params[:id]) @task.finish(Task::COMPLETED, true) redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task_id => @task.id end def abort task = Task.find params[:id] task.abort(user) back_or_redirect_to(:controller => "periods", :action => "show", :id => task.period) end def reopen task = Task.find(params[:id]) task.reopen back_or_redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => task.id end def start_work task = Task.find(params[:id]) task.start_work redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => task.id end def end_work @task = Task.find(params[:id]) next_quarter = Time.next_quarter if started_work = @task.started_work redirect_to({ :controller => 'works', :action => :edit, :id => started_work, 'work[task_id]' => @task.id, 'work[started_at]' => started_work.started_at.iso8601, 'work[completed_at]' => [next_quarter, started_work.started_at].max.iso8601, 'work[hours]' => "%0.2f" % ((next_quarter - started_work.started_at).to_f / 3600), 'estimate[todo]' => @task.todo }) else @task.update_attributes(params[:task]) redirect_to({ :controller => 'works', :action => :new, 'work[task_id]' => @task.id, 'work[started_at]' => Time.previous_quarter.iso8601, 'work[completed_at]' => next_quarter.iso8601, 'work[hours]' => "0.25", 'estimate[todo]' => @task.todo }) end end def specify parent_task = Task.find(params[:id]) raise "Task is not open" unless parent_task.active? if parent_task.children.empty? subtask_estimate = (parent_task.todo / 4).ceil no_of_subtasks = (parent_task.todo / subtask_estimate).to_i subtask_estimate = parent_task.todo / no_of_subtasks (1..no_of_subtasks).each do |index| subtask = parent_task.children.build subtask.description = "#{parent_task.description} ##{parent_task.children.size}" subtask.initial_estimate = subtask_estimate subtask.save! end redirect_to :controller => 'periods', :action => :show, :id => parent_task.period, :task => parent_task.children.first else average_estimate = parent_task.children.reduce {|value, task, size| value + (task.initial_estimate / size)} subtask = parent_task.children.build subtask.description = "#{parent_task.description} ##{parent_task.children.size}" subtask.initial_estimate = average_estimate subtask.save! redirect_to :controller => 'periods', :action => :show, :id => parent_task.period, :task => parent_task.children.first end if started_work = parent_task.started_work(user) flash[:notice] = "Pågående arbeid er flyttet til subtask" started_work.task = parent_task.children.last started_work.save! end end def set_task_description task = Task.find(params[:id]) task.description = params[:value] task.save! render :text => task.description end private end