0002-Extract-finding-the-default-or-middle-issue-priority.patch

Jan from Planio www.plan.io, 2020-01-07 06:23

Download (2.58 KB)

View differences:

app/models/issue_priority.rb
47 47
    update_all :position_name => nil
48 48
  end
49 49

  
50
  def self.default_or_middle
51
    default || begin
52
      priorities = active
53
      priorities[(priorities.size - 1) / 2]
54
    end
55
  end
56

  
50 57
  # Updates position_name for active priorities
51 58
  # Called from migration 20121026003537_populate_enumerations_position_name
52 59
  def self.compute_position_names
53 60
    priorities = active
54 61
    if priorities.any?
55
      default = priorities.detect(&:is_default?) || priorities[(priorities.size - 1) / 2]
62
      default_position = default_or_middle.position
56 63
      priorities.each_with_index do |priority, index|
57 64
        name =
58 65
          case
59
          when priority.position == default.position
66
          when priority.position == default_position
60 67
            "default"
61
          when priority.position < default.position
68
          when priority.position < default_position
62 69
            index == 0 ? "lowest" : "low#{index+1}"
63 70
          else
64 71
            index == (priorities.size - 1) ? "highest" : "high#{priorities.size - index}"
test/unit/issue_priority_test.rb
39 39
    assert_nil IssuePriority.default
40 40
  end
41 41

  
42
  def test_default_or_middle_should_return_the_default_priority
43
    assert_equal Enumeration.find_by_name('Normal'), IssuePriority.default_or_middle
44
  end
45

  
46
  def test_default_or_middle_should_return_middle_when_no_default_priority
47
    IssuePriority.update_all :is_default => false
48
    assert_equal Enumeration.find_by_name('High'), IssuePriority.default_or_middle
49
  end
50

  
42 51
  def test_should_be_an_enumeration
43 52
    assert IssuePriority.ancestors.include?(Enumeration)
44 53
  end
45
-