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.

issue_relation.rb 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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 IssueRelation < ActiveRecord::Base
  18. belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
  19. belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
  20. TYPE_RELATES = "relates"
  21. TYPE_DUPLICATES = "duplicates"
  22. TYPE_DUPLICATED = "duplicated"
  23. TYPE_BLOCKS = "blocks"
  24. TYPE_BLOCKED = "blocked"
  25. TYPE_PRECEDES = "precedes"
  26. TYPE_FOLLOWS = "follows"
  27. TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES },
  28. TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED },
  29. TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
  30. TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED },
  31. TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
  32. TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS },
  33. TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }
  34. }.freeze
  35. validates_presence_of :issue_from, :issue_to, :relation_type
  36. validates_inclusion_of :relation_type, :in => TYPES.keys
  37. validates_numericality_of :delay, :allow_nil => true
  38. validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
  39. validate :validate_issue_relation
  40. attr_protected :issue_from_id, :issue_to_id
  41. before_save :handle_issue_order
  42. def visible?(user=User.current)
  43. (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
  44. end
  45. def deletable?(user=User.current)
  46. visible?(user) &&
  47. ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
  48. (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
  49. end
  50. def initialize(attributes=nil, *args)
  51. super
  52. if new_record?
  53. if relation_type.blank?
  54. self.relation_type = IssueRelation::TYPE_RELATES
  55. end
  56. end
  57. end
  58. def validate_issue_relation
  59. if issue_from && issue_to
  60. errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
  61. errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
  62. #detect circular dependencies depending wether the relation should be reversed
  63. if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
  64. errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
  65. else
  66. errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
  67. end
  68. errors.add :base, :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
  69. end
  70. end
  71. def other_issue(issue)
  72. (self.issue_from_id == issue.id) ? issue_to : issue_from
  73. end
  74. # Returns the relation type for +issue+
  75. def relation_type_for(issue)
  76. if TYPES[relation_type]
  77. if self.issue_from_id == issue.id
  78. relation_type
  79. else
  80. TYPES[relation_type][:sym]
  81. end
  82. end
  83. end
  84. def label_for(issue)
  85. TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
  86. end
  87. def handle_issue_order
  88. reverse_if_needed
  89. if TYPE_PRECEDES == relation_type
  90. self.delay ||= 0
  91. else
  92. self.delay = nil
  93. end
  94. set_issue_to_dates
  95. end
  96. def set_issue_to_dates
  97. soonest_start = self.successor_soonest_start
  98. if soonest_start && issue_to
  99. issue_to.reschedule_after(soonest_start)
  100. end
  101. end
  102. def successor_soonest_start
  103. if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date)
  104. (issue_from.due_date || issue_from.start_date) + 1 + delay
  105. end
  106. end
  107. def <=>(relation)
  108. TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
  109. end
  110. private
  111. # Reverses the relation if needed so that it gets stored in the proper way
  112. # Should not be reversed before validation so that it can be displayed back
  113. # as entered on new relation form
  114. def reverse_if_needed
  115. if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
  116. issue_tmp = issue_to
  117. self.issue_to = issue_from
  118. self.issue_from = issue_tmp
  119. self.relation_type = TYPES[relation_type][:reverse]
  120. end
  121. end
  122. end