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 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 redirect_to :controller => 'periods', :action => :index 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 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 def new @backlog = Backlog.new 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]) 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.' 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 burn_down_chart_thumbnail send_burn_down_chart 270 end def burn_down_chart send_burn_down_chart 640 end private def send_burn_down_chart(size) backlog = Backlog.find(params[:id]) g = Gruff::Line.new(size) g.theme_37signals g.title = "Burn Down Graph" g.font = '/usr/share/fonts/bitstream-vera/Vera.ttf' g.hide_dots = true g.colors = %w{blue green} dates = [] backlog.periods.first.start_on.upto(backlog.periods.last.end_on) {|date| dates << date} g.data(l(:todo), get_todo_data(dates, backlog)) g.data(l(:done), get_work_data(dates, backlog)) g.minimum_value = 0 labels = {0 => dates.first.to_s, dates.length-1 => dates.last.to_s} labels.merge({dates.index(Date.today) => Date.today.to_s}) if (dates.index(Date.today).to_f / dates.length) > 0.15 g.labels = labels send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "burn_down_chart.png") end def get_todo_data(dates, backlog) dates.map do |date| backlog.get_estimate_data(date) end end def get_work_data(dates, backlog) dates.map do |date| backlog.get_work_data(date) end end end