Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: class IssuePriority < Enumeration Chris@1494: has_many :issues, :foreign_key => 'priority_id' Chris@1494: Chris@1494: after_destroy {|priority| priority.class.compute_position_names} Chris@1494: after_save {|priority| priority.class.compute_position_names if priority.position_changed? && priority.position} Chris@1494: Chris@1494: OptionName = :enumeration_issue_priorities Chris@1494: Chris@1494: def option_name Chris@1494: OptionName Chris@1494: end Chris@1494: Chris@1494: def objects_count Chris@1494: issues.count Chris@1494: end Chris@1494: Chris@1494: def transfer_relations(to) Chris@1494: issues.update_all("priority_id = #{to.id}") Chris@1494: end Chris@1494: Chris@1494: def css_classes Chris@1494: "priority-#{id} priority-#{position_name}" Chris@1494: end Chris@1494: Chris@1494: # Clears position_name for all priorities Chris@1494: # Called from migration 20121026003537_populate_enumerations_position_name Chris@1494: def self.clear_position_names Chris@1494: update_all :position_name => nil Chris@1494: end Chris@1494: Chris@1494: # Updates position_name for active priorities Chris@1494: # Called from migration 20121026003537_populate_enumerations_position_name Chris@1494: def self.compute_position_names Chris@1494: priorities = where(:active => true).all.sort_by(&:position) Chris@1494: if priorities.any? Chris@1494: default = priorities.detect(&:is_default?) || priorities[(priorities.size - 1) / 2] Chris@1494: priorities.each_with_index do |priority, index| Chris@1494: name = case Chris@1494: when priority.position == default.position Chris@1494: "default" Chris@1494: when priority.position < default.position Chris@1494: index == 0 ? "lowest" : "low#{index+1}" Chris@1494: else Chris@1494: index == (priorities.size - 1) ? "highest" : "high#{priorities.size - index}" Chris@1494: end Chris@1494: Chris@1494: update_all({:position_name => name}, :id => priority.id) Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end