To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / tracker.rb @ 1534:b31caaed9d4d

History | View | Annotate | Download (3.64 KB)

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