Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class Tracker < ActiveRecord::Base Chris@0: before_destroy :check_integrity Chris@0: has_many :issues Chris@0: has_many :workflows, :dependent => :delete_all do Chris@0: def copy(source_tracker) Chris@0: Workflow.copy(source_tracker, nil, proxy_owner, nil) Chris@0: end Chris@0: end Chris@0: Chris@0: has_and_belongs_to_many :projects Chris@0: 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' Chris@0: acts_as_list Chris@0: Chris@0: validates_presence_of :name Chris@0: validates_uniqueness_of :name Chris@0: validates_length_of :name, :maximum => 30 Chris@0: Chris@507: named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} Chris@507: Chris@0: def to_s; name end Chris@0: Chris@0: def <=>(tracker) Chris@0: name <=> tracker.name Chris@0: end Chris@0: Chris@0: def self.all Chris@0: find(:all, :order => 'position') Chris@0: end Chris@0: Chris@0: # Returns an array of IssueStatus that are used Chris@0: # in the tracker's workflows Chris@0: def issue_statuses Chris@0: if @issue_statuses Chris@0: return @issue_statuses Chris@0: elsif new_record? Chris@0: return [] Chris@0: end Chris@0: Chris@0: ids = Workflow. Chris@0: connection.select_rows("SELECT DISTINCT old_status_id, new_status_id FROM #{Workflow.table_name} WHERE tracker_id = #{id}"). Chris@0: flatten. Chris@0: uniq Chris@0: Chris@0: @issue_statuses = IssueStatus.find_all_by_id(ids).sort Chris@0: end Chris@0: Chris@0: private Chris@0: def check_integrity Chris@0: raise "Can't delete tracker" if Issue.find(:first, :conditions => ["tracker_id=?", self.id]) Chris@0: end Chris@0: end