]> source.dussan.org Git - redmine.git/commitdiff
Extract finding the default or "middle" issue priority into its own class method...
authorGo MAEDA <maeda@farend.jp>
Tue, 21 Jan 2020 04:08:29 +0000 (04:08 +0000)
committerGo MAEDA <maeda@farend.jp>
Tue, 21 Jan 2020 04:08:29 +0000 (04:08 +0000)
Patch by Jan Schulz-Hofen.

git-svn-id: http://svn.redmine.org/redmine/trunk@19447 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue_priority.rb
test/unit/issue_priority_test.rb

index 05be79dd1c5a4c713370a2f134ddfaa06e3f9e16..8326b8ffa92078c8895e52ea4de7c03dc82a9343 100644 (file)
@@ -47,18 +47,25 @@ class IssuePriority < Enumeration
     update_all :position_name => nil
   end
 
+  def self.default_or_middle
+    default || begin
+      priorities = active
+      priorities[(priorities.size - 1) / 2]
+    end
+  end
+
   # Updates position_name for active priorities
   # Called from migration 20121026003537_populate_enumerations_position_name
   def self.compute_position_names
     priorities = active
     if priorities.any?
-      default = priorities.detect(&:is_default?) || priorities[(priorities.size - 1) / 2]
+      default_position = default_or_middle.position
       priorities.each_with_index do |priority, index|
         name =
           case
-          when priority.position == default.position
+          when priority.position == default_position
             "default"
-          when priority.position < default.position
+          when priority.position < default_position
             index == 0 ? "lowest" : "low#{index+1}"
           else
             index == (priorities.size - 1) ? "highest" : "high#{priorities.size - index}"
index 07cd5c6cf236796a345a866166c1392b1a988833..2fb863aceb638c875f56040a8aeb4d8de185ded4 100644 (file)
@@ -39,6 +39,15 @@ class IssuePriorityTest < ActiveSupport::TestCase
     assert_nil IssuePriority.default
   end
 
+  def test_default_or_middle_should_return_the_default_priority
+    assert_equal Enumeration.find_by_name('Normal'), IssuePriority.default_or_middle
+  end
+
+  def test_default_or_middle_should_return_middle_when_no_default_priority
+    IssuePriority.update_all :is_default => false
+    assert_equal Enumeration.find_by_name('High'), IssuePriority.default_or_middle
+  end
+
   def test_should_be_an_enumeration
     assert IssuePriority.ancestors.include?(Enumeration)
   end