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