File: app/models/workflow.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: Workflow#18
inherits from
  Base ( ActiveRecord )
has properties
class method: count_by_tracker_and_role #26
class method: copy / 4 #45
class method: copy_one / 4 #67

Class Hierarchy

Object ( Builtin-Module )
Base ( ActiveRecord )
  Workflow    #18

Code

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