Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: class Tracker < ActiveRecord::Base Chris@1494: Chris@1494: CORE_FIELDS_UNDISABLABLE = %w(project_id tracker_id subject description priority_id is_private).freeze Chris@1494: # Fields that can be disabled Chris@1494: # Other (future) fields should be appended, not inserted! Chris@1494: CORE_FIELDS = %w(assigned_to_id category_id fixed_version_id parent_issue_id start_date due_date estimated_hours done_ratio).freeze Chris@1494: CORE_FIELDS_ALL = (CORE_FIELDS_UNDISABLABLE + CORE_FIELDS).freeze Chris@1494: Chris@1494: before_destroy :check_integrity Chris@1494: has_many :issues Chris@1494: has_many :workflow_rules, :dependent => :delete_all do Chris@1494: def copy(source_tracker) Chris@1494: WorkflowRule.copy(source_tracker, nil, proxy_association.owner, nil) Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: has_and_belongs_to_many :projects Chris@1494: 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@1494: acts_as_list Chris@1494: Chris@1494: attr_protected :fields_bits Chris@1494: Chris@1494: validates_presence_of :name Chris@1494: validates_uniqueness_of :name Chris@1494: validates_length_of :name, :maximum => 30 Chris@1494: Chris@1494: scope :sorted, lambda { order("#{table_name}.position ASC") } Chris@1494: scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} Chris@1494: Chris@1494: def to_s; name end Chris@1494: Chris@1494: def <=>(tracker) Chris@1494: position <=> tracker.position Chris@1494: end Chris@1494: Chris@1494: # Returns an array of IssueStatus that are used Chris@1494: # in the tracker's workflows Chris@1494: def issue_statuses Chris@1494: if @issue_statuses Chris@1494: return @issue_statuses Chris@1494: elsif new_record? Chris@1494: return [] Chris@1494: end Chris@1494: Chris@1494: ids = WorkflowTransition. Chris@1494: connection.select_rows("SELECT DISTINCT old_status_id, new_status_id FROM #{WorkflowTransition.table_name} WHERE tracker_id = #{id} AND type = 'WorkflowTransition'"). Chris@1494: flatten. Chris@1494: uniq Chris@1494: Chris@1494: @issue_statuses = IssueStatus.find_all_by_id(ids).sort Chris@1494: end Chris@1494: Chris@1494: def disabled_core_fields Chris@1494: i = -1 Chris@1494: @disabled_core_fields ||= CORE_FIELDS.select { i += 1; (fields_bits || 0) & (2 ** i) != 0} Chris@1494: end Chris@1494: Chris@1494: def core_fields Chris@1494: CORE_FIELDS - disabled_core_fields Chris@1494: end Chris@1494: Chris@1494: def core_fields=(fields) Chris@1494: raise ArgumentError.new("Tracker.core_fields takes an array") unless fields.is_a?(Array) Chris@1494: Chris@1494: bits = 0 Chris@1494: CORE_FIELDS.each_with_index do |field, i| Chris@1494: unless fields.include?(field) Chris@1494: bits |= 2 ** i Chris@1494: end Chris@1494: end Chris@1494: self.fields_bits = bits Chris@1494: @disabled_core_fields = nil Chris@1494: core_fields Chris@1494: end Chris@1494: Chris@1494: # Returns the fields that are disabled for all the given trackers Chris@1494: def self.disabled_core_fields(trackers) Chris@1494: if trackers.present? Chris@1494: trackers.uniq.map(&:disabled_core_fields).reduce(:&) Chris@1494: else Chris@1494: [] Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # Returns the fields that are enabled for one tracker at least Chris@1494: def self.core_fields(trackers) Chris@1494: if trackers.present? Chris@1494: trackers.uniq.map(&:core_fields).reduce(:|) Chris@1494: else Chris@1494: CORE_FIELDS.dup Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: private Chris@1494: def check_integrity Chris@1494: raise Exception.new("Can't delete tracker") if Issue.where(:tracker_id => self.id).any? Chris@1494: end Chris@1494: end