annotate app/models/workflow_rule.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents e248c7af89ec
children
rev   line source
Chris@1115 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1115 3 #
Chris@1115 4 # This program is free software; you can redistribute it and/or
Chris@1115 5 # modify it under the terms of the GNU General Public License
Chris@1115 6 # as published by the Free Software Foundation; either version 2
Chris@1115 7 # of the License, or (at your option) any later version.
Chris@1115 8 #
Chris@1115 9 # This program is distributed in the hope that it will be useful,
Chris@1115 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1115 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1115 12 # GNU General Public License for more details.
Chris@1115 13 #
Chris@1115 14 # You should have received a copy of the GNU General Public License
Chris@1115 15 # along with this program; if not, write to the Free Software
Chris@1115 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1115 17
Chris@1115 18 class WorkflowRule < ActiveRecord::Base
Chris@1115 19 self.table_name = "#{table_name_prefix}workflows#{table_name_suffix}"
Chris@1115 20
Chris@1115 21 belongs_to :role
Chris@1115 22 belongs_to :tracker
Chris@1115 23 belongs_to :old_status, :class_name => 'IssueStatus', :foreign_key => 'old_status_id'
Chris@1115 24 belongs_to :new_status, :class_name => 'IssueStatus', :foreign_key => 'new_status_id'
Chris@1115 25
Chris@1115 26 validates_presence_of :role, :tracker, :old_status
Chris@1115 27
Chris@1115 28 # Copies workflows from source to targets
Chris@1115 29 def self.copy(source_tracker, source_role, target_trackers, target_roles)
Chris@1115 30 unless source_tracker.is_a?(Tracker) || source_role.is_a?(Role)
Chris@1115 31 raise ArgumentError.new("source_tracker or source_role must be specified")
Chris@1115 32 end
Chris@1115 33
Chris@1115 34 target_trackers = [target_trackers].flatten.compact
Chris@1115 35 target_roles = [target_roles].flatten.compact
Chris@1115 36
Chris@1115 37 target_trackers = Tracker.sorted.all if target_trackers.empty?
Chris@1115 38 target_roles = Role.all if target_roles.empty?
Chris@1115 39
Chris@1115 40 target_trackers.each do |target_tracker|
Chris@1115 41 target_roles.each do |target_role|
Chris@1115 42 copy_one(source_tracker || target_tracker,
Chris@1115 43 source_role || target_role,
Chris@1115 44 target_tracker,
Chris@1115 45 target_role)
Chris@1115 46 end
Chris@1115 47 end
Chris@1115 48 end
Chris@1115 49
Chris@1115 50 # Copies a single set of workflows from source to target
Chris@1115 51 def self.copy_one(source_tracker, source_role, target_tracker, target_role)
Chris@1115 52 unless source_tracker.is_a?(Tracker) && !source_tracker.new_record? &&
Chris@1115 53 source_role.is_a?(Role) && !source_role.new_record? &&
Chris@1115 54 target_tracker.is_a?(Tracker) && !target_tracker.new_record? &&
Chris@1115 55 target_role.is_a?(Role) && !target_role.new_record?
Chris@1115 56
Chris@1115 57 raise ArgumentError.new("arguments can not be nil or unsaved objects")
Chris@1115 58 end
Chris@1115 59
Chris@1115 60 if source_tracker == target_tracker && source_role == target_role
Chris@1115 61 false
Chris@1115 62 else
Chris@1115 63 transaction do
Chris@1115 64 delete_all :tracker_id => target_tracker.id, :role_id => target_role.id
Chris@1464 65 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@1464 66 " 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 67 " FROM #{WorkflowRule.table_name}" +
Chris@1115 68 " WHERE tracker_id = #{source_tracker.id} AND role_id = #{source_role.id}"
Chris@1115 69 end
Chris@1115 70 true
Chris@1115 71 end
Chris@1115 72 end
Chris@1115 73 end