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_priority.rb 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 IssuePriority < Enumeration
  19. has_many :issues, :foreign_key => 'priority_id'
  20. after_destroy {|priority| priority.class.compute_position_names}
  21. after_save do |priority|
  22. if (priority.saved_change_to_position? && priority.position) ||
  23. priority.saved_change_to_active? || priority.saved_change_to_is_default?
  24. priority.class.compute_position_names
  25. end
  26. end
  27. OptionName = :enumeration_issue_priorities
  28. def option_name
  29. OptionName
  30. end
  31. def objects_count
  32. issues.count
  33. end
  34. def transfer_relations(to)
  35. issues.update_all(:priority_id => to.id)
  36. end
  37. def css_classes
  38. "priority-#{id} priority-#{position_name}"
  39. end
  40. # Clears position_name for all priorities
  41. # Called from migration 20121026003537_populate_enumerations_position_name
  42. def self.clear_position_names
  43. update_all :position_name => nil
  44. end
  45. def self.default_or_middle
  46. default || begin
  47. priorities = active
  48. priorities[(priorities.size - 1) / 2]
  49. end
  50. end
  51. def high?
  52. position > self.class.default_or_middle.position
  53. end
  54. def low?
  55. position < self.class.default_or_middle.position
  56. end
  57. # Updates position_name for active priorities
  58. # Called from migration 20121026003537_populate_enumerations_position_name
  59. def self.compute_position_names
  60. priorities = active
  61. if priorities.any?
  62. default_position = default_or_middle.position
  63. priorities.each_with_index do |priority, index|
  64. name =
  65. case
  66. when priority.position == default_position
  67. "default"
  68. when priority.position < default_position
  69. index == 0 ? "lowest" : "low#{index+1}"
  70. else
  71. index == (priorities.size - 1) ? "highest" : "high#{priorities.size - index}"
  72. end
  73. where(:id => priority.id).update_all({:position_name => name})
  74. end
  75. end
  76. end
  77. end