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