Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adds a textual css class for issue priorities (#12216).

git-svn-id: svn://rubyforge.org/var/svn/redmine/trunk@10720 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information...
commit 5c2a0af70898e55bbc047a1e761b79d99e0f1215 1 parent 43daaec
authored October 26, 2012
2  app/models/issue.rb
@@ -906,7 +906,7 @@ def to_s
906 906
 
907 907
   # Returns a string of css classes that apply to the issue
908 908
   def css_classes
909  
-    s = "issue status-#{status_id} priority-#{priority_id}"
  909
+    s = "issue status-#{status_id} #{priority.try(:css_classes)}"
910 910
     s << ' closed' if closed?
911 911
     s << ' overdue' if overdue?
912 912
     s << ' child' if child?
34  app/models/issue_priority.rb
@@ -18,6 +18,9 @@
18 18
 class IssuePriority < Enumeration
19 19
   has_many :issues, :foreign_key => 'priority_id'
20 20
 
  21
+  after_destroy {|priority| priority.class.compute_position_names}
  22
+  after_save {|priority| priority.class.compute_position_names if priority.position_changed? && priority.position}
  23
+
21 24
   OptionName = :enumeration_issue_priorities
22 25
 
23 26
   def option_name
@@ -31,4 +34,35 @@ def objects_count
31 34
   def transfer_relations(to)
32 35
     issues.update_all("priority_id = #{to.id}")
33 36
   end
  37
+
  38
+  def css_classes
  39
+    "priority-#{id} priority-#{position_name}"
  40
+  end
  41
+
  42
+  # Clears position_name for all priorities
  43
+  # Called from migration 20121026003537_populate_enumerations_position_name
  44
+  def self.clear_position_names
  45
+    update_all :position_name => nil
  46
+  end
  47
+
  48
+  # Updates position_name for active priorities
  49
+  # Called from migration 20121026003537_populate_enumerations_position_name
  50
+  def self.compute_position_names
  51
+    priorities = where(:active => true).all.sort_by(&:position)
  52
+    if priorities.any?
  53
+      default = priorities.detect(&:is_default?) || priorities[(priorities.size - 1) / 2]
  54
+      priorities.each_with_index do |priority, index|
  55
+        name = case
  56
+          when priority.position == default.position
  57
+            "default"
  58
+          when priority.position < default.position
  59
+            index == 0 ? "lowest" : "low#{index+1}"
  60
+          else
  61
+            index == (priorities.size - 1) ? "highest" : "high#{priorities.size - index}"
  62
+          end
  63
+
  64
+        update_all({:position_name => name}, :id => priority.id)
  65
+      end
  66
+    end
  67
+  end
34 68
 end
9  db/migrate/20121026002032_add_enumerations_position_name.rb
... ...
@@ -0,0 +1,9 @@
  1
+class AddEnumerationsPositionName < ActiveRecord::Migration
  2
+  def up
  3
+    add_column :enumerations, :position_name, :string, :limit => 30
  4
+  end
  5
+
  6
+  def down
  7
+    remove_column :enumerations, :position_name
  8
+  end
  9
+end
9  db/migrate/20121026003537_populate_enumerations_position_name.rb
... ...
@@ -0,0 +1,9 @@
  1
+class PopulateEnumerationsPositionName < ActiveRecord::Migration
  2
+  def up
  3
+    IssuePriority.compute_position_names
  4
+  end
  5
+
  6
+  def down
  7
+    IssuePriority.clear_position_names
  8
+  end
  9
+end
5  test/fixtures/enumerations.yml
@@ -23,6 +23,7 @@ enumerations_004:
23 23
   type: IssuePriority
24 24
   active: true
25 25
   position: 1
  26
+  position_name: lowest
26 27
 enumerations_005: 
27 28
   name: Normal
28 29
   id: 5
@@ -30,24 +31,28 @@ enumerations_005:
30 31
   is_default: true
31 32
   active: true
32 33
   position: 2
  34
+  position_name: default
33 35
 enumerations_006: 
34 36
   name: High
35 37
   id: 6
36 38
   type: IssuePriority
37 39
   active: true
38 40
   position: 3
  41
+  position_name: high3
39 42
 enumerations_007: 
40 43
   name: Urgent
41 44
   id: 7
42 45
   type: IssuePriority
43 46
   active: true
44 47
   position: 4
  48
+  position_name: high2
45 49
 enumerations_008: 
46 50
   name: Immediate
47 51
   id: 8
48 52
   type: IssuePriority
49 53
   active: true
50 54
   position: 5
  55
+  position_name: highest
51 56
 enumerations_009: 
52 57
   name: Design
53 58
   id: 9
30  test/unit/issue_priority_test.rb
@@ -73,4 +73,34 @@ def test_moving_in_list_should_reset_positions
73 73
     priority.expects(:reset_positions_in_list).once
74 74
     priority.move_to = 'higher'
75 75
   end
  76
+
  77
+  def test_clear_position_names_should_set_position_names_to_nil
  78
+    IssuePriority.clear_position_names
  79
+    assert IssuePriority.all.all? {|priority| priority.position_name.nil?}
  80
+  end
  81
+
  82
+  def test_compute_position_names_with_default_priority
  83
+    IssuePriority.clear_position_names
  84
+
  85
+    IssuePriority.compute_position_names
  86
+    assert_equal %w(lowest default high3 high2 highest), IssuePriority.active.all.sort.map(&:position_name)
  87
+  end
  88
+
  89
+  def test_compute_position_names_without_default_priority_should_split_priorities
  90
+    IssuePriority.clear_position_names
  91
+    IssuePriority.update_all :is_default => false
  92
+
  93
+    IssuePriority.compute_position_names
  94
+    assert_equal %w(lowest low2 default high2 highest), IssuePriority.active.all.sort.map(&:position_name)
  95
+  end
  96
+
  97
+  def test_adding_a_priority_should_update_position_names
  98
+    priority = IssuePriority.create!(:name => 'New')
  99
+    assert_equal %w(lowest default high4 high3 high2 highest), IssuePriority.active.all.sort.map(&:position_name)
  100
+  end
  101
+
  102
+  def test_destroying_a_priority_should_update_position_names
  103
+    IssuePriority.find_by_position_name('highest').destroy
  104
+    assert_equal %w(lowest default high2 highest), IssuePriority.active.all.sort.map(&:position_name)
  105
+  end
76 106
 end
7  test/unit/issue_test.rb
@@ -1821,4 +1821,11 @@ def test_journals_after_should_return_journals_with_greater_id
1821 1821
   def test_journals_after_with_blank_arg_should_return_all_journals
1822 1822
     assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('')
1823 1823
   end
  1824
+
  1825
+  def test_css_classes_should_include_priority
  1826
+    issue = Issue.new(:priority => IssuePriority.find(8))
  1827
+    classes = issue.css_classes.split(' ')
  1828
+    assert_include 'priority-8', classes
  1829
+    assert_include 'priority-highest', classes
  1830
+  end
1824 1831
 end

0 notes on commit 5c2a0af

Please sign in to comment.
Something went wrong with that request. Please try again.