You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tracker.rb 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class Tracker < ApplicationRecord
  19. include Redmine::SafeAttributes
  20. CORE_FIELDS_UNDISABLABLE = %w(project_id tracker_id subject is_private).freeze
  21. # Fields that can be disabled
  22. # Other (future) fields should be appended, not inserted!
  23. CORE_FIELDS =
  24. %w(assigned_to_id category_id fixed_version_id parent_issue_id
  25. start_date due_date estimated_hours done_ratio description priority_id).freeze
  26. CORE_FIELDS_ALL = (CORE_FIELDS_UNDISABLABLE + CORE_FIELDS).freeze
  27. before_destroy :check_integrity
  28. belongs_to :default_status, :class_name => 'IssueStatus'
  29. has_many :issues
  30. has_many :workflow_rules, :dependent => :delete_all
  31. has_and_belongs_to_many :projects
  32. has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField',
  33. :join_table => "#{table_name_prefix}custom_fields_trackers#{table_name_suffix}",
  34. :association_foreign_key => 'custom_field_id'
  35. acts_as_positioned
  36. validates_presence_of :default_status
  37. validates_presence_of :name
  38. validates_uniqueness_of :name, :case_sensitive => true
  39. validates_length_of :name, :maximum => 30
  40. validates_length_of :description, :maximum => 255
  41. scope :sorted, lambda {order(:position)}
  42. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  43. # Returns the trackers that are visible by the user.
  44. #
  45. # Examples:
  46. # project.trackers.visible(user)
  47. # => returns the trackers that are visible by the user in project
  48. #
  49. # Tracker.visible(user)
  50. # => returns the trackers that are visible by the user in at least on project
  51. scope :visible, (lambda do |*args|
  52. user = args.shift || User.current
  53. condition = Project.allowed_to_condition(user, :view_issues) do |role, user|
  54. unless role.permissions_all_trackers?(:view_issues)
  55. tracker_ids = role.permissions_tracker_ids(:view_issues)
  56. if tracker_ids.any?
  57. "#{Tracker.table_name}.id IN (#{tracker_ids.join(',')})"
  58. else
  59. '1=0'
  60. end
  61. end
  62. end
  63. joins(:projects).where(condition).distinct
  64. end)
  65. safe_attributes(
  66. 'name',
  67. 'default_status_id',
  68. 'is_in_roadmap',
  69. 'core_fields',
  70. 'position',
  71. 'custom_field_ids',
  72. 'project_ids',
  73. 'description')
  74. def copy_from(arg, options={})
  75. return if arg.blank?
  76. tracker = arg.is_a?(Tracker) ? arg : Tracker.find_by_id(arg.to_s)
  77. self.attributes = tracker.attributes.dup.except("id", "name", "position")
  78. self.custom_field_ids = tracker.custom_field_ids.dup
  79. self.project_ids = tracker.project_ids.dup
  80. self
  81. end
  82. def to_s; name end
  83. def <=>(tracker)
  84. return nil unless tracker.is_a?(Tracker)
  85. position <=> tracker.position
  86. end
  87. # Returns an array of IssueStatus that are used
  88. # in the tracker's workflows
  89. def issue_statuses
  90. @issue_statuses ||= IssueStatus.where(:id => issue_status_ids).to_a.sort
  91. end
  92. def issue_status_ids
  93. if new_record?
  94. []
  95. else
  96. @issue_status_ids ||=
  97. WorkflowTransition.where(:tracker_id => id).
  98. where('old_status_id <> new_status_id').
  99. distinct.pluck(:old_status_id, :new_status_id).flatten.uniq
  100. end
  101. end
  102. def disabled_core_fields
  103. i = -1
  104. @disabled_core_fields ||=
  105. CORE_FIELDS.select do
  106. i += 1
  107. (fields_bits || 0) & (1 << i) != 0
  108. end
  109. end
  110. def core_fields
  111. CORE_FIELDS - disabled_core_fields
  112. end
  113. def core_fields=(fields)
  114. raise ArgumentError.new("Tracker.core_fields takes an array") unless fields.is_a?(Array)
  115. bits = 0
  116. CORE_FIELDS.each_with_index do |field, i|
  117. unless fields.include?(field)
  118. bits |= 1 << i
  119. end
  120. end
  121. self.fields_bits = bits
  122. @disabled_core_fields = nil
  123. core_fields
  124. end
  125. def copy_workflow_rules(source_tracker)
  126. WorkflowRule.copy(source_tracker, nil, self, nil)
  127. end
  128. # Returns the fields that are disabled for all the given trackers
  129. def self.disabled_core_fields(trackers)
  130. if trackers.present?
  131. trackers.map(&:disabled_core_fields).reduce(:&)
  132. else
  133. []
  134. end
  135. end
  136. # Returns the fields that are enabled for one tracker at least
  137. def self.core_fields(trackers)
  138. if trackers.present?
  139. trackers.uniq.map(&:core_fields).reduce(:|)
  140. else
  141. CORE_FIELDS.dup
  142. end
  143. end
  144. private
  145. def check_integrity
  146. raise "Cannot delete tracker" if Issue.where(:tracker_id => self.id).any?
  147. end
  148. end