class PeriodsController < ApplicationController skip_before_filter :populate_layout, :only => [:index, :show_no_layout, :destroy, :burn_down_chart_thumbnail, :burn_down_chart] skip_before_filter :authenticate_user, :only => [:burn_down_chart, :burn_down_chart_thumbnail, :burn_down_chart_large] def index active_periods = Period.find(:all).select {|period| period.active?(true)} my_active_periods = active_periods.select {|period| period.party.includes?(current_user)} unless my_active_periods.empty? most_urgent_period = my_active_periods.sort_by {|p| p.required_speed}.last else most_urgent_period = active_periods.sort_by {|p| p.required_speed}.last end flash.keep redirect_to :action => 'show', :id => most_urgent_period end # 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 show if params[:task_id] @selected_task = Task.find(params[:task_id]) end if @selected_task @period = @selected_task.period if @period.nil? redirect_to :controller => 'backlogs', :action => :show, :id => @selected_task.backlog.id return end elsif params[:id] && @period = Period.find_by_id(params[:id]) elsif @period = Period.find(:first) else redirect_to :action => :new return end if @selected_task.nil? @selected_task = @period.open_tasks.first end load_tasks(@period) end def show_no_layout show render :partial => 'show_active', :layout => false, :locals => {:i => 0} end def new @period = Period.new if params[:period] && params[:period][:party_id] @period.party_id = params[:period][:party_id] previous = @period.party.periods.last if previous @period.start_on = previous.end_on + 1 @period.end_on = previous.end_on + (1 + (previous.end_on - previous.start_on)) else @period.start_on = Date.today end end @parties = Party.find(:all) end def create @period = Period.new(params[:period]) if @period.save @period.reload flash[:notice] = 'Period was successfully created.' back_or_redirect_to :action => 'show', :id => @period.higher_item ? @period.higher_item : @period else render :action => 'new' end end def edit @period = Period.find(params[:id]) end def update @period = Period.find(params[:id]) if @period.update_attributes(params[:period]) flash[:notice] = 'Period was successfully updated.' redirect_to :action => 'show', :id => @period else render :action => 'edit' end end def destroy Period.find(params[:id]).destroy flash[:notice] = 'Period was successfully deleted.' redirect_to :action => 'index' end def burn_down_chart_thumbnail send_burn_down_chart Chart::THUMBNAIL end def burn_down_chart send_burn_down_chart Chart::NORMAL end def burn_down_chart_large send_burn_down_chart Chart::LARGE end def list_work if @period = Period.find_by_id(params[:id]) @works = @period.tasks.map {|t| t.works}.flatten.sort_by {|w| [w.completed_at || Time.now.midnight, w.started_at]} def @works.page_count; size / 25; end render :template => '/works/list' else flash[:notice] = "You need to provide a period ID" redirect_to '' end end def make_link if params[:id].to_i > 0 render :partial => 'link', :layout => false, :locals => {:period => Period.find(params[:id])} else render :text => '' end 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.period) 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]) 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 end def finish_task @task = Task.find(params[:id]) @task.finish(Task::COMPLETED, true) load_tasks(@task.period) end def abort_task @task = Task.find params[:id] @task.abort load_tasks(@task.period) render :action => :finish_task, :layout => false end def reopen_task @task = Task.find(params[:id]) @task.reopen @task.reload finished_count = Task.count(:conditions => ['period_id = ? AND finished_at IS NOT NULL', @task.period_id]) @last_finished = finished_count == 0 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.period) 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 order params.keys.find {|k| k =~ /active_tasks_(.*)/} period_id = $1 tasks = params["active_tasks_#{period_id}"].select {|id| not id.empty?} tasks.each_with_index do |id,idx| task = Task.find(id) task.insert_at(idx + 1) task.save! end render :text => 'Updated sort order' end private def send_burn_down_chart(size) period = Period.find(params[:id]) g = period.burn_down_graph(size) send_data(g, :disposition => 'inline', :type => 'image/png', :filename => "burn_down_chart.png") end def load_tasks(period) @tasks = period.open_tasks @completed_tasks = period.completed_tasks.reverse end end