class BacklogsController < ApplicationController skip_before_filter :populate_layout, :only => [:index, :burn_down_chart, :burn_down_chart_thumbnail, :destroy] # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method => :post, :only => [ :destroy, :create, :update ], :redirect_to => { :action => :index } def index flash.keep if Task.find_started.size > 0 redirect_to :controller => 'tasks', :action => :list_started return end if Backlog.count == 0 redirect_to :action => :new return end most_urgent_backlog = Backlog.find(:first, :order => :id) redirect_to :action => :show, :id => most_urgent_backlog.id end def list @backlogs = Backlog.find(:all, :order => 'name') end def show if params[:id] @backlog = Backlog.find(params[:id]) end unless @backlog @backlog = Backlog.find(:first) unless @backlog redirect_to :controller => 'backlogs', :action => :new return end end load_tasks(@backlog) end def show_no_layout show render :partial => 'tasks' end def new @backlog = Backlog.new @work_accounts = WorkAccount.find(:all) end def create @backlog = Backlog.new(params[:backlog]) if @backlog.save flash[:notice] = 'Backlog was successfully created.' redirect_to :action => :show, :id => @backlog else render :action => 'new' end end def edit @backlog = Backlog.find(params[:id]) @work_accounts = WorkAccount.find(:all) end def edit_no_layout edit render :partial => 'tasks' end def update @backlog = Backlog.find(params[:id]) if @backlog.update_attributes(params[:backlog]) flash[:notice] = 'Backlog was successfully updated.' back_or_redirect_to :action => :show, :id => @backlog else render :action => 'edit' end end def destroy Backlog.find(params[:id]).destroy flash[:notice] = 'Backlog was successfully deleted.' redirect_to :action => 'index' end def update_task_estimate if params[:id] @task = Task.find_by_id(params[:id]) if params[:estimate] && params[:estimate][:todo] begin Float(params[:estimate][:todo]) @task.estimate(params[:estimate][:todo]) @period = @task.period @success, flash[:notice] = true, 'Estimate updated' rescue ArgumentError => e @success, flash[:notice] = false, "Estimate was not numeric" end if @task.finished? load_tasks(@task.backlog) render :action => 'finish_task', :layout => false else render :template => '/tasks/update_estimate', :layout => false end else return false, "Estimate is missing." end else @estimate = Estimate.new return false, 'Task id is missing.' end end def finish_task @task = Task.find(params[:id]) @task.finish(Task::COMPLETED, true) load_tasks(@task.backlog) end def abort_task @task = Task.find params[:id] @task.abort load_tasks(@task.backlog) render :action => :finish_task, :layout => false end def move_task_to_period params[:id] = $1 if params[:id] =~ /^task_(\d*)/ @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 load_tasks(@task.backlog) render :action => :move_task_to_period, :layout => false else rjs_detour_to :controller => 'periods', :action => :new, :period => {:party_id => @task.period.party_id}, :layout => false end else redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task_id => @task.id, :layout => false end end def move_task_to_next_period @task = Task.find(params[:id]) if @task.period next_period = @task.period.lower_item next_period = next_period.lower_item while next_period && next_period.passed? params[:period_id] = next_period && next_period.id move_task_to_period else rjs_redirect_to :controller => 'tasks', :action => :edit, :id => @task end end def reopen_task @task = Task.find(params[:id]).reopen load_tasks(@task.backlog) end def order params.keys.find {|k| k =~ /active_tasks_(.*)/} period_id = $1 if period_id period = period_id.empty? ? nil : Period.find(period_id) tasks = params["active_tasks_#{period_id}"].select {|id| not id.empty?} tasks.each_with_index do |id,idx| task = Task.find(id) task = task.move_to_period(period) if task.period != period task.insert_at(idx + 1) task.save! end end render :text => 'alert("Updated sort order");' end private def load_tasks(backlog) unplanned_tasks = backlog.tasks.select {|t| t.period.nil? && t.finished_at.nil?}.sort_by {|t| t.position} planned_tasks = backlog.tasks.select {|t| t.period && t.finished_at.nil?}.sort_by {|t| [t.period.end_on, t.position]} @tasks = planned_tasks + unplanned_tasks @completed_tasks = backlog.tasks.select {|t| t.finished_at}.sort {|t1, t2| t2.finished_at <=> t1.finished_at} end end