annotate .svn/pristine/9f/9fbac0459fe6cdaf55fb0e58852e9a45b0b0309f.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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