comparison app/models/tracker.rb @ 1338:25603efa57b5

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