annotate .svn/pristine/2a/2a48d77fce2105e4826cccf7c06db5962a047818.svn-base @ 1613:90bed4e10cc8 deploy

Download file link
author Chris Cannam
date Wed, 30 Aug 2017 17:24:37 +0100
parents dffacf8a6908
children
rev   line source
Chris@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 class Tracker < ActiveRecord::Base
Chris@1517 19
Chris@1517 20 CORE_FIELDS_UNDISABLABLE = %w(project_id tracker_id subject description priority_id is_private).freeze
Chris@1517 21 # Fields that can be disabled
Chris@1517 22 # Other (future) fields should be appended, not inserted!
Chris@1517 23 CORE_FIELDS = %w(assigned_to_id category_id fixed_version_id parent_issue_id start_date due_date estimated_hours done_ratio).freeze
Chris@1517 24 CORE_FIELDS_ALL = (CORE_FIELDS_UNDISABLABLE + CORE_FIELDS).freeze
Chris@1517 25
Chris@1517 26 before_destroy :check_integrity
Chris@1517 27 has_many :issues
Chris@1517 28 has_many :workflow_rules, :dependent => :delete_all do
Chris@1517 29 def copy(source_tracker)
Chris@1517 30 WorkflowRule.copy(source_tracker, nil, proxy_association.owner, nil)
Chris@1517 31 end
Chris@1517 32 end
Chris@1517 33
Chris@1517 34 has_and_belongs_to_many :projects
Chris@1517 35 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@1517 36 acts_as_list
Chris@1517 37
Chris@1517 38 attr_protected :fields_bits
Chris@1517 39
Chris@1517 40 validates_presence_of :name
Chris@1517 41 validates_uniqueness_of :name
Chris@1517 42 validates_length_of :name, :maximum => 30
Chris@1517 43
Chris@1517 44 scope :sorted, lambda { order("#{table_name}.position ASC") }
Chris@1517 45 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
Chris@1517 46
Chris@1517 47 def to_s; name end
Chris@1517 48
Chris@1517 49 def <=>(tracker)
Chris@1517 50 position <=> tracker.position
Chris@1517 51 end
Chris@1517 52
Chris@1517 53 # Returns an array of IssueStatus that are used
Chris@1517 54 # in the tracker's workflows
Chris@1517 55 def issue_statuses
Chris@1517 56 if @issue_statuses
Chris@1517 57 return @issue_statuses
Chris@1517 58 elsif new_record?
Chris@1517 59 return []
Chris@1517 60 end
Chris@1517 61
Chris@1517 62 ids = WorkflowTransition.
Chris@1517 63 connection.select_rows("SELECT DISTINCT old_status_id, new_status_id FROM #{WorkflowTransition.table_name} WHERE tracker_id = #{id} AND type = 'WorkflowTransition'").
Chris@1517 64 flatten.
Chris@1517 65 uniq
Chris@1517 66 @issue_statuses = IssueStatus.where(:id => ids).all.sort
Chris@1517 67 end
Chris@1517 68
Chris@1517 69 def disabled_core_fields
Chris@1517 70 i = -1
Chris@1517 71 @disabled_core_fields ||= CORE_FIELDS.select { i += 1; (fields_bits || 0) & (2 ** i) != 0}
Chris@1517 72 end
Chris@1517 73
Chris@1517 74 def core_fields
Chris@1517 75 CORE_FIELDS - disabled_core_fields
Chris@1517 76 end
Chris@1517 77
Chris@1517 78 def core_fields=(fields)
Chris@1517 79 raise ArgumentError.new("Tracker.core_fields takes an array") unless fields.is_a?(Array)
Chris@1517 80
Chris@1517 81 bits = 0
Chris@1517 82 CORE_FIELDS.each_with_index do |field, i|
Chris@1517 83 unless fields.include?(field)
Chris@1517 84 bits |= 2 ** i
Chris@1517 85 end
Chris@1517 86 end
Chris@1517 87 self.fields_bits = bits
Chris@1517 88 @disabled_core_fields = nil
Chris@1517 89 core_fields
Chris@1517 90 end
Chris@1517 91
Chris@1517 92 # Returns the fields that are disabled for all the given trackers
Chris@1517 93 def self.disabled_core_fields(trackers)
Chris@1517 94 if trackers.present?
Chris@1517 95 trackers.uniq.map(&:disabled_core_fields).reduce(:&)
Chris@1517 96 else
Chris@1517 97 []
Chris@1517 98 end
Chris@1517 99 end
Chris@1517 100
Chris@1517 101 # Returns the fields that are enabled for one tracker at least
Chris@1517 102 def self.core_fields(trackers)
Chris@1517 103 if trackers.present?
Chris@1517 104 trackers.uniq.map(&:core_fields).reduce(:|)
Chris@1517 105 else
Chris@1517 106 CORE_FIELDS.dup
Chris@1517 107 end
Chris@1517 108 end
Chris@1517 109
Chris@1517 110 private
Chris@1517 111 def check_integrity
Chris@1517 112 raise Exception.new("Can't delete tracker") if Issue.where(:tracker_id => self.id).any?
Chris@1517 113 end
Chris@1517 114 end