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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 IssuePriority < Enumeration
  18. has_many :issues, :foreign_key => 'priority_id'
  19. after_destroy {|priority| priority.class.compute_position_names}
  20. 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?}
  21. OptionName = :enumeration_issue_priorities
  22. def option_name
  23. OptionName
  24. end
  25. def objects_count
  26. issues.count
  27. end
  28. def transfer_relations(to)
  29. issues.update_all(:priority_id => to.id)
  30. end
  31. def css_classes
  32. "priority-#{id} priority-#{position_name}"
  33. end
  34. # Clears position_name for all priorities
  35. # Called from migration 20121026003537_populate_enumerations_position_name
  36. def self.clear_position_names
  37. update_all :position_name => nil
  38. end
  39. # Updates position_name for active priorities
  40. # Called from migration 20121026003537_populate_enumerations_position_name
  41. def self.compute_position_names
  42. priorities = where(:active => true).sort_by(&:position)
  43. if priorities.any?
  44. default = priorities.detect(&:is_default?) || priorities[(priorities.size - 1) / 2]
  45. priorities.each_with_index do |priority, index|
  46. name = case
  47. when priority.position == default.position
  48. "default"
  49. when priority.position < default.position
  50. index == 0 ? "lowest" : "low#{index+1}"
  51. else
  52. index == (priorities.size - 1) ? "highest" : "high#{priorities.size - index}"
  53. end
  54. where(:id => priority.id).update_all({:position_name => name})
  55. end
  56. end
  57. end
  58. end