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