Class: Workflow

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/workflow.rb

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

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.

See doc/COPYRIGHT.rdoc for more details. ++

Class Method Summary (collapse)

Class Method Details

+ (Object) available_statuses(project, user = User.current)

Find potential statuses the user could be allowed to switch issues to



42
43
44
45
46
47
48
49
50
# File 'app/models/workflow.rb', line 42

def self.available_statuses(project, user=User.current)
  Workflow.find(:all,
                :include => :new_status,
                :conditions => {:role_id => user.roles_for_project(project).collect(&:id)}).
    collect(&:new_status).
    compact.
    uniq.
    sort
end

+ (Object) copy(source_tracker, source_role, target_trackers, target_roles)

Copies workflows from source to targets



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/workflow.rb', line 53

def self.copy(source_tracker, source_role, target_trackers, target_roles)
  unless source_tracker.is_a?(Tracker) || source_role.is_a?(Role)
    raise ArgumentError.new("source_tracker or source_role must be specified")
  end

  target_trackers = [target_trackers].flatten.compact
  target_roles = [target_roles].flatten.compact

  target_trackers = Tracker.all if target_trackers.empty?
  target_roles = Role.all if target_roles.empty?

  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

+ (Object) copy_one(source_tracker, source_role, target_tracker, target_role)

Copies a single set of workflows from source to target



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/workflow.rb', line 75

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?

    raise ArgumentError.new("arguments can not be nil or unsaved objects")
  end

  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
      connection.insert "INSERT INTO #{Workflow.table_name} (tracker_id, role_id, old_status_id, new_status_id, author, assignee)" +
                        " SELECT #{target_tracker.id}, #{target_role.id}, old_status_id, new_status_id, author, assignee" +
                        " FROM #{Workflow.table_name}" +
                        " WHERE tracker_id = #{source_tracker.id} AND role_id = #{source_role.id}"
    end
    true
  end
end

+ (Object) count_by_tracker_and_role

Returns workflow transitions count by tracker and role



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/workflow.rb', line 23

def self.count_by_tracker_and_role
  counts = connection.select_all("SELECT role_id, tracker_id, count(id) AS c FROM #{Workflow.table_name} GROUP BY role_id, tracker_id")
  roles = Role.find(:all, :order => 'builtin, position')
  trackers = Tracker.find(:all, :order => 'position')

  result = []
  trackers.each do |tracker|
    t = []
    roles.each do |role|
      row = counts.detect {|c| c['role_id'].to_s == role.id.to_s && c['tracker_id'].to_s == tracker.id.to_s}
      t << [role, (row.nil? ? 0 : row['c'].to_i)]
    end
    result << [tracker, t]
  end

  result
end