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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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. CORE_FIELDS_UNDISABLABLE = %w(project_id tracker_id subject description priority_id is_private).freeze
  19. # Fields that can be disabled
  20. # Other (future) fields should be appended, not inserted!
  21. CORE_FIELDS = %w(assigned_to_id category_id fixed_version_id parent_issue_id start_date due_date estimated_hours done_ratio).freeze
  22. CORE_FIELDS_ALL = (CORE_FIELDS_UNDISABLABLE + CORE_FIELDS).freeze
  23. before_destroy :check_integrity
  24. has_many :issues
  25. has_many :workflow_rules, :dependent => :delete_all do
  26. def copy(source_tracker)
  27. WorkflowRule.copy(source_tracker, nil, proxy_association.owner, nil)
  28. end
  29. end
  30. has_and_belongs_to_many :projects
  31. 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'
  32. acts_as_list
  33. attr_protected :fields_bits
  34. validates_presence_of :name
  35. validates_uniqueness_of :name
  36. validates_length_of :name, :maximum => 30
  37. scope :sorted, lambda { order("#{table_name}.position ASC") }
  38. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  39. def to_s; name end
  40. def <=>(tracker)
  41. position <=> tracker.position
  42. end
  43. # Returns an array of IssueStatus that are used
  44. # in the tracker's workflows
  45. def issue_statuses
  46. if @issue_statuses
  47. return @issue_statuses
  48. elsif new_record?
  49. return []
  50. end
  51. ids = WorkflowTransition.
  52. connection.select_rows("SELECT DISTINCT old_status_id, new_status_id FROM #{WorkflowTransition.table_name} WHERE tracker_id = #{id} AND type = 'WorkflowTransition'").
  53. flatten.
  54. uniq
  55. @issue_statuses = IssueStatus.find_all_by_id(ids).sort
  56. end
  57. def disabled_core_fields
  58. i = -1
  59. @disabled_core_fields ||= CORE_FIELDS.select { i += 1; (fields_bits || 0) & (2 ** i) != 0}
  60. end
  61. def core_fields
  62. CORE_FIELDS - disabled_core_fields
  63. end
  64. def core_fields=(fields)
  65. raise ArgumentError.new("Tracker.core_fields takes an array") unless fields.is_a?(Array)
  66. bits = 0
  67. CORE_FIELDS.each_with_index do |field, i|
  68. unless fields.include?(field)
  69. bits |= 2 ** i
  70. end
  71. end
  72. self.fields_bits = bits
  73. @disabled_core_fields = nil
  74. core_fields
  75. end
  76. # Returns the fields that are disabled for all the given trackers
  77. def self.disabled_core_fields(trackers)
  78. if trackers.present?
  79. trackers.uniq.map(&:disabled_core_fields).reduce(:&)
  80. else
  81. []
  82. end
  83. end
  84. # Returns the fields that are enabled for one tracker at least
  85. def self.core_fields(trackers)
  86. if trackers.present?
  87. trackers.uniq.map(&:core_fields).reduce(:|)
  88. else
  89. CORE_FIELDS.dup
  90. end
  91. end
  92. private
  93. def check_integrity
  94. raise Exception.new("Can't delete tracker") if Issue.where(:tracker_id => self.id).any?
  95. end
  96. end