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

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