tracker.rb 1.73 KB
Newer Older
1
#-- encoding: UTF-8
2 3
#-- copyright
# ChiliProject is a project management system.
4
#
Holger Just's avatar
Holger Just committed
5
# Copyright (C) 2010-2013 the ChiliProject Team
6
#
7 8 9 10
# 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.
11
#
12 13 14
# See doc/COPYRIGHT.rdoc for more details.
#++

15
class Tracker < ActiveRecord::Base
16
  before_destroy :check_integrity
17
  has_many :issues
Jean-Philippe Lang's avatar
Jean-Philippe Lang committed
18
  has_many :workflows, :dependent => :delete_all do
19 20
    def copy(source_tracker)
      Workflow.copy(source_tracker, nil, proxy_owner, nil)
Jean-Philippe Lang's avatar
Jean-Philippe Lang committed
21 22
    end
  end
23

24
  has_and_belongs_to_many :projects
25
  has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_trackers#{table_name_suffix}", :association_foreign_key => 'custom_field_id'
26 27 28 29
  acts_as_list

  validates_presence_of :name
  validates_uniqueness_of :name
30
  validates_length_of :name, :maximum => 30
31

32
  def to_s; name end
33

34 35 36 37
  def <=>(tracker)
    name <=> tracker.name
  end

38 39 40
  def self.all
    find(:all, :order => 'position')
  end
41

42 43 44 45
  # Returns an array of IssueStatus that are used
  # in the tracker's workflows
  def issue_statuses
    if @issue_statuses
46
      return @issue_statuses
47 48 49
    elsif new_record?
      return []
    end
50

51 52 53 54
    ids = Workflow.
            connection.select_rows("SELECT DISTINCT old_status_id, new_status_id FROM #{Workflow.table_name} WHERE tracker_id = #{id}").
            flatten.
            uniq
55

56 57
    @issue_statuses = IssueStatus.find_all_by_id(ids).sort
  end
58

59 60 61
private
  def check_integrity
    raise "Can't delete tracker" if Issue.find(:first, :conditions => ["tracker_id=?", self.id])
Jean-Philippe Lang's avatar
Jean-Philippe Lang committed
62 63
  end
end