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.5KB

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