Chris@1115: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1115: # Chris@1115: # This program is free software; you can redistribute it and/or Chris@1115: # modify it under the terms of the GNU General Public License Chris@1115: # as published by the Free Software Foundation; either version 2 Chris@1115: # of the License, or (at your option) any later version. Chris@1115: # Chris@1115: # This program is distributed in the hope that it will be useful, Chris@1115: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1115: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1115: # GNU General Public License for more details. Chris@1115: # Chris@1115: # You should have received a copy of the GNU General Public License Chris@1115: # along with this program; if not, write to the Free Software Chris@1115: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1115: Chris@1115: class WorkflowRule < ActiveRecord::Base Chris@1115: self.table_name = "#{table_name_prefix}workflows#{table_name_suffix}" Chris@1115: Chris@1115: belongs_to :role Chris@1115: belongs_to :tracker Chris@1115: belongs_to :old_status, :class_name => 'IssueStatus', :foreign_key => 'old_status_id' Chris@1115: belongs_to :new_status, :class_name => 'IssueStatus', :foreign_key => 'new_status_id' Chris@1115: Chris@1115: validates_presence_of :role, :tracker, :old_status Chris@1115: Chris@1115: # Copies workflows from source to targets Chris@1115: def self.copy(source_tracker, source_role, target_trackers, target_roles) Chris@1115: unless source_tracker.is_a?(Tracker) || source_role.is_a?(Role) Chris@1115: raise ArgumentError.new("source_tracker or source_role must be specified") Chris@1115: end Chris@1115: Chris@1115: target_trackers = [target_trackers].flatten.compact Chris@1115: target_roles = [target_roles].flatten.compact Chris@1115: Chris@1115: target_trackers = Tracker.sorted.all if target_trackers.empty? Chris@1115: target_roles = Role.all if target_roles.empty? Chris@1115: Chris@1115: target_trackers.each do |target_tracker| Chris@1115: target_roles.each do |target_role| Chris@1115: copy_one(source_tracker || target_tracker, Chris@1115: source_role || target_role, Chris@1115: target_tracker, Chris@1115: target_role) Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: # Copies a single set of workflows from source to target Chris@1115: def self.copy_one(source_tracker, source_role, target_tracker, target_role) Chris@1115: unless source_tracker.is_a?(Tracker) && !source_tracker.new_record? && Chris@1115: source_role.is_a?(Role) && !source_role.new_record? && Chris@1115: target_tracker.is_a?(Tracker) && !target_tracker.new_record? && Chris@1115: target_role.is_a?(Role) && !target_role.new_record? Chris@1115: Chris@1115: raise ArgumentError.new("arguments can not be nil or unsaved objects") Chris@1115: end Chris@1115: Chris@1115: if source_tracker == target_tracker && source_role == target_role Chris@1115: false Chris@1115: else Chris@1115: transaction do Chris@1115: delete_all :tracker_id => target_tracker.id, :role_id => target_role.id Chris@1295: connection.insert "INSERT INTO #{WorkflowRule.table_name} (tracker_id, role_id, old_status_id, new_status_id, author, assignee, field_name, #{connection.quote_column_name 'rule'}, type)" + Chris@1295: " SELECT #{target_tracker.id}, #{target_role.id}, old_status_id, new_status_id, author, assignee, field_name, #{connection.quote_column_name 'rule'}, type" + Chris@1115: " FROM #{WorkflowRule.table_name}" + Chris@1115: " WHERE tracker_id = #{source_tracker.id} AND role_id = #{source_role.id}" Chris@1115: end Chris@1115: true Chris@1115: end Chris@1115: end Chris@1115: end