diff --git a/app/models/issue.rb b/app/models/issue.rb index 9c8123a4d..a2ba73d04 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -906,7 +906,7 @@ class Issue < ActiveRecord::Base # Returns a string of css classes that apply to the issue def css_classes - s = "issue status-#{status_id} priority-#{priority_id}" + s = "issue status-#{status_id} #{priority.try(:css_classes)}" s << ' closed' if closed? s << ' overdue' if overdue? s << ' child' if child? diff --git a/app/models/issue_priority.rb b/app/models/issue_priority.rb index 058b33fc8..d5b7669b3 100644 --- a/app/models/issue_priority.rb +++ b/app/models/issue_priority.rb @@ -18,6 +18,9 @@ class IssuePriority < Enumeration has_many :issues, :foreign_key => 'priority_id' + after_destroy {|priority| priority.class.compute_position_names} + after_save {|priority| priority.class.compute_position_names if priority.position_changed? && priority.position} + OptionName = :enumeration_issue_priorities def option_name @@ -31,4 +34,35 @@ class IssuePriority < Enumeration def transfer_relations(to) issues.update_all("priority_id = #{to.id}") end + + def css_classes + "priority-#{id} priority-#{position_name}" + end + + # Clears position_name for all priorities + # Called from migration 20121026003537_populate_enumerations_position_name + def self.clear_position_names + update_all :position_name => nil + end + + # Updates position_name for active priorities + # Called from migration 20121026003537_populate_enumerations_position_name + def self.compute_position_names + priorities = where(:active => true).all.sort_by(&:position) + if priorities.any? + default = priorities.detect(&:is_default?) || priorities[(priorities.size - 1) / 2] + priorities.each_with_index do |priority, index| + name = case + when priority.position == default.position + "default" + when priority.position < default.position + index == 0 ? "lowest" : "low#{index+1}" + else + index == (priorities.size - 1) ? "highest" : "high#{priorities.size - index}" + end + + update_all({:position_name => name}, :id => priority.id) + end + end + end end diff --git a/db/migrate/20121026002032_add_enumerations_position_name.rb b/db/migrate/20121026002032_add_enumerations_position_name.rb new file mode 100644 index 000000000..52cbe08eb --- /dev/null +++ b/db/migrate/20121026002032_add_enumerations_position_name.rb @@ -0,0 +1,9 @@ +class AddEnumerationsPositionName < ActiveRecord::Migration + def up + add_column :enumerations, :position_name, :string, :limit => 30 + end + + def down + remove_column :enumerations, :position_name + end +end diff --git a/db/migrate/20121026003537_populate_enumerations_position_name.rb b/db/migrate/20121026003537_populate_enumerations_position_name.rb new file mode 100644 index 000000000..31777b75f --- /dev/null +++ b/db/migrate/20121026003537_populate_enumerations_position_name.rb @@ -0,0 +1,9 @@ +class PopulateEnumerationsPositionName < ActiveRecord::Migration + def up + IssuePriority.compute_position_names + end + + def down + IssuePriority.clear_position_names + end +end diff --git a/test/fixtures/enumerations.yml b/test/fixtures/enumerations.yml index 2c4c82e90..23ea2b3aa 100644 --- a/test/fixtures/enumerations.yml +++ b/test/fixtures/enumerations.yml @@ -23,6 +23,7 @@ enumerations_004: type: IssuePriority active: true position: 1 + position_name: lowest enumerations_005: name: Normal id: 5 @@ -30,24 +31,28 @@ enumerations_005: is_default: true active: true position: 2 + position_name: default enumerations_006: name: High id: 6 type: IssuePriority active: true position: 3 + position_name: high3 enumerations_007: name: Urgent id: 7 type: IssuePriority active: true position: 4 + position_name: high2 enumerations_008: name: Immediate id: 8 type: IssuePriority active: true position: 5 + position_name: highest enumerations_009: name: Design id: 9 diff --git a/test/unit/issue_priority_test.rb b/test/unit/issue_priority_test.rb index 96d58196c..642203301 100644 --- a/test/unit/issue_priority_test.rb +++ b/test/unit/issue_priority_test.rb @@ -73,4 +73,34 @@ class IssuePriorityTest < ActiveSupport::TestCase priority.expects(:reset_positions_in_list).once priority.move_to = 'higher' end + + def test_clear_position_names_should_set_position_names_to_nil + IssuePriority.clear_position_names + assert IssuePriority.all.all? {|priority| priority.position_name.nil?} + end + + def test_compute_position_names_with_default_priority + IssuePriority.clear_position_names + + IssuePriority.compute_position_names + assert_equal %w(lowest default high3 high2 highest), IssuePriority.active.all.sort.map(&:position_name) + end + + def test_compute_position_names_without_default_priority_should_split_priorities + IssuePriority.clear_position_names + IssuePriority.update_all :is_default => false + + IssuePriority.compute_position_names + assert_equal %w(lowest low2 default high2 highest), IssuePriority.active.all.sort.map(&:position_name) + end + + def test_adding_a_priority_should_update_position_names + priority = IssuePriority.create!(:name => 'New') + assert_equal %w(lowest default high4 high3 high2 highest), IssuePriority.active.all.sort.map(&:position_name) + end + + def test_destroying_a_priority_should_update_position_names + IssuePriority.find_by_position_name('highest').destroy + assert_equal %w(lowest default high2 highest), IssuePriority.active.all.sort.map(&:position_name) + end end diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index 0c3085bb7..15e79bec4 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -1821,4 +1821,11 @@ class IssueTest < ActiveSupport::TestCase def test_journals_after_with_blank_arg_should_return_all_journals assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('') end + + def test_css_classes_should_include_priority + issue = Issue.new(:priority => IssuePriority.find(8)) + classes = issue.css_classes.split(' ') + assert_include 'priority-8', classes + assert_include 'priority-highest', classes + end end