Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

tracker.rb 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 < ActiveRecord::Base
  19. include Redmine::SafeAttributes
  20. CORE_FIELDS_UNDISABLABLE = %w(project_id tracker_id subject priority_id 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).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. position <=> tracker.position
  85. end
  86. # Returns an array of IssueStatus that are used
  87. # in the tracker's workflows
  88. def issue_statuses
  89. @issue_statuses ||= IssueStatus.where(:id => issue_status_ids).to_a.sort
  90. end
  91. def issue_status_ids
  92. if new_record?
  93. []
  94. else
  95. @issue_status_ids ||=
  96. WorkflowTransition.where(:tracker_id => id).
  97. distinct.pluck(:old_status_id, :new_status_id).flatten.uniq
  98. end
  99. end
  100. def disabled_core_fields
  101. i = -1
  102. @disabled_core_fields ||=
  103. CORE_FIELDS.select do
  104. i += 1
  105. (fields_bits || 0) & (1 << i) != 0
  106. end
  107. end
  108. def core_fields
  109. CORE_FIELDS - disabled_core_fields
  110. end
  111. def core_fields=(fields)
  112. raise ArgumentError.new("Tracker.core_fields takes an array") unless fields.is_a?(Array)
  113. bits = 0
  114. CORE_FIELDS.each_with_index do |field, i|
  115. unless fields.include?(field)
  116. bits |= 1 << i
  117. end
  118. end
  119. self.fields_bits = bits
  120. @disabled_core_fields = nil
  121. core_fields
  122. end
  123. def copy_workflow_rules(source_tracker)
  124. WorkflowRule.copy(source_tracker, nil, self, nil)
  125. end
  126. # Returns the fields that are disabled for all the given trackers
  127. def self.disabled_core_fields(trackers)
  128. if trackers.present?
  129. trackers.map(&:disabled_core_fields).reduce(:&)
  130. else
  131. []
  132. end
  133. end
  134. # Returns the fields that are enabled for one tracker at least
  135. def self.core_fields(trackers)
  136. if trackers.present?
  137. trackers.uniq.map(&:core_fields).reduce(:|)
  138. else
  139. CORE_FIELDS.dup
  140. end
  141. end
  142. private
  143. def check_integrity
  144. raise "Cannot delete tracker" if Issue.where(:tracker_id => self.id).any?
  145. end
  146. end