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