annotate app/models/workflow.rb @ 888:96376d0a80bd feature_80

Close obsolete branch feature_80
author Chris Cannam
date Sat, 05 Mar 2011 16:02:23 +0000
parents 513646585e45
children 0c939c159af4
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class Workflow < ActiveRecord::Base
Chris@0 19 belongs_to :role
Chris@0 20 belongs_to :old_status, :class_name => 'IssueStatus', :foreign_key => 'old_status_id'
Chris@0 21 belongs_to :new_status, :class_name => 'IssueStatus', :foreign_key => 'new_status_id'
Chris@0 22
Chris@0 23 validates_presence_of :role, :old_status, :new_status
Chris@0 24
Chris@0 25 # Returns workflow transitions count by tracker and role
Chris@0 26 def self.count_by_tracker_and_role
Chris@0 27 counts = connection.select_all("SELECT role_id, tracker_id, count(id) AS c FROM #{Workflow.table_name} GROUP BY role_id, tracker_id")
Chris@0 28 roles = Role.find(:all, :order => 'builtin, position')
Chris@0 29 trackers = Tracker.find(:all, :order => 'position')
Chris@0 30
Chris@0 31 result = []
Chris@0 32 trackers.each do |tracker|
Chris@0 33 t = []
Chris@0 34 roles.each do |role|
Chris@0 35 row = counts.detect {|c| c['role_id'].to_s == role.id.to_s && c['tracker_id'].to_s == tracker.id.to_s}
Chris@0 36 t << [role, (row.nil? ? 0 : row['c'].to_i)]
Chris@0 37 end
Chris@0 38 result << [tracker, t]
Chris@0 39 end
Chris@0 40
Chris@0 41 result
Chris@0 42 end
Chris@0 43
Chris@0 44 # Find potential statuses the user could be allowed to switch issues to
Chris@0 45 def self.available_statuses(project, user=User.current)
Chris@0 46 Workflow.find(:all,
Chris@0 47 :include => :new_status,
Chris@0 48 :conditions => {:role_id => user.roles_for_project(project).collect(&:id)}).
Chris@0 49 collect(&:new_status).
Chris@0 50 compact.
Chris@0 51 uniq.
Chris@0 52 sort
Chris@0 53 end
Chris@0 54
Chris@0 55 # Copies workflows from source to targets
Chris@0 56 def self.copy(source_tracker, source_role, target_trackers, target_roles)
Chris@0 57 unless source_tracker.is_a?(Tracker) || source_role.is_a?(Role)
Chris@0 58 raise ArgumentError.new("source_tracker or source_role must be specified")
Chris@0 59 end
Chris@0 60
Chris@0 61 target_trackers = [target_trackers].flatten.compact
Chris@0 62 target_roles = [target_roles].flatten.compact
Chris@0 63
Chris@0 64 target_trackers = Tracker.all if target_trackers.empty?
Chris@0 65 target_roles = Role.all if target_roles.empty?
Chris@0 66
Chris@0 67 target_trackers.each do |target_tracker|
Chris@0 68 target_roles.each do |target_role|
Chris@0 69 copy_one(source_tracker || target_tracker,
Chris@0 70 source_role || target_role,
Chris@0 71 target_tracker,
Chris@0 72 target_role)
Chris@0 73 end
Chris@0 74 end
Chris@0 75 end
Chris@0 76
Chris@0 77 # Copies a single set of workflows from source to target
Chris@0 78 def self.copy_one(source_tracker, source_role, target_tracker, target_role)
Chris@0 79 unless source_tracker.is_a?(Tracker) && !source_tracker.new_record? &&
Chris@0 80 source_role.is_a?(Role) && !source_role.new_record? &&
Chris@0 81 target_tracker.is_a?(Tracker) && !target_tracker.new_record? &&
Chris@0 82 target_role.is_a?(Role) && !target_role.new_record?
Chris@0 83
Chris@0 84 raise ArgumentError.new("arguments can not be nil or unsaved objects")
Chris@0 85 end
Chris@0 86
Chris@0 87 if source_tracker == target_tracker && source_role == target_role
Chris@0 88 false
Chris@0 89 else
Chris@0 90 transaction do
Chris@0 91 delete_all :tracker_id => target_tracker.id, :role_id => target_role.id
Chris@0 92 connection.insert "INSERT INTO #{Workflow.table_name} (tracker_id, role_id, old_status_id, new_status_id)" +
Chris@0 93 " SELECT #{target_tracker.id}, #{target_role.id}, old_status_id, new_status_id" +
Chris@0 94 " FROM #{Workflow.table_name}" +
Chris@0 95 " WHERE tracker_id = #{source_tracker.id} AND role_id = #{source_role.id}"
Chris@0 96 end
Chris@0 97 true
Chris@0 98 end
Chris@0 99 end
Chris@0 100 end