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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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 = %w(assigned_to_id category_id fixed_version_id parent_issue_id start_date due_date estimated_hours done_ratio description).freeze
  24. CORE_FIELDS_ALL = (CORE_FIELDS_UNDISABLABLE + CORE_FIELDS).freeze
  25. before_destroy :check_integrity
  26. belongs_to :default_status, :class_name => 'IssueStatus'
  27. has_many :issues
  28. has_many :workflow_rules, :dependent => :delete_all
  29. has_and_belongs_to_many :projects
  30. 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'
  31. acts_as_positioned
  32. validates_presence_of :default_status
  33. validates_presence_of :name
  34. validates_uniqueness_of :name
  35. validates_length_of :name, :maximum => 30
  36. validates_length_of :description, :maximum => 255
  37. scope :sorted, lambda { order(:position) }
  38. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  39. # Returns the trackers that are visible by the user.
  40. #
  41. # Examples:
  42. # project.trackers.visible(user)
  43. # => returns the trackers that are visible by the user in project
  44. #
  45. # Tracker.visible(user)
  46. # => returns the trackers that are visible by the user in at least on project
  47. scope :visible, lambda {|*args|
  48. user = args.shift || User.current
  49. condition = Project.allowed_to_condition(user, :view_issues) do |role, user|
  50. unless role.permissions_all_trackers?(:view_issues)
  51. tracker_ids = role.permissions_tracker_ids(:view_issues)
  52. if tracker_ids.any?
  53. "#{Tracker.table_name}.id IN (#{tracker_ids.join(',')})"
  54. else
  55. '1=0'
  56. end
  57. end
  58. end
  59. joins(:projects).where(condition).distinct
  60. }
  61. safe_attributes 'name',
  62. 'default_status_id',
  63. 'is_in_roadmap',
  64. 'core_fields',
  65. 'position',
  66. 'custom_field_ids',
  67. 'project_ids',
  68. 'description'
  69. def to_s; name end
  70. def <=>(tracker)
  71. position <=> tracker.position
  72. end
  73. # Returns an array of IssueStatus that are used
  74. # in the tracker's workflows
  75. def issue_statuses
  76. @issue_statuses ||= IssueStatus.where(:id => issue_status_ids).to_a.sort
  77. end
  78. def issue_status_ids
  79. if new_record?
  80. []
  81. else
  82. @issue_status_ids ||= WorkflowTransition.where(:tracker_id => id).distinct.pluck(:old_status_id, :new_status_id).flatten.uniq
  83. end
  84. end
  85. def disabled_core_fields
  86. i = -1
  87. @disabled_core_fields ||= CORE_FIELDS.select { i += 1; (fields_bits || 0) & (2 ** i) != 0}
  88. end
  89. def core_fields
  90. CORE_FIELDS - disabled_core_fields
  91. end
  92. def core_fields=(fields)
  93. raise ArgumentError.new("Tracker.core_fields takes an array") unless fields.is_a?(Array)
  94. bits = 0
  95. CORE_FIELDS.each_with_index do |field, i|
  96. unless fields.include?(field)
  97. bits |= 2 ** i
  98. end
  99. end
  100. self.fields_bits = bits
  101. @disabled_core_fields = nil
  102. core_fields
  103. end
  104. def copy_workflow_rules(source_tracker)
  105. WorkflowRule.copy(source_tracker, nil, self, nil)
  106. end
  107. # Returns the fields that are disabled for all the given trackers
  108. def self.disabled_core_fields(trackers)
  109. if trackers.present?
  110. trackers.map(&:disabled_core_fields).reduce(:&)
  111. else
  112. []
  113. end
  114. end
  115. # Returns the fields that are enabled for one tracker at least
  116. def self.core_fields(trackers)
  117. if trackers.present?
  118. trackers.uniq.map(&:core_fields).reduce(:|)
  119. else
  120. CORE_FIELDS.dup
  121. end
  122. end
  123. private
  124. def check_integrity
  125. raise "Cannot delete tracker" if Issue.where(:tracker_id => self.id).any?
  126. end
  127. end